当前位置:网站首页>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
边栏推荐
- FCOS3D label assignment
- 手里的闲钱是炒股票还是买理财产品好?
- 【AI实战】应用xgboost.XGBRegressor搭建空气质量预测模型(二)
- 2022-7-7 Leetcode 844. Compare strings with backspace
- Did login metamask
- 带你掌握三层架构(建议收藏)
- 请问,我kafka 3个分区,flinksql 任务中 写了 join操作,,我怎么单独给join
- call undefined function openssl_cipher_iv_length
- Dry goods | summarize the linkage use of those vulnerability tools
- WPF DataGrid realizes the UI interface to respond to a data change in a single line
猜你喜欢
Battle Atlas: 12 scenarios detailing the requirements for container safety construction
Leetcode simple question sharing (20)
Redis 核心数据结构 & Redis 6 新特性详
Details of redis core data structure & new features of redis 6
SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
XML文件的解析操作
Did login metamask
VSCode 配置使用 PyLint 语法检查器
Data flow diagram, data dictionary
"Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
随机推荐
2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array
First choice for stock account opening, lowest Commission for stock trading account opening, is online account opening safe
Excuse me, when using Flink SQL sink data to Kafka, the execution is successful, but there is no number in Kafka
TPG x AIDU | AI leading talent recruitment plan in progress!
The reason why data truncated for column 'xxx' at row 1 appears in the MySQL import file
手把手教会:XML建模
Assign a dynamic value to the background color of DataGrid through ivalueconverter
Evolution of customer service hotline of dewu
Vmware共享主机的有线网络IP地址
postgresql array类型,每一项拼接
Huawei image address
libSGM的horizontal_path_aggregation程序解读
2022-7-6 beginner redis (I) download, install and run redis under Linux
PostgreSQL array type, each splice
Help tenants
3D detection: fast visualization of 3D box and point cloud
AI人才培育新思路,这场直播有你关心的
Parameter keywords final, flags, internal, mapping keywords internal
Parsing of XML files
PHP中用下划线开头的变量含义