当前位置:网站首页>最长上升子序列模型 AcWing 482. 合唱队形
最长上升子序列模型 AcWing 482. 合唱队形
2022-07-07 12:08:00 【T_Y_F666】
最长上升子序列模型 AcWing 482. 合唱队形
原题链接
算法标签
DP 线性DP 最长上升子序列
思路
分别求以i结尾上升序列,以i结尾反向上升即下降序列,得到以i为分割点先上升后下降序列,取最大值, 将总数减最大值即可。
代码
#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;
// 以i结尾上升序列
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]);
}
// 以i结尾反向上升即下降序列
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]);
}
// 先上升 后下降序列
rep(i, 1, n+1){
ans=max(ans, (f[i]+f1[i]-1));
}
printf("%lld\n", n-ans);
}
与y总代码思路一致
y总代码
#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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- [fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
- 供应链供需预估-[时间序列]
- flask session伪造之hctf admin
- How can the PC page call QQ for online chat?
- The meaning of variables starting with underscores in PHP
- 2022-7-6 beginner redis (I) download, install and run redis under Linux
- 室内ROS机器人导航调试记录(膨胀半径的选取经验)
- 高等數學---第八章多元函數微分學1
- Es log error appreciation -limit of total fields
- js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
猜你喜欢
Build a secure and trusted computing platform based on Kunpeng's native security
Transferring files between VMware and host
Take you to master the three-tier architecture (recommended Collection)
SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
Help tenants
Leecode3. Longest substring without repeated characters
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
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
Wired network IP address of VMware shared host
得物客服热线的演进之路
随机推荐
Leecode3. Longest substring without repeated characters
[1] Basic knowledge of ros2 - summary version of operation commands
DID登陆-MetaMask
Redis 核心数据结构 & Redis 6 新特性详
2022-7-6 beginner redis (I) download, install and run redis under Linux
内存溢出和内存泄漏的区别
2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来
Custom thread pool rejection policy
手里的闲钱是炒股票还是买理财产品好?
call undefined function openssl_cipher_iv_length
现在网上开户安全么?那么网上开户选哪个证券公司?
SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
"Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
requires php ~7.1 -&gt; your PHP version (7.0.18) does not satisfy that requirement
室内ROS机器人导航调试记录(膨胀半径的选取经验)
請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
The meaning of variables starting with underscores in PHP
Dry goods | summarize the linkage use of those vulnerability tools
[high frequency interview questions] difficulty 2.5/5, simple combination of DFS trie template level application questions
requires php ~7.1 -&gt; your PHP version (7.0.18) does not satisfy that requirement