The fifth D-DropVoicing of the 2020 Summer Niuke Multi-school

The title here is the
meaning of the title: Given you have n numbers in a sequence, there are two more operations.
Operation 1: Put the penultimate number of the current sequence to the first place, and the remaining positions remain unchanged.
Operation 2: Put the current sequence of The first number is placed on the last digit, and the remaining positions remain unchanged.
Several consecutive operations 2 are regarded as operations that cost a unit cost. Operation 1 does not cost any cost.
Q: Change the originally given sequence to a 1 to n How much does an increasing sequence cost?

Insert picture description here
Put this sequence into a ring, operation one can be seen as moving a number counterclockwise by n-1 positions
, operation two is equivalent to rotating the current ring, combining these two operations, we can move Arbitrary element to any position, in this case, only the longest subsequence of the sequence starting from the point on each ring is not enumerated, and the rest is the number of elements that we need to adjust, so that we can get the answer of
500. Both O(N 3 ) and O(N 2 logN) can be used to obtain LIS

Code:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MAXN = 1e3+7;
int a[MAXN],dp[MAXN];

int main()
{
    
    
	int n;
	scanf("%d",&n);
	for(int i = 1;i <= n;i ++){
    
    
		scanf("%d",&a[i]);
		a[i+n] = a[i];//让自身变成一个环 便于理解和操作
	}
	int ans = 0;
	for(int i = 1;i <= n;i ++){
    
    
		memset(dp,0,sizeof(dp));
		for(int j = i;j <= i + n - 1;j ++){
    
    
			dp[j] = 1;
			for(int k = i;k < j;k ++){
    
    
				if(a[k] < a[j]){
    
    
					dp[j] = max(dp[j],dp[k]+1);
				}
			}
			ans = max(ans,dp[j]);
		}
	}
	printf("%d\n",n-ans);
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45672411/article/details/107672603