51nod-1700 首尾排序法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/C_13579/article/details/88547848

地址:http://www.51nod.com/Challenge/Problem.html#!#problemId=1700

思路:对于数组中的数a[i]都可以移到首尾去,只是移动的次序不同而使数组有序,因此只要找到一个最长的不移动的子序列即可,例如 [3 1 2 4 5]中为[3 4 5]最长,那么其可以不变,这样改变的个数就为最少的

Code:

#include<iostream>
using namespace std;

const int MAX_N=1e5+5; 
int n;
int d[MAX_N];

//最长连续子序列 
int main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	int x,ans=0;
	for(int i=0;i<n;++i)
	{
		cin>>x;
		d[x]=d[x-1]+1;
		ans=max(ans,d[x]);
	}
	ans=n-ans;
	cout<<ans<<endl;
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/C_13579/article/details/88547848