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
  • 相关阅读:
    如何将程序添加到系统服务实现开机自启动
    ASP.NET速度优化
    layer官方演示与讲解(jQuery弹出层插件)
    SQLSERVER排查CPU占用高的情况
    解决FlexPaper分页分段加载问题(转)
    SQL 2008升级SQL 2008 R2完全教程或者10.00.1600升级10.50.1600
    Virtualbox中win7虚拟机中U盘不可用问题的解决
    解决:对COM 组件的调用返回了错误 HRESULT E_FAIL
    google chrome插件开发,自己动手,丰衣足食
    WebKit.net最简单使用方法
  • 原文地址:https://www.cnblogs.com/kongbb/p/10446142.html
Copyright © 2011-2022 走看看