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
  • 相关阅读:
    Oracle中的序列
    Oracle中常见表与各类结构的查询
    软件项目中的沟通 第一故事
    bat脚本 得到前一天的日期
    Windows bat脚本的for语句
    Integer与int比较
    String参数传递
    数据类型转换
    synchronized
    Java的Thread和Runnable
  • 原文地址:https://www.cnblogs.com/sunus/p/4487527.html
Copyright © 2011-2022 走看看