1. 程式人生 > >Making the Grade POJ - 3666

Making the Grade POJ - 3666

ssi ner long long end ive lines contains pan print

A straight dirt road connects two fields on FJ‘s farm, but it changes elevation more than FJ would like. His cows do not mind climbing up or down a single slope, but they are not fond of an alternating succession of hills and valleys. FJ would like to add and remove dirt from the road so that it becomes one monotonic slope (either sloping up or down).

You are given N integers A1, ... , AN (1 ≤ N ≤ 2,000) describing the elevation (0 ≤ Ai ≤ 1,000,000,000) at each of N equally-spaced positions along the road, starting at the first field and ending at the other. FJ would like to adjust these elevations to a new sequence B1, . ... , BN that is either nonincreasing or nondecreasing. Since it costs the same amount of money to add or remove dirt at any position along the road, the total cost of modifying the road is

| A 1 - B 1| + | A 2 - B 2| + ... + | AN - BN |

Please compute the minimum cost of grading his road so it becomes a continuous slope. FJ happily informs you that signed 32-bit integers can certainly be used to compute the answer.

Input

* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains a single integer elevation: Ai

Output

* Line 1: A single integer that is the minimum cost for FJ to grade his dirt road so it becomes nonincreasing or nondecreasing in elevation.

Sample Input
7
1
3
2
4
5
3
9
Sample Output
3


題解:
  這個題目我們先考慮暴力,dp[i][j]表示dp到i這一位,最後一個數是j的最小花費,那麽狀態數是n*(l~r),顯然是不行的。
  考慮一個小優化,因為只有數j在序列中出現過才會有用,所以第二維可以改為第j大的數,這樣子狀態數就是n^2級別的了,轉移是(上升)dp[i][j]=min(dp[i-1][1~j])+abs(hi[i]-hi[j)。
  把這個式子列出來就知道怎麽優化轉移了,計D[i][j]=min(dp[i][1~j]),那麽轉移就是D[i][j]=min*+(D[i][j-1],dp[i][j]),轉移就變成O(n)的了。
代碼:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#define MAXN 2020
#define inf 0x3f3f3f3f3f
#define ll long long
using namespace std;
ll dp[MAXN][MAXN],last[MAXN],hi[MAXN],rak[MAXN],D[MAXN][MAXN],ans=inf;
int n;

ll abss(ll x){
    if(x<0) return -x;
    return x;
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%lld",&hi[i]),rak[i]=hi[i];
    sort(rak+1,rak+n+1);
    memset(D,inf,sizeof(D));
    memset(dp,inf,sizeof(dp));
    for(int i=0;i<=n;i++) dp[0][i]=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            if(i==1) dp[i][j]=abss(hi[i]-rak[j]);
            else dp[i][j]=D[i-1][j]+abss(hi[i]-rak[j]);
            D[i][j]=min(D[i][j-1],dp[i][j]);
        }
    for(int i=1;i<=n;i++) ans=min(ans,dp[n][i]);
    memset(dp,inf,sizeof(dp));
    memset(D,inf,sizeof(D));
    for(int i=0;i<=n;i++) dp[0][i]=0;
    for(int i=1;i<=n;i++)
        for(int j=n;j>=1;j--){
            if(i==1) dp[i][j]=abss(hi[i]-rak[j]);
            else dp[i][j]=D[i-1][j]+abss(hi[i]-rak[j]);
            D[i][j]=min(D[i][j+1],dp[i][j]);
        }
    for(int i=1;i<=n;i++) ans=min(ans,dp[n][i]);
    printf("%lld\n",ans);
    return 0;
}

Making the Grade POJ - 3666