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

    Making the Grade
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 4656   Accepted: 2206

    Description

    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

    |AB1| + |AB2| + ... + |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
    

    Source

    经典dp

    dp[i][j]表示第i个数变成第j小的数的最小消耗。

    dp[i][j] = dmin[i - 1][j] + abs(o[i] - d[j]) (ps : dmin[i - 1][j] 表示前i - 1个数并且第i - 1个数小于等于j的最小消耗, abs(o[i] - d[j]) 表示第i个数变成j的消耗);

    数据较水,只保证是上升序列即可。

    #include <cstdio>
    #include <iostream>
    #include <sstream>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <algorithm>
    using namespace std;
    #define ll long long
    #define _cle(m, a) memset(m, a, sizeof(m))
    #define repu(i, a, b) for(int i = a; i < b; i++)
    #define MAXN 2005
    ll dp[MAXN][MAXN];
    ll dmin[MAXN][MAXN];
    ll d[MAXN], o[MAXN];
    int main()
    {
       int n;
       scanf("%d", &n);
       repu(i, 1, n + 1) { scanf("%I64d", &d[i]); o[i] = d[i]; }
       sort(d + 1, d + n + 1);
       int l = 1;
       repu(i, 2, n + 1) if(d[i] != d[l]) d[++l] = d[i];
    
       repu(i, 1, l + 1) {
           dp[1][i] = (ll)abs((int)o[1] - (int)d[i]);
           if(i == 1) dmin[1][i] = dp[1][i];
           else dmin[1][i] = min(dp[1][i], dmin[1][i - 1]);
       }
       ll t = 0;
       repu(i, 2, n + 1) {
         dp[i][1] = dp[i - 1][1] + (ll)abs((int)o[i] - (int)d[1]);
         dmin[i][1] = dp[i][1];
         repu(j, 2, l + 1) {
            dp[i][j] = dmin[i - 1][j] + (ll)abs((int)o[i] - (int)d[j]);
            dmin[i][j] = min(dp[i][j], dmin[i][j - 1]);
         }
       }
       printf("%I64d
    ", dmin[n][l]);
       return 0;
    }
    View Code
  • 相关阅读:
    java内存溢出
    jstack命令使用
    JVM问题排查步骤
    c++指针常量和常量指针
    c++ 通讯录
    冒泡排序
    翻转数组
    敲桌子
    求一个100-999之间的水仙花数
    elasticsearch 模板的使用
  • 原文地址:https://www.cnblogs.com/sunus/p/4487527.html
Copyright © 2011-2022 走看看