1. 程式人生 > >poj 3666Making the Grade

poj 3666Making the Grade

Description

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

|A1 - B1| + |A2 - B2| + ... + |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

Source

dp+離散化

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdio>
#define inf 0x3f3f3f3f
#define maxn 2010
#define ll long long
using namespace std;
ll a[maxn];
ll b[maxn];
ll dp[maxn][maxn];
ll n;

bool cmp(ll a,ll b)
{
    return a>b;
}

ll solveup()
{sort(b,b+n);

    for(int k=0;k<n;k++)
    dp[0][k]=abs(a[0]-b[k]);
    ll v=0;
    for(int i=1;i<n;i++)
    {
        v=inf;
        for(int j=0;j<n;j++)
        {
            v=min(v,dp[i-1][j]);
            dp[i][j]=v+abs(a[i]-b[j]);
        }
    }
    v=inf;
    for(int i=0;i<n;i++)
    v=min(v,dp[n-1][i]);
    return v;
}
ll solvedown()
{
    sort(b,b+n,cmp);
    for(int k=0;k<n;k++)
    dp[0][k]=abs(a[0]-b[k]);
    ll v=0;
    for(int i=1;i<n;i++)
    {
        v=inf;
        for(int j=0;j<n;j++)
        {
            v=min(v,dp[i-1][j]);
            dp[i][j]=v+abs(a[i]-b[j]);
        }
    }
    v=inf;
    for(int i=0;i<n;i++)
    v=min(dp[n-1][i],v);
    return v;
}
int main()
{
    scanf("%lld",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%lld",&a[i]);
        b[i]=a[i];
    }
    printf("%lld\n",min(solveup(),solvedown()));
    return 0;
}

滾動陣列降維

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdio>
#define inf 0x3f3f3f3f
#define maxn 2010
#define ll long long
using namespace std;
ll a[maxn];
ll b[maxn];
ll dp[maxn];
ll n;

bool cmp(ll a,ll b)
{
    return a>b;
}

ll solveup()
{sort(b,b+n);

    for(int k=0;k<n;k++)
    dp[k]=abs(a[0]-b[k]);
    ll v=0;
    for(int i=1;i<n;i++)
    {
        v=inf;
        for(int j=0;j<n;j++)
        {
            v=min(v,dp[j]);
            dp[j]=v+abs(a[i]-b[j]);
        }
    }
    v=inf;
    for(int i=0;i<n;i++)
    v=min(v,dp[i]);
    return v;
}
ll solvedown()
{
    sort(b,b+n,cmp);
    for(int k=0;k<n;k++)
    dp[k]=abs(a[0]-b[k]);
    ll v=0;
    for(int i=1;i<n;i++)
    {
        v=inf;
        for(int j=0;j<n;j++)
        {
            v=min(v,dp[j]);
            dp[j]=v+abs(a[i]-b[j]);
        }
    }
    v=inf;
    for(int i=0;i<n;i++)
    v=min(dp[i],v);
    return v;
}
int main()
{
    scanf("%lld",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%lld",&a[i]);
        b[i]=a[i];
    }
    printf("%lld\n",min(solveup(),solvedown()));
    return 0;
}