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
  • 相关阅读:
    Kendo UI for ASP.NET MVC 的一些使用经验
    AI智能技术监控学生上课行为,智慧管理加强校园教学质量
    如何通过ffmpeg 实现实时推流和拉流保存的功能
    如何测试流媒体服务器的并发能力?
    TSINGSEE青犀视频H265播放器FLV.js播放一段时间后报内存不足怎么处理?
    互动电视的未来应该是什么样的?
    牛客练习赛89 题解
    【洛谷P3934】炸脖龙 I
    【CF1463F】Max Correct Set
    【CF1496F】Power Sockets
  • 原文地址:https://www.cnblogs.com/kongbb/p/10446142.html
Copyright © 2011-2022 走看看