当前位置:网站首页>数字三角形模型 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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Opencv learning note 5 - gradient calculation / edge detection
- Train your dataset with swinunet
- [Yu Yue education] C language programming reference of Zhongbei College of Nanjing Normal University
- Quick sorting (detailed illustration of single way, double way, three way)
- [Chongqing Guangdong education] audio visual language reference materials of Xinyang Normal University
- opencv之图像分割
- Exercise arrangement 2.10, 11
- A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)
- About using CDN based on Kangle and EP panel
- Tips for using jeditabletable
猜你喜欢
JS operation
Opencv learning note 4 - expansion / corrosion / open operation / close operation
let const
Data type - floating point (C language)
AVL balanced binary search tree
Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
Download and install orcale database11.2.0.4
Laravel8 uses passport login and JWT (generate token)
如何在HarmonyOS应用中集成App Linking服务
Opencv learning notes 1 -- several methods of reading images
随机推荐
You should use Google related products with caution
关于基于kangle和EP面板使用CDN
求有符号数的原码、反码和补码【C语言】
基本数据类型和string类型互相转化
如何理解分布式架构和微服务架构呢
为什么要选择云原生数据库
Exercise arrangement 2.10, 11
南京商品房买卖启用电子合同,君子签助力房屋交易在线网签备案
Mock.js用法详解
一种适用于应用频繁测试下快速查看Pod的日志的方法(grep awk xargs kuberctl)
[Chongqing Guangdong education] organic electronics (Bilingual) reference materials of Nanjing University of Posts and Telecommunications
QT charts use (rewrite qchartview to realize some custom functions)
Golang compilation constraint / conditional compilation (/ / +build < tags>)
opencv之图像分割
Opencv learning note 3 - image smoothing / denoising
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
Rapid integration of authentication services - harmonyos platform
Golan idea IntelliJ cannot input Chinese characters
Opencv converts 16 bit image data to 8 bits and 8 to 16
Go语言中,函数是一种类型