zoukankan      html  css  js  c++  java
  • [poj 3666] Making the Grade (离散化 线性dp)

    今天的第一题(/ω\)!
    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

    code:

    //By Menteur_Hxy
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    using namespace std;
    
    const int MAX=2010;
    const int INF=0x3f3f3f3f;
    int n,a[MAX],num[MAX],f[MAX][MAX];
    
    int main() {
        scanf("%d",&n);
        for(int i=1;i<=n;i++) {
            scanf("%d",&a[i]);
            num[i]=a[i];
        }
        sort(num+1,num+n+1);
        int m=unique(num+1,num+n+1)-num-1;
        memset(f,0x3f,sizeof f);
        f[0][0]=0;
        for(int i=1;i<=n;i++) {
            int temp=f[i-1][0];
            for(int j=1;j<=m;j++) {
                temp=min(temp,f[i-1][j]);
                f[i][j]=temp+abs(a[i]-num[j]);
            }
        }
        int ans=INF;
        for(int i=1;i<=m;i++) ans=min(ans,f[n][i]);
        printf("%d",ans);
        return 0;
    }
    版权声明:本文为博主原创文章,未经博主允许不得转载。 博主:https://www.cnblogs.com/Menteur-Hxy/
  • 相关阅读:
    POJ 1113--Wall(计算凸包)
    博弈论笔记--06--纳什均衡之约会游戏与古诺模型
    atan和atan2反正切计算
    POJ 1410--Intersection(判断线段和矩形相交)
    FirstBird--项目流程
    POJ 2653--Pick-up sticks(判断线段相交)
    POJ 1066--Treasure Hunt(判断线段相交)
    POJ 2398--Toy Storage(叉积判断,二分找点,点排序)
    Jetty数据同步使用
    Linux小知识(1): bash中执行数据库的相关操作
  • 原文地址:https://www.cnblogs.com/Menteur-Hxy/p/9248005.html
Copyright © 2011-2022 走看看