zoukankan      html  css  js  c++  java
  • POJ 3666 Making the Grade (动态规划)

    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[i][j]表示,将第i个数字,变成第j大的数字所需的最小花费。j实际上就是离散化之后的数组的下标。
    dp[i][j]=min(dp[i-1][k]+abs(num[i]-p[k]),dp[i][j]);
    其中num是原高度,p是离散化后的数组。k<=j;
    但是这样的复杂度是n的三次方,不过还好我们可以用一个数组记录下j之前dp[i-1][k]+abs(num[i]-p[k])的最小值,这样就能优化成n方了。
    TLE
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<ctime>
    #define fuck(x) cout<<#x<<" = "<<x<<endl;
    #define debug(x,i,j) cout<<#x<<"["<<i<<"]["<<j<<"] = "<<x[i][j]<<endl;
    #define ls (t<<1)
    #define rs ((t<<1)+1)
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn = 100086;
    const int inf = 2.1e9;
    const ll Inf = 999999999999999999;
    const int mod = 1000000007;
    const double eps = 1e-6;
    const double pi = acos(-1);
    int num[2008],p[2008];
    int n;
    int dp[2008][2008];
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&num[i]);
            p[i]=num[i];
        }
        sort(p+1,p+1+n);
        int m=unique(p+1,p+1+n)-p-1;
        for(int i=1;i<=n;i++){
            int t=lower_bound(p+1,p+1+n,num[i])-p-1;
            for(int j=1;j<=m;j++){
                dp[i][j]=inf;
                for(int k=1;k<=j;k++){
                    dp[i][j]=min(dp[i-1][k]+abs(num[i]-p[k]),dp[i][j]);
                }
            }
        }
        printf("%d
    ",dp[n][m]);
        return 0;
    }
    View Code
    AC
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<ctime>
    #define fuck(x) cout<<#x<<" = "<<x<<endl;
    #define debug(x,i,j) cout<<#x<<"["<<i<<"]["<<j<<"] = "<<x[i][j]<<endl;
    #define ls (t<<1)
    #define rs ((t<<1)+1)
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn = 100086;
    const int inf = 2.1e9;
    const ll Inf = 999999999999999999;
    const int mod = 1000000007;
    const double eps = 1e-6;
    const double pi = acos(-1);
    int num[2008],p[2008];
    int n;
    int dp[2008][2008];
    int minn[2008];
    int main()
    {
    //    ios::sync_with_stdio(false);
    //    freopen("in.txt","r",stdin);
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&num[i]);
            p[i]=num[i];
        }
        sort(p+1,p+1+n);
        int m=unique(p+1,p+1+n)-p-1;
        for(int i=1;i<=n;i++){
            int t=lower_bound(p+1,p+1+n,num[i])-p-1;
            minn[0]=inf;
            for(int j=1;j<=m;j++){
                minn[j]=min(minn[j-1],dp[i-1][j]+abs(num[i]-p[j]));
            }
            for(int j=1;j<=m;j++){
                dp[i][j]=minn[j];
            }
        }
        printf("%d
    ",dp[n][m]);
        return 0;
    }
    View Code
  • 相关阅读:
    jQuery EasyUI API 中文文档
    easyui datagrid使用
    Jquery EasyUI的添加,修改,删除,查询等基本操作介绍
    JQueryEasyUI datagrid框架的基本使用
    使用easyUI 创建复杂的toolbar到datagrid
    转换和删除重复命令tr
    格式化文本数据抽取工具awk
    流编辑器sed
    查找文本工具grep
    查找文件工具find
  • 原文地址:https://www.cnblogs.com/ZGQblogs/p/10672660.html
Copyright © 2011-2022 走看看