当前位置:网站首页>Digital triangle model acwing 275 Pass a note
Digital triangle model acwing 275 Pass a note
2022-07-07 08:51:00 【T_ Y_ F666】
Digital triangle model AcWing 275. Pass slip
Original link
Algorithm tags
Dynamic programming linear DP
Ideas
Optimization of row number value range
Code
#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();
}
}
// Sum of row and column values The row determines the column
rep(k, 2, n+m+1){
// First time i That's ok
rep(i1, 1, n+1){
// Second time i That's ok
rep(i2, 1, n+1){
// j1 It means the first time j Column j2 It means the second time j Column
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;
}
Line number value range optimization code
#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();
}
}
// Sum of row and column values The row determines the column
rep(k, 2, n+m+1){
// First time i That's ok
rep(i1, max(1ll, k-m), min(k, n)+1){
// Second time i That's ok
rep(i2, max(1ll, k-m), min(k, n)+1){
// k-i1 Indicates the number of columns for the first time k-i2 Indicates the number of secondary columns
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;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support
边栏推荐
- 如何统计项目代码行数
- Greenplum 6.x build_ install
- PPT模板、素材下载网站(纯干货,建议收藏)
- LeetCode 736. Lisp 语法解析
- [MySQL] detailed explanation of trigger content of database advanced
- Introduction to data fragmentation
- Implement custom memory allocator
- 数字三角形模型 AcWing 1027. 方格取数
- Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
- Lenovo hybrid cloud Lenovo xcloud: 4 major product lines +it service portal
猜你喜欢
NCS Chengdu New Electric interview Experience
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
Appeler l'interface du moteur de création du service multimédia de jeu Huawei renvoie le Code d'erreur 1002, le message d'erreur: les paramètres sont l'erreur
21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design
数字三角形模型 AcWing 1027. 方格取数
数据分析方法论与前人经验总结2【笔记干货】
Mountaineering team (DFS)
指针进阶,字符串函数
Platformization, a fulcrum of strong chain complementing chain
Greenplum6.x监控软件搭建
随机推荐
Analysis of abnormal channel number information before and after AGC re signature service
登山小分队(dfs)
Lenovo hybrid cloud Lenovo xcloud: 4 major product lines +it service portal
String operation
调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology
Pointer advanced, string function
最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼
leetcode135. Distribute candy
Greenplum6.x重新初始化
Mountaineering team (DFS)
What are the advantages of commas in conditional statements- What is the advantage of commas in a conditional statement?
Greenplum6.x-版本变化记录-常用手册
You should use Google related products with caution
[Chongqing Guangdong education] audio visual language reference materials of Xinyang Normal University
How to add a mask of a target in a picture
Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot
RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (5 x 5). Kernel size c
MySQL partition explanation and operation statement
数据分片介绍