zoukankan      html  css  js  c++  java
  • 【DP】+【贪心】【前缀和】洛谷P2893 [USACO08FEB]修路Making the Grade 题解

        正常的没想到的DP和玄学贪心。

    题目描述

    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 (A_1, ... , A_N (1 le N le 2,000)) describing the elevation ( (0 le A_i le 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 (B_1, ... , B_N) 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|+...+|A_N-B_N|)

    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.

    农夫约翰想改造一条路,原来的路的每一段海拔是(A_i),修理后是(B_i),花费(|A_i – B_i|)。我们要求修好的路是单调不升或者单调不降的。求最小花费。

    输入输出格式

    输入格式:

    * Line 1: A single integer: (N)

    * Lines 2..N+1: Line i+1 contains a single integer elevation: (A_i)

    输出格式:

    * 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.

    输入输出样例

    输入样例#1:
    7
    1
    3
    2
    4
    5
    3
    9
    
    输出样例#1:
    3

    题解 of DP:

        首先这个题有一个比较正常的DP做法,但是长时间的思维定式会让人不敢朝这里想。

        看到(2,000)的数据范围,可以联想到的复杂度是(O(n^2),O(n^2log n))等,因此考虑DP或递推。状态如果从前面所有段转移过来,感觉不太现实,而且不容易处理。不过我们手玩发现,把一段路的高度改为没有修改过的是最优的一种方案。因此把一开始的数据离散化,得到不超过(n)个数。

        考虑DP,f[i][j]表示到第i段路满足不下降(只做不下降,不上升待会再做一遍),且当前高度为j所需要的最小花费。它的转移实际上是(minlimits_{kin[1,j]}{f[i-1][k]}+|h[i]-ori[j]|),ori[j]是j对应的离散化之前的数,h[i]是i原来的高度,因为不下降,所以只能从([1,j])转移。不过这样的复杂度是(O(n^3))的,我们发现,随着(j)的增大,(k)的取值范围只是上界变大了,因此可以通过更新前缀和来维护(minn[i]=minlimits_{j=[1,i]}{ f[j]})。

        同时,因为循环(1-n)在最外层,所以那层可以滚掉,同时f数组就可以直接更新前缀和,因此也不用额外开一个数组了。

    Code of DP:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    long long Abs(long long x)
    {
        return x>0?x:(-x);
    }
    long long Min(long long x,long long y)
    {
        return x<y?x:y;
    }
    struct node
    {
        long long a,b,i;
        friend bool operator <(node x,node y)
        {
            return x.i<y.i;
        }
    }a[2001];
    bool cmp(node x,node y)
    {
        return x.a<y.a;
    }
    long long ori[2001];
    long long f[2001];//f[i]代表f[i]到f[n]的最大值
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&a[i].a);
            a[i].i=i;
        }
    
        std::sort(a+1,a+n+1,cmp);
        int cnt=0;
        a[0].a=-1;
        for(int i=1;i<=n;++i)
        {
            if(a[i].a!=a[i-1].a)
            {
                ++cnt;
                ori[cnt]=a[i].a;
            }
            a[i].b=cnt;
        }
        std::sort(a+1,a+1+n);
    
    
        long long ans=1000000000000000ll;
    
        memset(f,0,sizeof(f));
        f[cnt+1]=10000000000000ll;
        for(int i=n;i;--i)
        {
            for(int j=1;j<=cnt;++j)
                f[j]+=Abs(a[i].a-ori[j]);
            for(int j=cnt;j;--j)
                f[j]=Min(f[j],f[j+1]);
        }
        for(int i=1;i<=cnt;++i)
            ans=ans<f[i]?ans:f[i];
        printf("%lld
    ",ans);
        return 0;
    }

    题解 of 贪心:

        在翻最优解代码时,发现了一种玄学贪心,用了堆优化,时间复杂度是(O(nlog n)),分析了很久感觉算法是对的但是不知道是什么原理。后来@Dew推出来了应该是正确的思路。

        对于维护从左到右的不下降性质,可以用一个大根堆。假设当前做到第(i)段,前面的(1-i-1)段都已经保证了不下降性质,每一段的高度都被放在了堆里,那么如果现在插入了一个数不满足不下降性质,我们就从大根堆里取出堆顶(x)。我们知道,要使(a_i)和(x)这两个元素满足不下降,所要付出的代价至少是(|a_i-x|)。而我们可以发现,对于两个数,假设是5和2,我们付出(|a_i-x|=3)的代价可以使这两个数满足不下降这几种中的任意一种:({5,5},{4,4},{3,3},{2,2}),由于贪心,我们把5取出来,把({2,2})放进去,并计算一个3的代价,这个代价是必须的。不过现在序列可能变成了({1,3,4,2,2}),它好像不满足不下降了。

        不过我们心里要明白,最后的({2,2})是活动的,可以换成({4,4})或({5,5}),({2,2})只是一个下界。而我们把5这个“障碍”移开了,如果接下来再出现小于4的,就可以把4的位置换成新来的元素,而不论怎样,因为他们可以被替换,所以还是维持了不下降的性质。而因为5被替换后,4成了最大的,所以那些活动的元素上界就成了4,此时满足以4结尾的不下降序列。

    Code of greedy:(虽然都是ms级的

    #include<cstdio>
    #include<queue>
    using std::priority_queue;
    priority_queue<int> q;
    priority_queue<int> q1;
    int a[2010];
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
            scanf("%d",&a[i]);
    
        long long sum=0,ans=0;
        for(int i=1;i<=n;++i)
        {
            q.push(a[i]);
            if(q.top()>a[i])//a[i]不是最大的,就需要调整
            {
                sum+=q.top()-a[i];
                q.pop();
                q.push(a[i]);
            }
        }
        ans=sum;
        sum=0;
        for(int i=n;i;--i)
        {
            q1.push(a[i]);
            if(q1.top()>a[i])
            {
                sum+=q1.top()-a[i];
                q1.pop();
                q1.push(a[i]);
            }
        }
        printf("%lld
    ",ans<sum?ans:sum);
        return 0;
    }
  • 相关阅读:
    自动化运维-ansible
    Linux中安装wget命令
    Celery
    SQLAlchemy
    非关系型数据库--MongoDB
    Flask框架
    Redis
    公钥与私钥
    win10 文件扩展名的更改
    if 和 elif 的区别
  • 原文地址:https://www.cnblogs.com/wjyyy/p/lg2893.html
Copyright © 2011-2022 走看看