POJ3666(dp)

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


先考虑一个贪心:存在一种使答案最小的方案,使得 B B 数组中出现过的数在A中仍出现过
这是显然,也是可以证明的
那我们只要把 A A 数组排一下序(升序降序各做一次),拍完序的数组为C数组
再设状态 fi,j f i , j 表示当前考虑第 i i 位,C数组中放第 j j 个答案的最小值
那么fi,j=min1<=k<=j{ fi1,k f i − 1 , k } +|aibj| + | a i − b j |
min m i n 里面的部分我们可以仿照 LCIS L C I S 的处理方法,枚举 j j 的时候维护一个极值
显然dp的第一位是可以省略的

吐槽:usaco数据真水,不降序做都能A

#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i = j;i <= k;++i)
#define repp(i,j,k) for(int i = j;i >= k;--i)
#define rept(i,x) for(int i = linkk[x];i;i = e[i].n)
#define P pair<int,int>
#define Pil pair<int,ll>
#define Pli pair<ll,int>
#define Pll pair<ll,ll>
#define pb push_back 
#define pc putchar
#define mp make_pair
#define file(k) memset(k,0,sizeof(k))
#define ll long long
namespace fastIO{
    #define BUF_SIZE 100000
    #define OUT_SIZE 100000
    bool IOerror = 0;
    inline char nc(){
        static char buf[BUF_SIZE],*p1 = buf+BUF_SIZE, *pend = buf+BUF_SIZE;
        if(p1 == pend){
            p1 = buf; pend = buf+fread(buf, 1, BUF_SIZE, stdin);
            if(pend == p1){ IOerror = 1; return -1;}
        }
        return *p1++;
    }
    inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
    inline void read(int &x){
        bool sign = 0; char ch = nc(); x = 0;
        for(; blank(ch); ch = nc());
        if(IOerror)return;
        if(ch == '-') sign = 1, ch = nc();
        for(; ch >= '0' && ch <= '9'; ch = nc()) x = x*10+ch-'0';
        if(sign) x = -x;
    }
    inline void read(ll &x){
        bool sign = 0; char ch = nc(); x = 0;
        for(; blank(ch); ch = nc());
        if(IOerror) return;
        if(ch == '-') sign = 1, ch = nc();
        for(; ch >= '0' && ch <= '9'; ch = nc()) x = x*10+ch-'0';
        if(sign) x = -x;
    }
    #undef OUT_SIZE
    #undef BUF_SIZE
};
using namespace fastIO;
int n;
ll ans;
int a[2010];
int b[2010];
ll f[2010];
int main()
{
    read(n);
    rep(i,1,n) read(a[i]),b[i] = a[i];
    sort(b+1,b+n+1);
    memset(f,20,sizeof(f));ans = f[0];
    f[1] = 0;
    rep(i,1,n)
    {
        ll val = f[1];
        rep(j,1,n)
        {
            val = min(val,f[j]);
            f[j] = val + abs(a[i]-b[j]);
        }
    }
    rep(i,1,n) ans = min(ans,f[i]);
    memset(f,20,sizeof(f));
    f[n] = 0;
    rep(i,1,n)
    {
        ll val = f[n];
        repp(j,n,1)
        {
            val = min(val,f[j]);
            f[j] = val + abs(a[i]-b[j]);
        }
    } 
    rep(i,1,n) ans = min(ans,f[i]);
    printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值