51nod 1241:特殊的排序

51nod 1241:特殊的排序

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1241

题目大意:给出$n$个数($1 leqslant a_i leqslant n$),现在要对这个数组进行排序,在排序时只能将元素放在数组的头部或尾部,问至少需要移动多少个数字,才能完成整个排序过程?

DP

显然最少操作数等于$n-|$最长连续子序列$|$.

故定义状态:$dp[i]$表示以$i$结尾的连续子序列长度.

转移方程:$dp[i]=dp[i-1]+1$.

代码如下:

 1 #include <iostream>
 2 #define N 50005
 3 using namespace std;
 4 int n,t,dp[N],ans;
 5 int main(void){
 6     std::ios::sync_with_stdio(false);
 7     cin>>n;
 8     for(int i=0;i<n;++i){
 9         cin>>t;
10         dp[t]=dp[t-1]+1;
11         ans=max(ans,dp[t]);
12     }
13     cout<<n-ans;
14 }
原文地址:https://www.cnblogs.com/barrier/p/6722247.html