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
  • 相关阅读:
    待写
    让一个小div在另一个大div里面 垂直居中的四种方法
    20 个有用的 SVG 工具,提供更好的图像处理
    php原理简述
    Apache 打开网页的时候等待时间过长的解决方案
    TCP协议中的三次握手和四次挥手(图解)
    apache 各平台进程线程模块解析
    浅谈移动Web开发(上):深入概念
    响应式布局
    jQuery Mobile 入门教程
  • 原文地址:https://www.cnblogs.com/kongbb/p/10446142.html
Copyright © 2011-2022 走看看