Poj 3666 Making 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

题意:给你一个序列A,将其转化为序列B,满足:

  1. B非严格单调递增或递减
  2. Ai - Bi的和最小

求最小值

这个题数据讲道理很水,我用了两种方法写,都是只需要考虑递增情况就能过了,完全不需要考虑递减的情况。。。



贪心

第一次做到这个题是在dp的训练赛里,当时不会做,90n找了一个单调队列的做法

只考虑递增可过

#include<iostream>
#include<queue>
#define ll long long
using namespace std;
ll a[2005];
ll dp[2005][2];
int main()
{
    ios::sync_with_stdio(false);
    int n;
    while(cin>>n){
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        ll ans1=0;
        priority_queue<ll> Q;
        for(int i=1;i<=n;i++){
            Q.push(a[i]);
            if(Q.top()>a[i]){
                ans1+=Q.top()-a[i];
                Q.pop();
                Q.push(a[i]);
            }
        }
        cout<<ans1<<endl;
    }
}

正解应该同时考虑递减

#include<iostream>
#include<queue>
#define ll long long
using namespace std;
ll a[2005];
ll dp[2005][2];
int main()
{
    ios::sync_with_stdio(false);
    int n;
    while(cin>>n){
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        ll ans1=0;
        priority_queue<ll> Q;
        for(int i=1;i<=n;i++){
            Q.push(a[i]);
            if(Q.top()>a[i]){
                ans1+=Q.top()-a[i];
                Q.pop();
                Q.push(a[i]);
            }
        }
        while(!Q.empty())Q.pop();
        ll ans2=0;
        for(int i=n;i>=1;i--){
            Q.push(a[i]);
            if(Q.top()>a[i]){
                ans2+=Q.top()-a[i];
                Q.pop();
                Q.push(a[i]);
            }
        }
        cout<<min(ans1,ans2)<<endl;
    }
}



dp

dp按着蓝书的思路写的,同样是只考虑递增可过。。。

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<ctime>
#include<bitset>
#define ll long long
#define ull unsigned long long
using namespace std;
const ll N=1001,M=1e9+7;
const ull base=13331;
const double Pi=acos(-1.0);
const ll C=299792458;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
ll a[2005];
ll b[2005];
int t,n;
void discrete(){
    sort(b+1,b+n+1);
    t=unique(b+1,b+n+1)-(b+1);
}
int id(int x){
    return lower_bound(b+1,b+t+1,x)-b;
}
ll dp[2005][2005];
int main(){
    ios::sync_with_stdio(false);
    while(cin>>n){
        for(int i=1;i<=n;i++){
            cin>>a[i];
            b[i]=a[i];
        }
        dp[0][0]=0;
        discrete();
        for(int i=1;i<=n;i++){
            ll minn=dp[i-1][1];
            for(int j=1;j<=t;j++){
                int k=id(b[j]);
                minn=min(minn,dp[i-1][k]);
                if(a[i]>b[j])dp[i][k]=minn+a[i]-b[j];
                else dp[i][k]=minn+b[j]-a[i];
            }
        }
        ll ans=0x3f3f3f3f3f3f3f3f;
        for(int i=1;i<=n;i++){
            ans=min(ans,dp[n][id(a[i])]);
        }
        cout<<ans<<endl;
    }
}

正解同时考虑递减,不过因为离散化的原因,一开始被卡了时间,所以id最好先求出来

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<ctime>
#include<bitset>
#define ll long long
#define ull unsigned long long
using namespace std;
const ll N=1001,M=1e9+7;
const ull base=13331;
const double Pi=acos(-1.0);
const ll C=299792458;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
ll a[2005];
ll b[2005];
int t,n;
void discrete(){
    sort(b+1,b+n+1);
    t=unique(b+1,b+n+1)-(b+1);
}
int id(int x){
    return lower_bound(b+1,b+t+1,x)-b;
}
ll dp[2005][2005];
int id1[2005],id2[2005];
int main(){
    ios::sync_with_stdio(false);
    while(cin>>n){
        for(int i=1;i<=n;i++){
            cin>>a[i];
            b[i]=a[i];
        }
        dp[0][0]=0;
        discrete();
        for(int i=1;i<=n;i++)id1[i]=id(a[i]);
        for(int j=1;j<=t;j++)id2[j]=id(b[j]);
        for(int i=1;i<=n;i++){
            ll minn=dp[i-1][1];
            for(int j=1;j<=t;j++){
                int k=id2[j];
                minn=min(minn,dp[i-1][k]);
                if(a[i]>b[j])dp[i][k]=minn+a[i]-b[j];
                else dp[i][k]=minn+b[j]-a[i];
            }
        }
        ll ans=0x3f3f3f3f3f3f3f3f;
        for(int i=1;i<=n;i++){
            ans=min(ans,dp[n][id1[i]]);
        }
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++){
            ll minn=dp[i-1][id2[t]];
            for(int j=t;j>=1;j--){
                int k=id2[j];
                minn=min(minn,dp[i-1][k]);
                if(a[i]>b[j])dp[i][k]=minn+a[i]-b[j];
                else dp[i][k]=minn+b[j]-a[i];
            }
        }
        for(int i=1;i<=n;i++){
            ans=min(ans,dp[n][id1[i]]);
        }
        cout<<ans<<endl;
    }
}