当前位置:网站首页>数字三角形模型 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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- IMG SRC is empty or SRC does not exist, and the picture has a white border
- Deep analysis: what is diffusion model?
- Chunjun supports DDL conversion and automatic execution of heterogeneous data sources - dtmo 02 review (including course playback + courseware)
- 11 wrong set
- Time and power allocation method to ensure fairness in sensor fusion system
- The difference between scalar, vector, matrix and tensor in deep learning
- The second method of calculating overlapping integral
- Background color style modification on table hover in antd
- Why is the data service API the standard configuration of the data midrange when we take the last mile of the data midrange?
- 如何组装一个注册中心
猜你喜欢
![Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.](/img/7f/f42f9267cdff35522c260ef073bab9.png)
Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.

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

Introduction to software vulnerability analysis (I)

Wenzhou University X kangaroo cloud: how to "know well" in the construction of higher talent education

The difference of iteration number and information entropy

Internal and external troubles of digital collection NFT "boring ape" bayc

Cancer DDD

荒野觅踪---寻找迭代次数

A verification test of the relationship between iteration number and entropy

Shock simulation of engine mounting system transient modal dynamic analysis and response spectrum analysis
随机推荐
Play with the cluster configuration center and learn about the Taier console
DNS principle and resolution process
Use__ slots__ And__ dict__ To save space (it's simply a qualitative leap, and leetcode's personal test is effective)
How to build a data index system is the most effective. From 0 to 1, we will take you a quick start - 02 live review
Antd table+checkbox default value display
Cancer DDD
Use of pyquery
TensorFlow张量运算函数集
推导STO双中心动能积分的详细展开式
The second method of calculating overlapping integral
学习笔记-微信支付
Wenzhou University X kangaroo cloud: how to "know well" in the construction of higher talent education
熵与形态的非递进现象
IO stream_ Overview and explanation of data input and output flow
Delete in MySQL: the difference between delete, drop and truncate
tf.AUTO_ Function of reuse
Chunjun supports DDL conversion and automatic execution of heterogeneous data sources - dtmo 02 review (including course playback + courseware)
Miscellaneous records of Finance
基于FPGA的ECG信号采集,存储以及传输系统verilog实现
How to assemble a registry