当前位置:网站首页>Longest ascending subsequence model acwing 482. Chorus formation
Longest ascending subsequence model acwing 482. Chorus formation
2022-07-27 11: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 
边栏推荐
- 学习笔记-微信支付
- Using skills of word
- Use kaggle to run Li Hongyi's machine learning homework
- 涌现与形态的局部差异和整体差异
- Research on synaesthesia integration and its challenges
- Substr and substring function usage in SQL
- Taishan Office Technology Lecture: scaling and opening files
- Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.
- Description and feelings
- Ten year structure five year life-07 young and vigorous transformation
猜你喜欢

Iptables prevent nmap scanning and binlog explanation

Local and overall differences between emergence and morphology

IMG SRC is empty or SRC does not exist, and the picture has a white border

XXX packages are looking for funding run 'NPM fund' for details solutions

Redis+caffeine two-level cache enables smooth access speed

Views, triggers and stored procedures in MySQL

What is the mystery of the gate of the meta universe?

如何创建一个带诊断工具的.NET镜像

Wenzhou University X kangaroo cloud: how to "know well" in the construction of higher talent education

MySQL installation (RPM package)
随机推荐
Derivation of the detailed expansion sto overlap integrals
The largest square of leetcode (violence solving and dynamic programming solving)
Introduction to software vulnerability analysis (I)
antd中table hover上去的背景色样式修改
Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.
The open source project Taier version 1.1 was officially released, and the list of new functions is fast
Application scenarios, key technologies and network architecture of communication perception integration
如何组装一个注册中心
Overview of radar communication integrated waveform design
2022牛客多校训练(3)A-Ancestor 题目翻译
Verilog implementation of ECG signal acquisition, storage and transmission system based on FPGA
【FPGA教程案例40】通信案例10——基于FPGA的简易OFDM系统verilog实现
14 check whether integers and their multiples exist
KEPServer配置
Symmetric encryption and asymmetric encryption
tensorflow运行报错解决方法
antd table+checkbox 默认值显示
4 search insertion location
洛谷P1441 砝码称重
学习笔记-uni-app