当前位置:网站首页>线性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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- IPhone 6 plus is listed in Apple's "retro products" list
- CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
- drools执行指定的规则
- CDH6之Sqoop添加数据库驱动
- 单指令多数据SIMD的SSE/AVX指令集和API
- Sparkcontext: error initializing sparkcontext solution
- Deep Copy Event bus
- (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?
- Simple understanding of ThreadLocal
- 浏览器存储方案
猜你喜欢
drools决策表的简单使用
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
堆(優先級隊列)
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
浏览器存储方案
MySQL与PostgreSQL抓取慢sql的方法
SparkContext: Error initializing SparkContext解决方法
中国交通标志检测数据集
Simple use of drools decision table
JSON序列化 与 解析
随机推荐
单指令多数据SIMD的SSE/AVX指令集和API
Gaode map test case
The differences and relationships among port, targetport, nodeport and containerport in kubenetes
FastDateFormat为什么线程安全
Lombok common annotations
Leetcode14 longest public prefix
Fluent fluent library encapsulation
H5 to app
Calculate the maximum path sum of binary tree
Embedded Software Engineer career planning
CDA data analysis -- common knowledge points induction of Excel data processing
Input a three digit number and output its single digit, ten digit and hundred digit.
Jenkins voucher management
Multiply LCA (nearest common ancestor)
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
Test shift left and right
Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
高性能纠删码编码
Enhance network security of kubernetes with cilium
Differences between nodes and sharding in ES cluster