当前位置:网站首页>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 
边栏推荐
- Best practice | using Tencent cloud AI willingness to audit as the escort of telephone compliance
- Common response status codes
- Social responsibility · value co creation, Zhongguancun network security and Information Industry Alliance dialogue, wechat entrepreneur Haitai Fangyuan, chairman Mr. Jiang Haizhou
- The delivery efficiency is increased by 52 times, and the operation efficiency is increased by 10 times. See the compilation of practical cases of financial cloud native technology (with download)
- Excuse me, when using Flink SQL sink data to Kafka, the execution is successful, but there is no number in Kafka
- PHP中用下划线开头的变量含义
- Laravel5 call to undefined function OpenSSL cipher IV length() error php7 failed to open OpenSSL extension
- libSGM的horizontal_path_aggregation程序解读
- Wired network IP address of VMware shared host
- What are the principles for distinguishing the security objectives and implementation methods that cloud computing security expansion requires to focus on?
猜你喜欢

Redis 核心数据结构 & Redis 6 新特性详

Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf

2022-7-6 Leetcode27. Remove the element - I haven't done the problem for a long time. It's such an embarrassing day for double pointers

Horizontal of libsgm_ path_ Interpretation of aggregation program

Selenium库

【立体匹配论文阅读】【三】INTS

The delivery efficiency is increased by 52 times, and the operation efficiency is increased by 10 times. See the compilation of practical cases of financial cloud native technology (with download)
![[Reading stereo matching papers] [III] ints](/img/d3/4238432492ac3dc4ec14a971b8848d.png)
[Reading stereo matching papers] [III] ints

libSGM的horizontal_path_aggregation程序解读
![[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?](/img/fb/17e029b1d955965d7e2e0f58701d91.png)
[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
随机推荐
Laravel5 call to undefined function openssl cipher iv length() 报错 PHP7开启OpenSSL扩展失败
What are the principles for distinguishing the security objectives and implementation methods that cloud computing security expansion requires to focus on?
Use day JS let time (displayed as minutes, hours, days, months, and so on)
Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]
Help tenants
2022-7-6 Leetcode27. Remove the element - I haven't done the problem for a long time. It's such an embarrassing day for double pointers
Leetcode simple question sharing (20)
CSMA/CD 载波监听多点接入/碰撞检测协议
566. Reshaping the matrix
VSCode 配置使用 PyLint 语法检查器
call undefined function openssl_cipher_iv_length
Redis 核心数据结构 & Redis 6 新特性详
【网络安全】sql注入语法汇总
Battle Atlas: 12 scenarios detailing the requirements for container safety construction
How can the PC page call QQ for online chat?
一个简单LEGv8处理器的Verilog实现【四】【单周期实现基础知识及模块设计讲解】
搜索框效果的实现【每日一题】
Mathématiques avancées - - chapitre 8 différenciation des fonctions multivariables 1
The reason why data truncated for column 'xxx' at row 1 appears in the MySQL import file
Learning breakout 2 - about effective learning methods