zoukankan      html  css  js  c++  java
  • POJ 3666 Making the Grade

    Making the Grade

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5037   Accepted: 2384

    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

    |A1 - B1| + |A2 - B2| + ... + |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
    

    这个题目苦思冥想了好久,但是一直不能总结出转移方程,看了这个人的题解。发现用DP求解这个真的很简单。

    另外开一个数组保存排好序的输入的数字串

    利用dp[i][j]表示前i个元素中以j作为最后一个数的单调序列需要的最小花费。

    转移方程:dp[i][j]  = min(dp[i – 1][k]) + |A[i] – B[j]|       (k = 0…j)

    其中如果我们每次对k都再进行一次循环会超时,我们发现k的值就是i - 1之前所有的数字以k为最后的最小消费。他和dp[i - 1][j]中的最小值就是k的值。就不用循环了。

    代码如下:

    /*************************************************************************
    	> File Name: Making_the_Grade.cpp
    	> Author: Zhanghaoran
    	> Mail: chilumanxi@xiyoulinux.org
    	> Created Time: Wed 28 Oct 2015 10:33:23 PM CST
     ************************************************************************/
    
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #define ll long long
    using namespace std;
    
    int N;
    ll a[2010];
    ll dp[2010][2010];
    ll temp[2010];
    
    int min(ll a, ll b){
        return a < b ? a : b;
    }
    
    int main(void){
        scanf("%d", &N);
        for(int i = 1; i <= N; i ++){
            scanf("%lld", &a[i]);
            temp[i] = a[i];
        }
        sort(temp + 1, temp + 1 + N);
        for(int i  = 1; i <= N; i ++){
            dp[1][i] = llabs(a[1] - temp[i]);
            //cout << temp[i] << endl;
        }
    
        for(int i = 2; i <= N; i ++){
            ll k = dp[i - 1][1];
            for(int j = 1; j <= N; j ++){
                k = min(k, dp[i - 1][j]);
                dp[i][j] = k + llabs(a[i] - temp[j]);
            }
        }
        ll Min = dp[N][1];
        for(int i = 2; i <= N; i ++){
            if(dp[N][i] < Min)
                Min = dp[N][i];
        }
        
        printf("%d
    ", Min);
    }


  • 相关阅读:
    Mimblewimble:新型的隐私保护协议
    权益证明生态系统
    理解去中心化身份
    TPS 是一种糟糕的评价标准
    以太坊 2.0 :双生以太奇谭
    以太坊 2.0:信标链
    以太坊 2.0:验证者详解
    论共识机制
    以太坊钱包开发系列
    将不确定变成确定~LINQ DBML模型可以对应多个数据库吗
  • 原文地址:https://www.cnblogs.com/chilumanxi/p/5136063.html
Copyright © 2011-2022 走看看