51nod 1241: Special sort
Title Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1241
Title: Give the number of $n$ ($ \leqslant a_i \leqslant n$), now to sort the array, you can only place the element at the beginning or end of the array when sorting, ask at least how many numbers to move to complete the sorting process?
Dp
The least number of operands is obviously equal to $n-|$ longest continuous subsequence $|$.
So define the state: $DP [i]$ represents the length of a continuous subsequence ending with $i$.
Transfer equation: $DP [i]=dp[i-1]+1$.
The code is as follows:
1#include <iostream>2 #defineN 500053 using namespacestd;4 intN,t,dp[n],ans;5 intMainvoid){6Std::ios::sync_with_stdio (false);7Cin>>N;8 for(intI=0; i<n;++i) {9Cin>>T;Tendp[t]=dp[t-1]+1; Oneans=Max (ans,dp[t]); A } -cout<<n-ans; -}
51nod 1241: Special sort