zoukankan      html  css  js  c++  java
  • POJ-3666-Making the Grade(DP)

    链接:

    https://vjudge.net/problem/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.

    思路:

    Dp[i, j], 表示第i个值, 变成j的最小花费,
    考虑总和较大, 离散化把值控制到2000以内即可.

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    //#include <memory.h>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    #include <string>
    #include <assert.h>
    #include <iomanip>
    #include <iostream>
    #include <sstream>
    #define MINF 0x3f3f3f3f
    using namespace std;
    typedef long long LL;
    const LL MOD = 20090717;
    const LL MAXN = 2e3+10;
    
    LL a[MAXN], Dp[MAXN][MAXN];
    LL b[MAXN];
    int n;
    
    LL Abs(LL l, LL r)
    {
        return l > r?l-r:r-l;
    }
    
    int main()
    {
        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++)
        {
            LL mmin = Dp[i-1][1];
            for (int j = 1;j <= n;j++)
            {
                mmin = min(mmin, Dp[i-1][j]);
                int val = Abs(b[j], a[i])+mmin;
                Dp[i][j] = val;
            }
        }
        LL res = Dp[n][1];
        for (int i = 1;i <= n;i++)
            res = min(res, Dp[n][i]);
        printf("%d
    ", res);
    
        return 0;
    }
    
  • 相关阅读:
    第九章 读书笔记
    第八章 读书笔记
    第七章 读书笔记
    第六章 读书笔记
    第五章 读书笔记
    第四章读书笔记
    第三章读书笔记
    第九章 硬件抽象层:HAL
    第10章 嵌入式linux的调试技术
    第八章 蜂鸣器驱动
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11660890.html
Copyright © 2011-2022 走看看