当前位置:网站首页>线性DP AcWing 898. 数字三角形
线性DP AcWing 898. 数字三角形
2022-07-02 09:43:00 【T_Y_F666】
线性DP AcWing 898. 数字三角形
原题链接
算法标签
动态规划 线性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 = 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;
}
优化代码
由于可以直接将数字存储至f[N][N],进行状态转移运算时累加即可,所以可以优化掉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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 2.6 using recursion and stack - [tower of Hanoi problem]
- Writing method of then part in drools
- Simple understanding of ThreadLocal
- (C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
- Drools executes the specified rule
- drools中then部分的写法
- Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
- Drools dynamically add, modify, and delete rules
- Deep Copy Event bus
- CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
猜你喜欢

Find the common ancestor of any two numbers in a binary tree

甜心教主:王心凌
![2.7 binary tree, post order traversal - [FBI tree]](/img/6b/1ded3632cc69329d7b2762ce47fdbc.jpg)
2.7 binary tree, post order traversal - [FBI tree]

AI mid stage technology research

This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry

Deep understanding of P-R curve, ROC and AUC

初始JDBC 编程

Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
![[ybtoj advanced training guide] similar string [string] [simulation]](/img/eb/acfefc7f85018fe9365d13502e2b0a.jpg)
[ybtoj advanced training guide] similar string [string] [simulation]

Record the range of data that MySQL update will lock
随机推荐
深拷貝 事件總線
High performance erasure code coding
MySQL and PostgreSQL methods to grab slow SQL
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
Differences between nodes and sharding in ES cluster
防抖 节流
From scratch, develop a web office suite (3): mouse events
甜心教主:王心凌
Map and set
Find the common ancestor of any two numbers in a binary tree
Fluent fluent library encapsulation
2.6 using recursion and stack - [tower of Hanoi problem]
Deep copy event bus
Record the range of data that MySQL update will lock
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
drools执行完某个规则后终止别的规则执行
Drools executes the specified rule
Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
mysql表的增删改查(进阶)
Introduction to CPU instruction set