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

    Making the Grade
    Time Limit: 1000MS      Memory Limit: 65536K
    Total Submissions: 8732     Accepted: 4088
    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
    Source
    
    USACO 2008 February Gold

    经过证明可以知道选出的数字一定在a中出现过。
    (待补充)


    先考虑单调不降的情况
    f[i][j]为前i位,最后一位选取的数字是j的情况。
    j也就是最大值了。

    f[i][j]=min(f[i-1][k])+abs(a[i]-j] 其中1<=k<=j

    就像LICS一样,转移可以由O(n)压为O(1)

    简单地讲,就是不需要真的遍历[1,j]的最小值,它是由前往后转移的,维护决策集合的最小值,加入新值的时候判断是否可以更新决策集合的最小值mn。

    不升同理,但是这题数据有问题,不降就可以过了。


    //Stay foolish,stay hungry,stay young,stay simple
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    const int MAXN=2002;
    
    int n;
    int a[MAXN],b[MAXN];
    
    int f[MAXN][MAXN];
    
    int main(){
        cin.sync_with_stdio(false);
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>a[i],b[i]=a[i];
        sort(b+1,b+1+n);
        for(int i=1;i<=n;i++){
            int mn=f[i-1][1];
            for(int j=1;j<=n;j++){
                mn=min(mn,f[i-1][j]);
                f[i][j]=mn+abs(a[i]-b[j]);
            }
        }
        int ans=1<<30;
        for(int i=1;i<=n;i++) ans=min(ans,f[n][i]);
        cout<<ans<<endl;
        return 0;
    }
    

    本文来自博客园,作者:GhostCai,转载请注明原文链接:https://www.cnblogs.com/ghostcai/p/9247445.html

  • 相关阅读:
    LeetCode 面试题 02.02. 返回倒数第 k 个节点
    LeetCode 1290. 二进制链表转整数
    LeetCode 面试题52. 两个链表的第一个公共节点
    LeetCode 2. 两数相加
    Jupyter Notebook 常用快捷键 (转)
    LeetCode 414. 第三大的数
    LeetCode 404. 左叶子之和
    三年了
    LeetCode 543. 二叉树的直径
    求结点在二叉排序树中层次的算法
  • 原文地址:https://www.cnblogs.com/ghostcai/p/9247445.html
Copyright © 2011-2022 走看看