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可变参数
    为什么static方法中不可以调用非static方法
    用注解@DelcareParents实现引用增强
    在SpringBoot中用SpringAOP实现日志记录功能
    梳理一下我理解的aop
    包裹iframe的div与iframe存在高度差的问题解决方案
    非跨域情况下iframe 高度自适应的问题解决(一)
    flex布局较之float布局的优点新发现
    webpack4 动态导入文件 dynamic-import 报错的解决方法
    vue chrome 浏览器调试工具devtools插件安装
  • 原文地址:https://www.cnblogs.com/sunus/p/4487527.html
Copyright © 2011-2022 走看看