当前位置:网站首页>最长上升子序列模型 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 difference of iteration number and information entropy
- Yiwen counts NFT projects valued at more than US $100million
- Sorry, you guys have something to deal with in the bank recently, which has been delayed
- Use kaggle to run Li Hongyi's machine learning homework
- Ten year structure five year life-07 young and vigorous transformation
- 学习笔记-简易服务器实现
- 15th largest value of data flow
- Pyqt5 rapid development and practice 4.2 QWidget
- GEE中下载过程中出现 Error: Image.clipToBoundsAndScale, argument 'input'
- 14 check whether integers and their multiples exist
猜你喜欢

熵与形态的非递进现象

Wilderness search --- search iterations

Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing

Wenzhou University X kangaroo cloud: how to "know well" in the construction of higher talent education

SQL Server2000数据库错误

15 design movie rental system

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

如何创建一个带诊断工具的.NET镜像

荒野觅踪---寻找迭代次数

Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
随机推荐
pyquery 的使用
学习笔记-简易服务器实现
MySQL installation (RPM package)
ASP. Net core dependency injection journey: 1. Theoretical concepts
antd table中排序th阻止悬停变色+table悬停行变色+table表头变色
Redis+caffeine two-level cache enables smooth access speed
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
49字母异位分组和242有效的字母异位词
What changes will metauniverse bring to the music industry in the trillion market?
Tree data conversion
洛谷P1896 互不侵犯
Why is the data service API the standard configuration of the data midrange when we take the last mile of the data midrange?
Overview of radar communication integrated waveform design
SQL Server2000 database error
Application scenarios, key technologies and network architecture of communication perception integration
Sorry, you guys have something to deal with in the bank recently, which has been delayed
Students, don't copy all my code, remember to change it, or we both want G
Ansible
学习笔记-微信支付
神经网络学习笔记