Codeforces 13C. Sequence scrolling array + discretization

Source: Internet
Author: User

Click to open link
C. Sequencetime limit per test1 secondmemory limit per test64 megabytesinputstandard inputoutputstandard output

Little Petya likes to play very much. And most of all he likes to play the following game:

He is given a sequence of  N  integer numbers. At each step it was allowed to increase the value of any number By 

The Sequence  a  is called non-decreasing if  a 1 ? ≤? a 2 ? ≤?...? ≤? a N  holds, Where 

Input

The first line of the input contains a single integer N (1?≤? N. ≤?5000)-the length of the initial sequence. The following N lines contain one integer each-elements of the sequence. These numbers do not exceed 9 by absolute value.

Output

Output one integer-minimum number of steps required to achieve the goal.

Sample Test (s) input
53 2-1 2 11
Output
4
Input
52 1 1) 1 1
Output
1

Give you n number, each operation can make a number plus 1 or minus 1, ask at least how many operations make this n number is non-decrement. Non-descending definition:

a 1 ? ≤? a 2 ? ≤?...? ≤? a N

The minimum number of requests must be made up of the original number.

DP[I][J] Represents the number of previous I to a[j] as the end of the minimum operation to meet the requirements, but the maximum number of topics given is 10^9, two-dimensional array of J elements can not open so large, so need to be discretized, changed to the number of the first I with the number of J as the end of the minimum operation to meet the requirements.

Dp[i][j]=min (Dp[i][j], the smallest operation of the first J number of the i-1 position +fabs (B[j]-a[i]) b array is the original input of the array of a well-ordered and discretized

Since B arrays are ordered from small to large, the number of J in the first position must be greater than the number of i-1 in the first position.

Finally dp[5000][5000] can not be saved, then this two-dimensional array will have to roll.

780 MS 0 kb#include<stdio.h> #include <math.h> #include <string.h> #include <algorithm># Define ll __int64using namespace Std;const ll inf = 1ll<<62;//inf to take great ll Dp[2][5007],a[5007],b[5007];ll min (ll x,ll y ) {return x<y?x:y;}    int main () {ll n;            while (scanf ("%i64d", &n)!=eof) {for (int i=1;i<=n;i++) {scanf ("%i64d", &a[i]);        B[i]=a[i];        } sort (b+1,b+n+1);        int Tot=unique (b+1,b+n+1)-b-1;//discretization Dp[1][1]=inf;            for (int i=1;i<=tot;i++) {dp[1&1][i]=fabs (b[i]-a[1]);        dp[2&1][i]=inf;             } for (int i=2;i<=n;i++) {ll minn=inf;                 for (int j=1;j<=tot;j++) {minn=min (minn,dp[(i-1) &1][j]);//Take the minimum in a tree with a different height from the previous position             Dp[i&1][j]=min (Dp[i&1][j],minn+fabs (B[j]-a[i]));//The current position equals the first J tree in the previous position spend minimum + this position is the cost of the J tree} for (int j=1;j<=tot;j++) dp[(i+1) &1][j]=inf;        } ll Ans=inf;    for (int i=1;i<=tot;i++) ans=min (Ans,dp[n&1][i]);//Take the last position is the first J tree is the minimum cost of printf ("%i64d\n", ans); } return 0;}


Codeforces 13C. Sequence scrolling array + discretization

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.