当前位置:网站首页>数字三角形模型 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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Opengauss kernel analysis - statistics and row count estimation
- FAQs of "relay chain" and "dot" in Poka ecosystem
- Neural network learning notes
- 10 complete half of the questions
- 11 wrong set
- How to modify the strict mode under MySQL so that adding new users by inserting user table is successful
- The open source project Taier version 1.1 was officially released, and the list of new functions is fast
- Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
- MIMO array 3D imaging technology based on mobile terminal
- 49字母异位分组和242有效的字母异位词
猜你喜欢

An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft

Redis high availability principle

Iptables prevent nmap scanning and binlog explanation

pyquery 的使用

Openatom openharmony sub forum, see you today at 14:00! Wonderful release of memorabilia attached

SQL Server2000数据库错误

Wilderness search --- search iterations

MySQL installation (RPM package)

Antd table+checkbox default value display

最短移动距离和形态复合体的熵
随机推荐
Play with the cluster configuration center and learn about the Taier console
泰山OFFICE技术讲座:缩放比例与打开文件
Learning notes - simple server implementation
Sort th in antd table to prevent hovering color change +table hovering row color change +table header color change
Error: image clipToBoundsAndScale, argument 'input'
Substr and substring function usage in SQL
Shock simulation of engine mounting system transient modal dynamic analysis and response spectrum analysis
Object array de duplication
An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft
Symmetric encryption and asymmetric encryption
A measurement method of 5g air interface one-way delay and its reliability
Remember not to copy your group work, students. Fortunately, you only passed two questions. Don't have an accident
The largest square of leetcode (violence solving and dynamic programming solving)
Today's code farmer girl summarized her notes about NPM package management and URL module
推导STO双中心动能积分的详细展开式
TensorFlow张量运算函数集
Description and feelings
49字母异位分组和242有效的字母异位词
Influence of black and white pixel distribution on iteration times
How to modify the strict mode under MySQL so that adding new users by inserting user table is successful