当前位置:网站首页>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 
边栏推荐
- What are the principles for distinguishing the security objectives and implementation methods that cloud computing security expansion requires to focus on?
- 118. Yanghui triangle
- [untitled]
- 手里的闲钱是炒股票还是买理财产品好?
- call undefined function openssl_cipher_iv_length
- Did login metamask
- 最长上升子序列模型 AcWing 1014. 登山
- [AI practice] Application xgboost Xgbregressor builds air quality prediction model (II)
- 使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )
- postgresql array类型,每一项拼接
猜你喜欢
![Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]](/img/d3/20674983717d829489149b4d3bfedf.png)
Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]

Reverse non return to zero code, Manchester code and differential Manchester code of common digital signal coding

How to check the ram and ROM usage of MCU through Keil

常用数字信号编码之反向不归零码码、曼彻斯特编码、差分曼彻斯特编码

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

Use day JS let time (displayed as minutes, hours, days, months, and so on)

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

2022-7-6 Leetcode 977. Square of ordered array

2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array

566. Reshaping the matrix
随机推荐
Common response status codes
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
FC连接数据库,一定要使用自定义域名才能在外面访问吗?
供应链供需预估-[时间序列]
XML文件的解析操作
Flask session forged hctf admin
IP address home location query
Is the compass stock software reliable? Is it safe to trade stocks?
Environment configuration of lavarel env
requires php ~7.1 -&gt; your PHP version (7.0.18) does not satisfy that requirement
postgresql array类型,每一项拼接
Laravel5 call to undefined function OpenSSL cipher IV length() error php7 failed to open OpenSSL extension
2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out
Redis can only cache? Too out!
[AI practice] Application xgboost Xgbregressor builds air quality prediction model (II)
Excuse me, why is it that there are no consumption messages in redis and they are all piled up in redis? Cerely is used.
请问,我kafka 3个分区,flinksql 任务中 写了 join操作,,我怎么单独给join
Dry goods | summarize the linkage use of those vulnerability tools
"New red flag Cup" desktop application creativity competition 2022
PERT图(工程网络图)