当前位置:网站首页>The longest ascending subsequence model acwing 482 Chorus formation
The longest ascending subsequence model acwing 482 Chorus formation
2022-07-07 14:13:00 【T_ Y_ F666】
Longest ascending subsequence model AcWing 482. Chorus formation
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 , Get to i It is a sequence in which the dividing point rises first and then falls , Taking the maximum , Reduce the total by 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", n-ans);
}
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", n - res);
return 0;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- [network security] SQL injection syntax summary
- AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
- Oracle advanced (V) schema solution
- Excusez - moi, l'exécution a été réussie lors de l'utilisation des données de puits SQL Flink à Kafka, mais il n'y a pas de nombre dans Kafka
- 566. Reshaping the matrix
- call undefined function openssl_ cipher_ iv_ length
- SAKT方法部分介绍
- 请问指南针股票软件可靠吗?交易股票安全吗?
- Arm cortex-a9, mcimx6u7cvm08ad processor application
- Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
猜你喜欢

AI talent cultivation new ideas, this live broadcast has what you care about

js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点

libSGM的horizontal_path_aggregation程序解读

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
![[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?

TPG x AIDU | AI leading talent recruitment plan in progress!

AI人才培育新思路,这场直播有你关心的

Did login metamask

Best practice | using Tencent cloud AI willingness to audit as the escort of telephone compliance

Parsing of XML files
随机推荐
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]
接口自动化测试-接口间数据依赖问题解决
Beginner XML
高等数学---第八章多元函数微分学1
[untitled]
Excuse me, as shown in the figure, the python cloud function prompt uses the pymysql module. What's the matter?
GVIM [III] [u vimrc configuration]
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
Horizontal of libsgm_ path_ Interpretation of aggregation program
XML文件的解析操作
Excuse me, when using Flink SQL sink data to Kafka, the execution is successful, but there is no number in Kafka
Clickhouse (03) how to install and deploy Clickhouse
Leecode3. Longest substring without repeated characters
Vscode configuration uses pylint syntax checker
内存溢出和内存泄漏的区别
Es log error appreciation -limit of total fields
PostgreSQL array type, each splice
Huawei image address
Redis can only cache? Too out!