当前位置:网站首页>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. 登山
- 解决 ImportError: cannot import name 'abs' 导入tensorflow报错
- Students, don't copy all my code, remember to change it, or we both want G
- Play with the cluster configuration center and learn about the Taier console
- 6 find the smallest letter larger than the target letter
- 349两个数组的交集和01两数之和
- img src为空或者src不存在,图片出现白色边框
- BeautifulSoup的使用
- 12 is at least twice the maximum number of other numbers
- ACM warm-up Exercise 1 in 2022 summer vacation (summary)
猜你喜欢

Longest ascending subsequence model acwing 1010. Interceptor missile

最长上升子序列模型 AcWing 1012. 友好城市

The longest ascending subsequence model acwing 1017. The glider wing of the strange thief Kidd

15 design movie rental system

Longest ascending subsequence model acwing 1012. Sister Cities

ACM warm-up Exercise 2 in 2022 summer vacation (summary)

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

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

pyquery 的使用

推导STO双中心动能积分的详细展开式
随机推荐
Tree data conversion
IO stream_ Overview and explanation of data input and output flow
泰山OFFICE技术讲座:缩放比例与打开文件
MIMO array 3D imaging technology based on mobile terminal
Overview of radar communication integrated waveform design
2022牛客多校训练(3)A-Ancestor 题目翻译
Redis high availability principle
How to build a data index system is the most effective. It will take you a quick start from 0 to 1
基于FPGA的ECG信号采集,存储以及传输系统verilog实现
6 find the smallest letter larger than the target letter
Object array de duplication
Shock simulation of engine mounting system transient modal dynamic analysis and response spectrum analysis
神经网络学习笔记
Thank you for your likes and attention
Influence of black and white pixel distribution on iteration times
What changes will metauniverse bring to the music industry in the trillion market?
Kangaroo cloud stack based on CBO in spark SQL optimization
BeautifulSoup的使用
ethereum rpc
Miscellaneous records of Finance