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;
    }
  • 相关阅读:
    【Spring】注解的循环依赖问题
    【网络】计算机网络自顶向下读书笔记
    【JUC】并发编程的艺术笔记
    【JUC】多线程手撕代码面试题
    【操作系统】王道操作系统全盘汇总
    【Spring】IoC源码分析以及实现
    【Spring】用例子来初次理解IoC
    拼音工具类
    chr(10)与chr(13)的区别
    List 集合转String 逗号拼接
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5877202.html
Copyright © 2011-2022 走看看