当前位置:网站首页>最长上升子序列模型 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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- MySQL installation (RPM package)
- 洛谷P1896 互不侵犯
- Overview of data security in fog computing
- KEPServer配置
- ethereum rpc
- A measurement method of 5g air interface one-way delay and its reliability
- 2022牛客多校 (3)J.Journey
- Application of 5g private network in smart medicine
- SQL Server2000数据库错误
- Self optimization of wireless cell load balancing based on machine learning technology
猜你喜欢

ethereum rpc

How to modify the strict mode under MySQL so that adding new users by inserting user table is successful

The influence of the number of non-zero values in the picture on Classification

parsel的使用

NFT leaderboard -nft real offer latest address: NFT leaderboard.com

The difference of iteration number and information entropy

Redis high availability principle

KEPServer配置

Use of pyquery

One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database
随机推荐
11 wrong set
C language 2: find the maximum value of three numbers, find the middle value of three numbers, and write program steps
Cancer DDD
Shock simulation of engine mounting system transient modal dynamic analysis and response spectrum analysis
How to modify the strict mode under MySQL so that adding new users by inserting user table is successful
Yum source installation
Synaesthesia integrated de cellular super large-scale MIMO and high-frequency wireless access technology
parsel的使用
10 complete half of the questions
7 row K with the weakest combat effectiveness in the matrix
【FPGA教程案例40】通信案例10——基于FPGA的简易OFDM系统verilog实现
JVM judges that the object is dead, and practices verify GC recycling
Problems and Countermeasures of minors' digital security protection
Ansible
Antd table+checkbox default value display
Background color style modification on table hover in antd
涌现与形态的局部差异和整体差异
Internal and external troubles of digital collection NFT "boring ape" bayc
SQL Server2000数据库错误
Wilderness search --- search iterations