zoukankan      html  css  js  c++  java
  • POJ 3666 Making the Grade

    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

    A B 1| + | A B 2| + ... + | 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


    将一个长度为n的数组更改成一个非严格递增或者非严格递减的数组,代价为更改前后的数值的差的绝对值。
    总代价就为 ∑abs(a[i]-b[i])(i<=n) a为输入的数组,b为更改后的新数组。
    输出最小的总代价。

    这道题的数据有点水。只需要求非严格递增的最小总代价就可以了。

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    
    int a[2005], b[2005];
    long long dp[20005][2005];
    
    int main(){
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]), b[i]=a[i];
    
        sort(b+1,b+n+1);
    
        for(int i=1;i<=n;i++){
            long long min_ = dp[i-1][1];
            for(int j=1;j<=n;j++){
                min_ = min(min_, dp[i-1][j]);
                dp[i][j] = abs(a[i]-b[j]) + min_;
            }
        }
    
        long long ans = dp[n][1];
        for(int i=1;i<=n;i++)
            ans = min(ans,dp[n][i]);
    
        printf("%lld
    ",ans);
        return 0;
    }
    View Code
  • 相关阅读:
    VBA中使用计时器的两种方法
    好的关卡离不开优秀的团队
    如何从无到有做一个好关卡?
    性能优化总结
    用超链接提交表单,实现在动态网页的url中隐藏参数
    js 中使用el表达式 关键总结:在js中使用el表达式一定要使用双引号
    js中getBoundingClientRect的作用及兼容方案
    IE10、IE11和Microsoft Edge的Hack
    CSS Hack大全-教你如何区分出IE6-IE10、FireFox、Chrome、Opera
    点击a标签,跳转到iframe中,并在iframe中显示指定的页面
  • 原文地址:https://www.cnblogs.com/kongbb/p/10446142.html
Copyright © 2011-2022 走看看