当前位置:网站首页>数字三角形模型 AcWing 275. 传纸条
数字三角形模型 AcWing 275. 传纸条
2022-07-27 10:35: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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 推导重叠积分的详细展开式 STO overlap integrals
- Remember not to copy your group work, students. Fortunately, you only passed two questions. Don't have an accident
- Students, don't copy all my code, remember to change it, or we both want G
- img src为空或者src不存在,图片出现白色边框
- parsel的使用
- tensorflow运行报错解决方法
- How to build a real-time development platform to deeply release the value of enterprise real-time data?
- 发动机悬置系统冲击仿真-瞬时模态动态分析与响应谱分析
- Yonbuilder enables innovation, and the "golden keyboard Award" of the fourth UFIDA developer competition is open!
- Kangaroo cloud stack based on CBO in spark SQL optimization
猜你喜欢

How to assemble a registry

FAQs of "relay chain" and "dot" in Poka ecosystem

Asustek unparalleled, this may be the best affordable high brush thin notebook on the screen

pyquery 的使用

2022牛客多校 (3)J.Journey

Influence of black and white pixel distribution on iteration times

Chengying, kangaroo cloud one-stop fully automated operation and maintenance steward, is officially open source

Redis+caffeine two-level cache enables smooth access speed

解决 ImportError: cannot import name 'abs' 导入tensorflow报错

49字母异位分组和242有效的字母异位词
随机推荐
Neural network learning notes
I've compromised. Since everyone wants to call me Yelin, there's nothing I can do
XXX packages are looking for funding run 'NPM fund' for details solutions
正则form表单判断
Yonbuilder enables innovation, and the "golden keyboard Award" of the fourth UFIDA developer competition is open!
Non progressive phenomena of entropy and morphology
Using skills of word
How to assemble a registry
Remember not to copy your group work, students. Fortunately, you only passed two questions. Don't have an accident
Shock simulation of engine mounting system transient modal dynamic analysis and response spectrum analysis
img src为空或者src不存在,图片出现白色边框
学习笔记-微信支付
Antd table+checkbox default value display
MIMO array 3D imaging technology based on mobile terminal
FAQs of "relay chain" and "dot" in Poka ecosystem
Ansible
NFT leaderboard -nft real offer latest address: NFT leaderboard.com
Regular form form judgment
Time and power allocation method to ensure fairness in sensor fusion system
Li Hongyi_ Machine learning_ Assignment 4 (detailed explanation)_ HW4 Classify the speakers