当前位置:网站首页>最长上升子序列模型 AcWing 1016. 最大上升子序列和
最长上升子序列模型 AcWing 1016. 最大上升子序列和
2022-07-27 10:35:00 【T_Y_F666】
最长上升子序列模型 AcWing 1016. 最大上升子序列和
原题链接
算法标签
DP 线性DP 最长上升子序列
思路
类比最大上升子序列长度, 将f[i]初始化为a[i]; f[i]更新为f[i]与f[j]+a[i]中最大值即可。
代码
#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 = 1005, INF = 0x3f3f3f3f;
int f[N], f1[N], a[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);
int n=read();
rep(i, 0, n){
a[i]=read();
}
int ans=0;
// 以i结尾上升序列
rep(i, 0, n){
f[i]=a[i];
rep(j, 0, i){
if(a[j]<a[i]){
f[i]=max(f[i], f[j]+a[i]);
}
}
ans=max(ans, f[i]);
}
printf("%lld\n", ans);
}
与y总思路一致
y总代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n;
int w[N];
int f[N];
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i ++ ) scanf("%d", &w[i]);
int res = 0;
for (int i = 0; i < n; i ++ )
{
f[i] = w[i];
for (int j = 0; j < i; j ++ )
if (w[i] > w[j])
f[i] = max(f[i], f[j] + w[i]);
res = max(res, f[i]);
}
printf("%d\n", res);
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- The influence of the number of non-zero values in the picture on Classification
- Shock simulation of engine mounting system transient modal dynamic analysis and response spectrum analysis
- TensorFlow张量运算函数集
- Background color style modification on table hover in antd
- The largest square of leetcode (violence solving and dynamic programming solving)
- 对象数组去重
- Remember not to copy your group work, students. Fortunately, you only passed two questions. Don't have an accident
- Redis high availability principle
- antd table+checkbox 默认值显示
- Research on synaesthesia integration and its challenges
猜你喜欢

SQL Server2000 database error

An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft

The second method of calculating overlapping integral

Yiwen counts NFT projects valued at more than US $100million

ASP. Net core dependency injection journey: 1. Theoretical concepts

Compete for the key battle of stock users and help enterprises build a perfect labeling system - 01 live review

Chengying, kangaroo cloud one-stop fully automated operation and maintenance steward, is officially open source

The largest square of leetcode (violence solving and dynamic programming solving)

antd table中排序th阻止悬停变色+table悬停行变色+table表头变色

Influence of black and white pixel distribution on iteration times
随机推荐
Overview of data security in fog computing
The influence of the number of non-zero values in the picture on Classification
SQL Server2000数据库错误
ethereum rpc
对象数组去重
深析C语言的灵魂 -- 指针
Why is the data service API the standard configuration of the data midrange when we take the last mile of the data midrange?
Object array de duplication
[QNX hypervisor 2.2 user manual]9.9 logger
IO流_字符流、IO流小结、IO流案例总结
荒野觅踪---寻找迭代次数
antd table中排序th阻止悬停变色+table悬停行变色+table表头变色
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
【FPGA教程案例40】通信案例10——基于FPGA的简易OFDM系统verilog实现
Detailed explanation of status code meaning
学习笔记-微信支付
Verilog implementation of ECG signal acquisition, storage and transmission system based on FPGA
洛谷P1441 砝码称重
Yum source installation
Substr and substring function usage in SQL