当前位置:网站首页>数字三角形模型 AcWing 275. 传纸条
数字三角形模型 AcWing 275. 传纸条
2022-07-07 06:11:00 【T_Y_F666】
数字三角形模型 AcWing 275. 传纸条
原题链接
算法标签
动态规划 线性DP
思路

行数取值范围优化

代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>=b;--i)
using namespace std;
const int N = 105;
int f[2*N][N][N], a[N][N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read(),m=read();
rep(i, 1, n+1){
rep(j, 1, m+1){
a[i][j]=read();
}
}
// 行列值之和 由行确定列
rep(k, 2, n+m+1){
// 第一次第i行
rep(i1, 1, n+1){
// 第二次第i行
rep(i2, 1, n+1){
// j1表示第一次第j列 j2表示第二次第j列
int j1=k-i1, j2=k-i2;
if(j1>=1&&j1<=m&&j2>=1&&j2<=m){
int t=a[i1][j1];
if(i1-i2){
t+=a[i2][j2];
}
f[k][i1][i2]=max({f[k][i1][i2], f[k-1][i1-1][i2-1]+t, f[k-1][i1-1][i2]+t, f[k-1][i1][i2-1]+t, f[k-1][i1][i2]+t});
}
}
}
}
printf("%lld", f[n+m][n][n]);
return 0;
}
行数取值范围优化代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>=b;--i)
using namespace std;
const int N = 105;
int f[2*N][N][N], a[N][N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read(),m=read();
rep(i, 1, n+1){
rep(j, 1, m+1){
a[i][j]=read();
}
}
// 行列值之和 由行确定列
rep(k, 2, n+m+1){
// 第一次第i行
rep(i1, max(1ll, k-m), min(k, n)+1){
// 第二次第i行
rep(i2, max(1ll, k-m), min(k, n)+1){
// k-i1表示第一次列数 k-i2表示第二次列数
int t=a[i1][k-i1];
if(i1-i2){
t+=a[i2][k-i2];
}
f[k][i1][i2]=max({f[k][i1][i2], f[k-1][i1-1][i2-1]+t, f[k-1][i1-1][i2]+t, f[k-1][i1][i2-1]+t, f[k-1][i1][i2]+t});
}
}
}
printf("%lld", f[n+m][n][n]);
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 打通法律服务群众“最后一公里”,方正璞华劳动人事法律自助咨询服务平台频获“点赞”
- ES6_ Arrow function
- National standard gb28181 protocol video platform easygbs adds streaming timeout configuration
- Virtual address space
- Three series of BOM elements
- [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
- Merge sort and non comparison sort
- Golang 编译约束/条件编译 ( // +build <tags> )
- What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
- [Chongqing Guangdong education] audio visual language reference materials of Xinyang Normal University
猜你喜欢

关于基于kangle和EP面板使用CDN

In go language, function is a type

leetcode135. Distribute candy

Implement your own dataset using bisenet

联想混合云Lenovo xCloud:4大产品线+IT服务门户

let const

調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
![Data analysis methodology and previous experience summary 2 [notes dry goods]](/img/e1/643e847a777e1effcbd39f8b009e2b.png)
Data analysis methodology and previous experience summary 2 [notes dry goods]

The field value in Splunk subquery fuzzy matching CSV is*

【MySQL】数据库进阶之触发器内容详解
随机推荐
数据分析方法论与前人经验总结2【笔记干货】
All about PDF crack, a complete solution to meet all your PDF needs
Opencv converts 16 bit image data to 8 bits and 8 to 16
Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
go写一个在一定时间内运行的程序
基本数据类型和string类型互相转化
opencv 将16位图像数据转为8位、8转16
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
Count sort (diagram)
JS的操作
Iptables' state module (FTP service exercise)
String operation
GOLand idea intellij 无法输入汉字
What are the advantages of commas in conditional statements- What is the advantage of commas in a conditional statement?
求有符号数的原码、反码和补码【C语言】
Several ways of lambda used in functions in kotlin (higher-order functions)
POJ - 3784 Running Median(对顶堆)
JEditableTable的使用技巧
MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)