[最长上升子序列]2020牛客多校第五场 D-Drop Voicing

题目

在这里插入图片描述
题目链接:https://ac.nowcoder.com/acm/contest/5670/D

思路

先把p变成一个环形序列,画图可发现 两个操作合在一起就是把序列里面任意一个数换从其他地方插入 我们只需要求出最多有多少个不用进行变换
所以将1-n分别作为开头 计算后n个里面的最长上升子序列 找出最大的结果 n-maxn即可AC

代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<ctime>
#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<iomanip>
#include<list>
#include<bitset>
#include<sstream>
#include<fstream>
#include<complex>
#include<algorithm>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#define ll long long
#define int long long
using namespace std;
const int INF = 0x3f3f3f3f;
int a[1010],dp[1010]; 
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=n+1;i<=2*n;i++) a[i]=a[i-n];
	int maxn=0;
	for(int i=1;i<=n;i++){
		memset(dp,0,sizeof dp);
		for(int j=i;j<=i+n;j++){
		//	dp[j]=1;
			for(int k=i;k<=j-1;k++){
				if(a[k]<a[j]){
					dp[j]=max(dp[j],dp[k]);
				}
			}
		dp[j]+=1;
		maxn=max(maxn,dp[j]);
		}
	}
	cout<<n-maxn<<endl;
    return 0;
}


猜你喜欢

转载自blog.csdn.net/kosf_/article/details/107690545