当前位置:网站首页>Longest ascending subsequence model acwing 1014. mountaineering
Longest ascending subsequence model acwing 1014. mountaineering
2022-07-27 11:13:00 【T_ Y_ F666】
Longest ascending subsequence model AcWing 1014. Mountaineering
Original link
Algorithm tags
DP linear DP Longest ascending subsequence
Ideas
Ask for i Ending ascending sequence , With i The reverse rising at the end is the descending sequence , With i It is a sequence in which the dividing point rises first and then falls , Find the maximum .
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, 1, n+1){
a[i]=read();
}
int ans=0, ans1=0, ans2=0;
// With i Ending ascending sequence
rep(i, 1, n+1){
f[i]=1;
rep(j, 1, i){
if(a[j]<a[i]){
f[i]=max(f[i], f[j]+1);
}
}
ans1=max(ans1, f[i]);
}
// With i The reverse rising at the end is the descending sequence
Rep(i, n, 0){
f1[i]=1;
Rep(j, n, i+1){
if(a[j]<a[i]){
f1[i]=max(f1[i], f1[j]+1);
}
}
ans2=max(ans2, f1[i]);
}
// Go up first Post descent sequence
rep(i, 1, n+1){
ans=max(ans, f[i]+f1[i]-1);
}
printf("%lld\n", max({ans, ans1, ans2}));
}
And y The general code is consistent
y Master code
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n;
int h[N];
int f[N], g[N];
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i ++ ) scanf("%d", &h[i]);
for (int i = 0; i < n; i ++ )
{
f[i] = 1;
for (int j = 0; j < i; j ++ )
if (h[i] > h[j])
f[i] = max(f[i], f[j] + 1);
}
for (int i = n - 1; i >= 0; i -- )
{
g[i] = 1;
for (int j = n - 1; j > i; j -- )
if (h[i] > h[j])
g[i] = max(g[i], g[j] + 1);
}
int res = 0;
for (int i = 0; i < n; i ++ ) res = max(res, f[i] + g[i] - 1);
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 
边栏推荐
- GEE中下载过程中出现 Error: Image.clipToBoundsAndScale, argument 'input'
- Internal and external troubles of digital collection NFT "boring ape" bayc
- Description and feelings
- Shock simulation of engine mounting system transient modal dynamic analysis and response spectrum analysis
- antd中table hover上去的背景色样式修改
- Problems and Countermeasures of minors' digital security protection
- A verification test of the relationship between iteration number and entropy
- Redis high availability principle
- Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
- 最长上升子序列模型 AcWing 1014. 登山
猜你喜欢

XXX packages are looking for funding run 'NPM fund' for details solutions

最长上升子序列模型 AcWing 482. 合唱队形

How to build a real-time development platform to deeply release the value of enterprise real-time data?

推导STO双中心动能积分的详细展开式

img src为空或者src不存在,图片出现白色边框

迭代次数和熵之间关系的一个验证试验

How to build a data index system is the most effective. It will take you a quick start from 0 to 1

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

What is the mystery of the gate of the meta universe?

Derive the detailed expansion of STO double center kinetic energy integral
随机推荐
Use of pyquery
IO流_字符流、IO流小结、IO流案例总结
antd table中排序th阻止悬停变色+table悬停行变色+table表头变色
树形数据转换
学习笔记-微信支付
Object array de duplication
背包模型 AcWing 1022. 宠物小精灵之收服
img src为空或者src不存在,图片出现白色边框
IO流_数据输入输出流的概述和讲解
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
Wilderness search --- search iterations
Yum source installation
Sorry, you guys have something to deal with in the bank recently, which has been delayed
A verification test of the relationship between iteration number and entropy
Learning notes - simple server implementation
Remember not to copy your group work, students. Fortunately, you only passed two questions. Don't have an accident
学习笔记-简易服务器实现
Thank you for your likes and attention
C language 2: find the maximum value of three numbers, find the middle value of three numbers, and write program steps
The article will not keep VIP charges all the time. It will be open for a period of time