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/
  • 相关阅读:
    第一章、web应用安全概论--web应用系统介绍--TCP/IP协议
    IIS配置导入导出
    shell习题第10题:打印每个单词的字数
    腾讯云的对象存储COS
    shell习题第9题:sed的常用用法
    shell习题第8题:监控nginx的502状态
    shell习题第7题:备份数据库
    shell习题第6题:监听80端口
    Python的math模块
    Python模块
  • 原文地址:https://www.cnblogs.com/Menteur-Hxy/p/9248005.html
Copyright © 2011-2022 走看看