当前位置:网站首页>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
边栏推荐
- Drools terminates the execution of other rules after executing one rule
- Oracle从入门到精通(第4版)
- The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
- Simple understanding of ThreadLocal
- 正确遍历EntryList方法
- 线性DP AcWing 897. 最长公共子序列
- 深拷贝 事件总线
- Sse/avx instruction set and API of SIMD
- JDBC prevent SQL injection problems and solutions [preparedstatement]
- This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
猜你喜欢
[FFH] little bear driver calling process (take calling LED light driver as an example)
Rust search server, rust quick service finding tutorial
spfa AcWing 852. spfa判断负环
Docker compose configuration mysql, redis, mongodb
Redis sentinel mechanism and configuration
In development, why do you find someone who is paid more than you but doesn't write any code?
包管理工具
BOM DOM
JDBC 预防sql注入问题与解决方法[PreparedStatement]
AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning
随机推荐
About asp Net MVC project in local vs running response time is too long to access, the solution!
Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)
怎样写一篇赏心悦目的英文数学论文
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
Find the common ancestor of any two numbers in a binary tree
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
arcgis js 4. Add pictures to x map
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
BOM DOM
JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)
Introduction to CPU instruction set
spfa AcWing 851. SPFA finding the shortest path
线性DP AcWing 896. 最长上升子序列 II
Hash table acwing 840 Simulated hash table
堆 AcWing 838. 堆排序
Do you know all the interface test interview questions?
Hash table acwing 841 String hash
应用LNK306GN-TL 转换器、非隔离电源
Error in kubeadm join: [error port-10250]: port 10250 is in use [error fileavailable--etc kubernetes PKI
Direct control PTZ PTZ PTZ PTZ camera debugging (c)