码迷,mamicode.com
首页 > 编程语言 > 详细

POJ 3666 Making the Grade(数列变成非降序/非升序数组的最小代价,dp)

时间:2018-07-21 22:38:10      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:alt   integer   enc   put   type   forms   查找   first   form   

传送门:

http://poj.org/problem?id=3666

Making the Grade
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9468   Accepted: 4406

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

 
 

题目意思:

给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调增或者单调减(不严格).

参考了一下网友的思想:https://www.cnblogs.com/Philip-Tell-Truth/p/4916026.html

就是农夫要修一条路,现在要求这条路要么就是上升的,要么就是下降的,总代价为∑|a[i]-b[i]|,求代价最低的修路方案, (0 ≤ β≤ 1,000,000,000) , (1 ≤ N ≤ 2,000)

  这一题百分百就是DP了,为什么?我们现在就是要让cost最小,但是我们不知道cost应该怎么才能最小。

  我们可以这么想,因为序列总是上升或者下降的,我们可以考虑上升的情况,假设前几个数组成的最大值为β,我们要考虑从0-β的改变值,然后不断推到第n个序列。

  显然,这样的复杂度为0(Nβ^2),当然这样的复杂度显然是出事的。  

  现在我们想着优化这个东西,我们可以这么想,如果我们像之前那样扫描的话,那么其实我们忽略了一个很重要的事实,就是在变到α(α<β),其实对于α+1~β之内不会对α造成影响,于是我们可以用一个最小值的临时变量储存在α之前的最小值,用这个更新dp即可,那样就少了一次扫β的复杂度

  复杂度变为O(Nβ);

  但是如果仅仅是这样的话,绝对是TLE,因为β实在是太大了。

  那么我们就要用到离散化的思想,把β投影到有限区域中,既然β是大小的函数,那么我们把可以这样对应:我们只用把新的序列按从小到大排列,然后只对下标进行查找就可以了,这样我们就把解的空间变到下标中了。

   最后状态转移方程:dp[i-1][j]=ABS(a[i]-b[j])+min(dp[i-1][j]);(用滚动数组就可以了)

 

具体做法:

    首先可以看出变化后的序列中所有的数一定还在原数列中,
     那么先对原数列排序
        a                  b
   1 3 2 4 5 3 9 -> 1 2 3 3 4 5 9
   然后dp[i][j]  表示第i个数, 把他变成 b[j] 所要画的最小代价
   dp[i][j] = dp[i-1] [ 0~j] + abs(b[j] - a[i])   以此循环。
 
code:
#include<stdio.h>
#include<string.h>
#include<memory>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
#define max_v 2005
#define INF 0x7fffffff
int n;
int dp[max_v];
int e[max_v];
int b[max_v];
int main()
{
    /*
    首先可以看出变化后的序列中所有的数一定还在原数列中,
     那么先对原数列排序

        a                  b
   1 3 2 4 5 3 9 -> 1 2 3 3 4 5 9
   然后dp[i][j]  表示第i个数, 把他变成 b[j] 所要画的最小代价
   dp[i][j] = dp[i-1] [ 0~j] + abs(b[j] - a[i])   以此循环。
   */
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>e[i];
        b[i]=e[i];
    }
    sort(b,b+n);//升序之后的数组,对比e
    int ans=INF;
    int t;
    for(int i=0;i<n;i++)
    {
        t=INF;
        for(int j=0;j<n;j++)
        {
            t=min(t,dp[j]);
            dp[j]=abs(b[j]-e[i])+t;
        }
    }
    for(int i=0;i<n;i++)
    {
        ans=min(ans,dp[i]);
    }
    printf("%d\n",ans);
    return 0;
}

 

POJ 3666 Making the Grade(数列变成非降序/非升序数组的最小代价,dp)

标签:alt   integer   enc   put   type   forms   查找   first   form   

原文地址:https://www.cnblogs.com/yinbiao/p/9345700.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!