当前位置:网站首页>The longest ascending subsequence model acwing 1014 Mountaineering
The longest ascending subsequence model acwing 1014 Mountaineering
2022-07-07 14: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
边栏推荐
- 566. Reshaping the matrix
- NDK beginner's study (1)
- Common response status codes
- Redis can only cache? Too out!
- TPG x AIDU | AI leading talent recruitment plan in progress!
- FCOS3D label assignment
- Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1
- Regular expression integer positive integer some basic expressions
- SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
- SAKT方法部分介绍
猜你喜欢
Vscode configuration uses pylint syntax checker
Evolution of customer service hotline of dewu
. Net core about redis pipeline and transactions
Excerpt from "misogyny: female disgust in Japan"
Selenium库
Take you to master the three-tier architecture (recommended Collection)
Redis 核心数据结构 & Redis 6 新特性详
常用數字信號編碼之反向不歸零碼碼、曼徹斯特編碼、差分曼徹斯特編碼
Redis can only cache? Too out!
最长上升子序列模型 AcWing 482. 合唱队形
随机推荐
js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
PostgreSQL array type, each splice
请问,我kafka 3个分区,flinksql 任务中 写了 join操作,,我怎么单独给join
常用數字信號編碼之反向不歸零碼碼、曼徹斯特編碼、差分曼徹斯特編碼
【AI实战】应用xgboost.XGBRegressor搭建空气质量预测模型(二)
最长上升子序列模型 AcWing 1012. 友好城市
Cargo placement problem
requires php ~7.1 -&gt; your PHP version (7.0.18) does not satisfy that requirement
现在网上开户安全么?那么网上开户选哪个证券公司?
2022-7-7 Leetcode 844. Compare strings with backspace
Realization of search box effect [daily question]
Reverse non return to zero code, Manchester code and differential Manchester code of common digital signal coding
Is it safe to open an account online now? Which securities company should I choose to open an account online?
Laravel Form-builder使用
请问,如图,pyhon云函数提示使用了 pymysql模块,这个是怎么回事?
供应链供需预估-[时间序列]
FCOS3D label assignment
UML 顺序图(时序图)
First choice for stock account opening, lowest Commission for stock trading account opening, is online account opening safe
请问,PTS对数据库压测有好方案么?