zoukankan      html  css  js  c++  java
  • 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

    |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

    【题意】给出一个序列,求以最小代价改成单调不下降序列或单调不上升序列。这里只求单调不减序列。

    【思路】dp[i][j]=min(dp[i-1][j]|1<=j<=n)+abs(a[i]-b[j]);

    dp[i][j]前i个数,最大为b[j]时的最小代价

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<cmath>
    #define inf 0x7fffffff
    #define ll long long
    #define get_abs(a) ((a)>0?(a):-(a))
    using namespace std;
    const int N=2222;
    ll a[N],b[N];
    long long int dp[N][N];
    
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            for(int i=1; i<=n; i++)
            {
                scanf("%d",&a[i]);
                b[i]=a[i];
            }
            sort(b+1,b+1+n);
            memset(dp,0,sizeof(dp));
            ll minx,ans=inf;
            for(int i=1; i<=n; i++)
            {
                minx=dp[i-1][1];
                for(int j=1; j<=n; j++)
                {
                    minx=min(minx,dp[i-1][j]);
                    dp[i][j]=minx+get_abs(a[i]-b[j]);
                }
            }
            for(int i=1; i<=n; i++)
            {
                ans=min(ans,dp[n][i]);
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    JZOJ 4298. 【NOIP2015模拟11.2晚】我的天
    JZOJ 4314. 【NOIP2015模拟11.4】老司机
    JZOJ 4313. 【NOIP2015模拟11.4】电话线铺设
    SP2416 DSUBSEQ
    JZOJ 2020.08.03【NOIP提高组】模拟 &&【NOIP2015模拟11.5】
    Android一些网站介绍
    http://www.androiddevtools.cn/
    Eclipse的安装使用
    JDK环境配置
    关于appcompat_v7的说明
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5877202.html
Copyright © 2011-2022 走看看