当前位置:网站首页>The longest ascending subsequence model acwing 1016. The sum of the largest ascending subsequence
The longest ascending subsequence model acwing 1016. The sum of the largest ascending subsequence
2022-07-27 11:13:00 【T_ Y_ F666】
Longest ascending subsequence model AcWing 1016. The maximal ascending subsequence and
Original link
AcWing 1016. The maximal ascending subsequence and
Algorithm tags
DP linear DP Longest ascending subsequence
Ideas
Analog maximum ascending subsequence length , take f[i] Initialize to a[i]; f[i] Updated to f[i] And f[j]+a[i] The maximum value in .
Code
#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;
// With i Ending ascending sequence
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);
}
And y The general idea is the same
y Master code
#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;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- 最长上升子序列模型 AcWing 1014. 登山
- Redis high availability principle
- 6 find the smallest letter larger than the target letter
- Synaesthesia integrated de cellular super large-scale MIMO and high-frequency wireless access technology
- Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.
- Review and Prospect of encrypted traffic identification based on deep learning
- KEPServer配置
- Antd table+checkbox default value display
- 十年架构五年生活-07 年轻气盛的蜕变
- C language 2: find the maximum value of three numbers, find the middle value of three numbers, and write program steps
猜你喜欢

最长上升子序列模型 AcWing 1010. 拦截导弹

深析C语言的灵魂 -- 指针

2022牛客多校 (3)J.Journey

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

Sort th in antd table to prevent hovering color change +table hovering row color change +table header color change

SQL Server2000 database error

KEPServer配置

Knapsack model acwing 423. Picking herbs

最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼

Introduction to software vulnerability analysis (I)
随机推荐
十年架构五年生活-07 年轻气盛的蜕变
parsel的使用
Budweiser, a well-known beer, plans to launch NFT in an attempt to unveil the "long planned" uplink?
Background color style modification on table hover in antd
The largest square of leetcode (violence solving and dynamic programming solving)
洛谷 P3052 [USACO12MAR]Cows in a Skyscraper G
How to assemble a registry
ACM warm-up Exercise 2 in 2022 summer vacation (summary)
最短移动距离和形态复合体的熵
Taishan Office Technology Lecture: scaling and opening files
推导STO双中心动能积分的详细展开式
Redis high availability principle
KEPServer配置
MySQL installation (RPM package)
Description and feelings
熵与形态的非递进现象
Students, don't copy all my code, remember to change it, or we both want G
FAQs of "relay chain" and "dot" in Poka ecosystem
Overview of radar communication integrated waveform design
49字母异位分组和242有效的字母异位词