题意:给出一个n个数的排列,每次可以把一个数放到最前面或者最后面的位置,问至少要进行多少次操作才能使得数列升序。
思路:考虑不被移动的那些数,当把所有一定的数去掉以后,这些剩下的数一定是一段数值连续,位置递增的数。如果想要移动的数最少,俺么这串递增的数就尽可能长。
dp[a[i]] = dp[a[i]-1] + 1
那么ans = n – max{dp[i]}
另外:对于要移动的数,我们可以按照一定顺序(放在前面的数按照递减顺序,放在最长序列后面的数按照递增顺序)移动,可以保证每个数只需要移动一次。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
/* *********************************************** Author :111qqz Created Time :2016年10月04日 星期二 20时10分00秒 File Name :code/cf/problem/605A.cpp ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <deque> #include <map> #include <string> #include <cmath> #include <cstdlib> #include <bitset> #define fst first #define sec second #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define ms(a,x) memset(a,x,sizeof(a)) typedef long long LL; #define pi pair < int ,int > #define MP make_pair using namespace std; const double eps = 1E-8; const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; const int inf = 0x3f3f3f3f; const int N=1E5+7; int n; int a[N]; int dp[N]; int main() { #ifndef ONLINE_JUDGE freopen("code/in.txt","r",stdin); #endif cin>>n; for ( int i = 1 ; i <= n ; i++) cin>>a[i]; ms(dp,0); int mx = 0 ; for ( int i = 1 ; i <= n ; i++) { dp[a[i]] = dp[a[i]-1] + 1; mx = max(dp[a[i]],mx); } cout<<n-mx<<endl; #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; } |
说点什么
您将是第一位评论人!