1. 程式人生 > >POJ 3666 Making the Grade (動態規劃)

POJ 3666 Making the Grade (動態規劃)

lower not rst pst 除了 最小值 動態規劃 total back

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

USACO 2008 February Gold 題意: 將所給數組中的某個數字加上或者減去某個數,使數組變為非降數組,問所需最小花費。 思路: 允許數組中的數字相等,那麽最後最優解不會出現除了輸入以外的數字,所以可以將輸入的數字離散化。 dp[i][j]表示,將第i個數字,變成第j大的數字所需的最小花費。j實際上就是離散化之後的數組的下標。 dp[i][j]=min(dp[i-1][k]+abs(num[i]-p[k]),dp[i][j]); 其中num是原高度,p是離散化後的數組。k<=j; 但是這樣的復雜度是n的三次方,不過還好我們可以用一個數組記錄下j之前dp[i-1][k]+abs(num[i]-p[k])的最小值,這樣就能優化成n方了。 TLE 技術分享圖片
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(x,i,j) cout<<#x<<"["<<i<<"]["<<j<<"] = "<<x[i][j]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100086;
const int inf = 2.1e9;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);
int num[2008],p[2008];
int n;
int dp[2008][2008];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&num[i]);
        p[i]=num[i];
    }
    sort(p+1,p+1+n);
    int m=unique(p+1,p+1+n)-p-1;
    for(int i=1;i<=n;i++){
        int t=lower_bound(p+1,p+1+n,num[i])-p-1;
        for(int j=1;j<=m;j++){
            dp[i][j]=inf;
            for(int k=1;k<=j;k++){
                dp[i][j]=min(dp[i-1][k]+abs(num[i]-p[k]),dp[i][j]);
            }
        }
    }
    printf("%d\n",dp[n][m]);
    return 0;
}
View Code AC 技術分享圖片
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(x,i,j) cout<<#x<<"["<<i<<"]["<<j<<"] = "<<x[i][j]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100086;
const int inf = 2.1e9;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);
int num[2008],p[2008];
int n;
int dp[2008][2008];
int minn[2008];
int main()
{
//    ios::sync_with_stdio(false);
//    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&num[i]);
        p[i]=num[i];
    }
    sort(p+1,p+1+n);
    int m=unique(p+1,p+1+n)-p-1;
    for(int i=1;i<=n;i++){
        int t=lower_bound(p+1,p+1+n,num[i])-p-1;
        minn[0]=inf;
        for(int j=1;j<=m;j++){
            minn[j]=min(minn[j-1],dp[i-1][j]+abs(num[i]-p[j]));
        }
        for(int j=1;j<=m;j++){
            dp[i][j]=minn[j];
        }
    }
    printf("%d\n",dp[n][m]);
    return 0;
}
View Code

POJ 3666 Making the Grade (動態規劃)