cf13c Sequence (DP)

Source: Internet
Author: User

Test instructions

N number. A1...an.

For each number, each step can only be added one or minus one.

Ask at least how many steps it takes to make the new sequence a non-descending sequence.

n (1≤ n ≤5000)

Ideas:

* One does not know how to prove the conclusion (to be proved): The last new sequence b1...bn each number of BI, must be the original A1. A number in the sequence of an.

Will A1. An from small to large arranged, get c1...cn.

DP[I][J]: The first sequence before I number of operations, the number of I do not exceed c[j] The minimum number of steps spent.

Dp[i][j]=min (Dp[i-1][j]+abs (A[i]-b[j]), dp[i][j-1])

Then use the scrolling array.

Code:

int Constn=5005;intn;ll a[n],b[n];ll dp[n];intMain () {CIN>>N; Rep (I,1, N) {scanf ("%i64d",&A[i]); B[i]=A[i]; } mem (DP,0); dp[0]=INF; Sort (b+1, B +1+N); Rep (I,1, N) {//First iRep (J,1, N) {//the height of the last height not exceeding b[j]Dp[j]=min (Dp[j]+abs (A[i]-b[j]), dp[j-1] ); }} ll ans=INF; Rep (I,1, N) {ans=min (ans,dp[i]); } printf ("%i64d\n", ans); return 0;}

cf13c Sequence (DP)

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.