51nod 1241: Special sort

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.