当前位置:网站首页>最长上升子序列模型 AcWing 1014. 登山
最长上升子序列模型 AcWing 1014. 登山
2022-07-27 10:35:00 【T_Y_F666】
最长上升子序列模型 AcWing 1014. 登山
原题链接
算法标签
DP 线性DP 最长上升子序列
思路
分别求以i结尾上升序列,以i结尾反向上升即下降序列,以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, 1, n+1){
a[i]=read();
}
int ans=0, ans1=0, ans2=0;
// 以i结尾上升序列
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]);
}
// 以i结尾反向上升即下降序列
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]);
}
// 先上升 后下降序列
rep(i, 1, n+1){
ans=max(ans, f[i]+f1[i]-1);
}
printf("%lld\n", max({ans, ans1, ans2}));
}
与y总代码思路一致
y总代码
#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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- parsel的使用
- [QNX hypervisor 2.2 user manual]9.9 logger
- 对象数组去重
- 8 find subsequences with a maximum length of K
- Wenzhou University X kangaroo cloud: how to "know well" in the construction of higher talent education
- Symmetric encryption and asymmetric encryption
- Openatom openharmony sub forum, see you today at 14:00! Wonderful release of memorabilia attached
- The open source project Taier version 1.1 was officially released, and the list of new functions is fast
- Background color style modification on table hover in antd
- 黑白像素分布对迭代次数的影响
猜你喜欢
![Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.](/img/7f/f42f9267cdff35522c260ef073bab9.png)
Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.

Chengying, kangaroo cloud one-stop fully automated operation and maintenance steward, is officially open source

Yonbuilder enables innovation, and the "golden keyboard Award" of the fourth UFIDA developer competition is open!

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

Yiwen counts NFT projects valued at more than US $100million

Influence of black and white pixel distribution on iteration times

Internal and external troubles of digital collection NFT "boring ape" bayc

How to build a data index system is the most effective. From 0 to 1, we will take you a quick start - 02 live review

如何组装一个注册中心

Redis+caffeine two-level cache enables smooth access speed
随机推荐
神经网络学习笔记
Learning notes - wechat payment
Synaesthesia integrated de cellular super large-scale MIMO and high-frequency wireless access technology
洛谷P1896 互不侵犯
tensorflow运行报错解决方法
IO流_数据输入输出流的概述和讲解
Problems and Countermeasures of minors' digital security protection
Deep analysis: what is diffusion model?
Neural network learning notes
Ten year structure five year life-07 young and vigorous transformation
Maximized array sum after 13 K negations
11 wrong set
14 check whether integers and their multiples exist
Self optimization of wireless cell load balancing based on machine learning technology
Today's code farmer girl learned notes about event operation and ref attribute, and did the practice of form two-way binding
Data assets are king. How to analyze the relationship between enterprise digital transformation and data asset management?
Introduction to software vulnerability analysis (I)
How to build a data index system is the most effective. It will take you a quick start from 0 to 1
7 row K with the weakest combat effectiveness in the matrix
Error: image clipToBoundsAndScale, argument 'input'