当前位置:网站首页>线性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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Mysql database foundation
- kubenetes中port、targetPort、nodePort、containerPort的区别与联系
- The blink code based on Arduino and esp8266 runs successfully (including error analysis)
- Shutter encapsulated button
- This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
- IPhone 6 plus is listed in Apple's "retro products" list
- [C language] convert decimal numbers to binary numbers
- Test shift left and right
- (C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
- Docker-compose配置Mysql,Redis,MongoDB
猜你喜欢

Jenkins user rights management

Anti shake throttle

甜心教主:王心凌

"As a junior college student, I found out how difficult it is to counter attack after graduation."

寻找二叉树中任意两个数的公共祖先

(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?

Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
![1380. Lucky numbers in the matrix [two-dimensional array, matrix]](/img/8c/c050af5672268bc7e0df3250f7ff1d.jpg)
1380. Lucky numbers in the matrix [two-dimensional array, matrix]

kubenetes中port、targetPort、nodePort、containerPort的区别与联系

Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
随机推荐
Leetcode - Sword finger offer 51 Reverse pairs in an array
FastDateFormat为什么线程安全
Go learning notes - go based interprocess communication
SparkContext: Error initializing SparkContext解决方法
Leetcode922 sort array by parity II
Input a three digit number and output its single digit, ten digit and hundred digit.
LeetCode—剑指 Offer 37、38
ThreadLocal的简单理解
BOM DOM
Drools executes the specified rule
字符串回文hash 模板题 O(1)判字符串是否回文
堆(优先级队列)
Leetcode - Sword finger offer 37, 38
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
[FFH] little bear driver calling process (take calling LED light driver as an example)
Addition, deletion, modification and query of MySQL table (Advanced)
Intel 内部指令 --- AVX和AVX2学习笔记
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
记录一下MySql update会锁定哪些范围的数据
Bom Dom