• POJ 3666 峰谷化单调斜坡


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

    这道题目大意是给出一组山谷的海拔,将这组山谷化为单调统一的斜坡(上坡或下坡,但是中间不允许有起伏)。
    当然将这些峰谷进行操作时需要一定的花费,不过题目还好一些:所进行的填山与挖山的操作花费是相同的,即将坐标a移到坐标b需要花费|a-b|。这时我们容易想到对于一组操作,总花费就是这n组|a-b|的和。

    我们可以对每一步进行划分,用dp[i][j]表示第i个坐标数据的第j(1<=j<=n)次操作。
    因此我们所求解就是对第n个数据坐标的第j次操作的最小值(min(dp[n][j]))。
    状态转移方程如下:minn = min(dp[i - 1][j],minn);dp[i][j] = minn + fabs(a[i] - b[j]);

    另外题目给出的一个地方是这组数据的坐标: (0 ≤ Ai ≤ 1,000,000,000) ,我们要想到使用long long来定义数据。

    具体代码如下:

    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cmath>
    using namespace std;
    const int maxn = 2010;
    const long long inf = 1e15;                 //定义了一个超级大的数
    long long dp[maxn][maxn],a[maxn],b[maxn];
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i = 1;i <= n;i++)
        {
            scanf("%lld",&a[i]);
            b[i] = a[i];                         //记录下初始坐标,并准备为这些坐标进行相应排序
        }
        sort(b + 1,b + 1 + n);              
        for(int i = 1;i <= n;i++)
            dp[1][i] = fabs(a[1] - b[i]);                //排完序用dp[1][i]记录下a[1](首坐标)与排完序后各坐标差的绝对值(操作消费)
        for(int i = 2;i <= n;i++)
        {
            long long minn = dp[i - 1][1];
            for(int j = 1;j <= n;j++)
            {
                minn = min(dp[i - 1][j],minn);
                dp[i][j] = minn + fabs(a[i] - b[j]);
            }
        }
        long long ans = inf;
        for(int i = 1;i <= n;i++)
            ans = min(ans,dp[n][i]);               //枚举求在对n组坐标操作完毕后的最小花费
        printf("%lld
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    React之react-router(connected-react-router/react-router-dom)
    React之redux学习日志(redux/react-redux/redux-saga)
    React之常用技术栈
    js书写规范
    Mybatis plus 常用更新操作
    Mybatis plus 常用删除方式
    Mybatis plus 实体类常用注解
    spring boot 单元测试
    mybatis plus 添加启用打印日志
    Lombok插件优缺点
  • 原文地址:https://www.cnblogs.com/study-hard-forever/p/12130016.html
Copyright © 2020-2023  润新知