当前位置:网站首页>Linear DP acwing 898 Number triangle
Linear DP acwing 898 Number triangle
2022-07-02 12:43:00 【T_ Y_ F666】
linear DP AcWing 898. Digital triangle
Original link
Algorithm tags
Dynamic programming linear DP
Ideas

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 = 505, INF = 0x3f3f3f3f;
int w[N][N], f[N][N];
int 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);
n=read();
rep(i, 1, n+1){
rep(j, 1, i+1){
w[i][j]=read();
}
}
rep(i, 1, n+1){
f[n][i]=w[n][i];
}
Rep(i, n-1, -1){
rep(j, 1, i+1){
f[i][j]=max(f[i+1][j], f[i+1][j+1])+w[i][j];
}
}
printf("%lld", f[1][1]);
return 0;
}
Optimize the code
Because the number can be directly stored in f[N][N], When performing state transition operation, it is sufficient to accumulate , So it can be optimized w[N][N]
#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 = 505, INF = 0x3f3f3f3f;
int f[N][N];
int 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);
n=read();
rep(i, 1, n+1){
rep(j, 1, i+1){
f[i][j]=read();
}
}
Rep(i, n-1, -1){
rep(j, 1, i+1){
f[i][j]+=max(f[i+1][j], f[i+1][j+1]);
}
}
printf("%lld", f[1][1]);
return 0;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- Typora+docsify quick start
- The second composition template of postgraduate entrance examination English / chart composition, English chart composition is enough
- Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
- 模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
- Shuttle encapsulated AppBar
- PR 2021 quick start tutorial, learn about the and functions of the timeline panel
- 浏览器node事件循环
- Embedded Software Engineer career planning
- JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
- High performance erasure code coding
猜你喜欢

BOM DOM

Sparkcontext: error initializing sparkcontext solution

Redis bloom filter

3 A VTT端接 稳压器 NCP51200MNTXG资料

染色法判定二分图 AcWing 860. 染色法判定二分图

Heap acwing 839 Simulated reactor

Distributed machine learning framework and high-dimensional real-time recommendation system

Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately

js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)

Deep copy event bus
随机推荐
Execute any method of any class through reflection
Intel internal instructions - AVX and avx2 learning notes
js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)
中国交通标志检测数据集
async/await 异步函数
深拷貝 事件總線
About the loading of layer web spring layer components, the position of the layer is centered
线性DP AcWing 896. 最长上升子序列 II
AI mid stage technology research
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY
百款拿来就能用的网页特效,不来看看吗?
Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
Oracle从入门到精通(第4版)
std::vector批量导入快速去重方法
[ybtoj advanced training guidance] judgment overflow [error]
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
Leetcode - Sword finger offer 51 Reverse pairs in an array
怎样写一篇赏心悦目的英文数学论文
Input box assembly of the shutter package