当前位置:网站首页>数字三角形模型 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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- QT charts use (rewrite qchartview to realize some custom functions)
- 2-3 lookup tree
- Laravel8 uses passport login and JWT (generate token)
- grpc、oauth2、openssl、双向认证、单向认证等专栏文章目录
- Composer change domestic image
- Rapid integration of authentication services - harmonyos platform
- Virtual address space
- GOLand idea intellij 无法输入汉字
- 如何理解分布式架构和微服务架构呢
- Low success rate of unit test report
猜你喜欢

SSM integration

归并排序和非比较排序

【踩坑】nacos注册一直连接localhost:8848,no available server

Data type - floating point (C language)
![[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?](/img/ff/81a7b2ec08a6a422a5cf578c1fed13.jpg)
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?

指针进阶,字符串函数

Three series of BOM elements

let const

AVL平衡二叉搜索树

联想混合云Lenovo xCloud:4大产品线+IT服务门户
随机推荐
Introduction to data fragmentation
数据分析方法论与前人经验总结2【笔记干货】
SSM integration
uniapp 微信小程序监测网络
Snyk dependency security vulnerability scanning tool
Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
Go write a program that runs within a certain period of time
Iptables' state module (FTP service exercise)
go写一个在一定时间内运行的程序
[Yugong series] February 2022 U3D full stack class 006 unity toolbar
字符串操作
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
Data type - integer (C language)
ncs成都新电面试经验
[machine learning] watermelon book data set_ data sharing
联想混合云Lenovo xCloud:4大产品线+IT服务门户
PLSQL的安装和配置
Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
Through the "last mile" of legal services for the masses, fangzheng Puhua labor and personnel law self-service consulting service platform has been frequently "praised"