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 ( ≤ N ≤ ,) describing the elevation ( ≤ Ai ≤ ,,,) 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 -bit integers can certainly be used to compute the answer.

Input

* Line : A single integer: N
* Lines ..N+: Line i+ contains a single integer elevation: Ai

Output

* Line : 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


Sample Output


Source

 

题意:

给定一个序列,以最小代价将其变成单调不增或单调不减序列,这里的代价看题目公式。

思路:

很容易想到是DP。

1.

对前i个序列,构成的最优解其实就是与两个参数有关。一个是这个序列处理后的最大值mx,和这个序列处理的代价值cost。

显然最大值mx最小最好(这样第i+1个值可以不花代价直接接在其后面的可能性更大),cost最小也最好(题意要求),但是两者往往是鱼和熊掌。

用dp[i][j]表示:前i个数构成的序列,这个序列最大值为j,dp[i][j]的值代表相应的cost。

所以状态转移方程如下:

dp[i][j]=abs(j-w[i])+min(dp[i-1][k]);(k<=j)

 

这个表格是根据转移方程写出来的dp数组。

再仔细看一下转移方程:dp[i][j]=abs(j-w[i])+min(dp[i-1][k]);(k<=j)

右边没填充的是因为填充的数字肯定比前面的数字大,无用,因为在求min( dp[i-1][k] )时,是求最小值,既然更大,则最小值时无需考虑。

又从表格中可以看出:

dp[i][j]=abs(j-w[i])+min(dp[i-1][k]);(k<=j)这里的k无需从1遍历到j。

只要在对j进行for循环的时候不断更新一个dp[i-1][j]的最小值mn=min(mn,dp[i-1][j]),

然后对dp[i][j]=abs(j-w[i])+mn即可;

这样改进之后即可从本来的时候时间复杂度O(NMM)改进为O(NM);

 

但是,这里的m是A[i]的最大值,显然TLE。

所以必须用离散化思想改进,因为N=2000。远小于A[i]的最大值。

离散化:将序列排序一下,然后用位置的前后关系来制定其值,这样时间复杂度变成O(N^2).

 

 

最后是这题数据有bug,只需要求不减序列即可。

 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
#define inf 1<<30
#define N 2006
int n;
int a[N];
int b[N]; int dp[N][N];
bool cmp(int a,int b){
return a>b;
}
int main()
{
while(scanf("%d",&n)==){
for(int i=;i<=n;i++){
int x;
scanf("%d",&x);
a[i]=b[i]=x;
} sort(b+,b++n);
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++){
dp[][i]=abs(a[]-b[i]);
}
//dp[1][tmp]=tmp;
for(int i=;i<=n;i++){
int tmp=dp[i-][];
for(int j=;j<=n;j++){
tmp=min(tmp,dp[i-][j]);
dp[i][j]=tmp+abs(a[i]-b[j]);
}
}
int ans1=inf;
for(int i=;i<=n;i++){
ans1=min(ans1,dp[n][i]);
} //printf("%d\n",ans1); memset(dp,,sizeof(dp));
sort(b+,b++n,cmp); for(int i=;i<=n;i++){
dp[][i]=abs(a[]-b[i]);
}
for(int i=;i<=n;i++){
int tmp=dp[i-][];
for(int j=;j<=n;j++){
tmp=min(tmp,dp[i-][j]);
dp[i][j]=tmp+abs(a[i]-b[j]);
}
}
int ans2=inf;
for(int i=;i<=n;i++){
ans2=min(ans2,dp[n][i]);
}
printf("%d\n",min(ans1,ans2)); }
return ;
}

poj 3666 Making the Grade(dp)的更多相关文章

  1. Poj 3666 Making the Grade (排序+dp)

    题目链接: Poj 3666 Making the Grade 题目描述: 给出一组数,每个数代表当前位置的地面高度,问把路径修成非递增或者非递减,需要花费的最小代价? 解题思路: 对于修好的路径的每 ...

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

    传送门: http://poj.org/problem?id=3666 Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total ...

  3. POJ - 3666 Making the Grade(dp+离散化)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  4. POJ 3666 Making the Grade(二维DP)

    题目链接:http://poj.org/problem?id=3666 题目大意:给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调不降或者单调不增(题目BUG,只能求 ...

  5. poj 3666 Making the Grade(离散化+dp)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  6. POJ 3666 Making the Grade (线性dp,离散化)

    Making the Grade Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) T ...

  7. poj 3666 Making the Grade(dp离散化)

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7068   Accepted: 3265 ...

  8. POJ 3666 Making the Grade (DP)

    题意:输入N, 然后输入N个数,求最小的改动这些数使之成非严格递增即可,要是非严格递减,反过来再求一下就可以了. 析:并不会做,知道是DP,但就是不会,菜....d[i][j]表示前 i 个数中,最大 ...

  9. POJ 3666 Making the Grade (DP滚动数组)

    题意:农夫约翰想修一条尽量平缓的路,路的每一段海拔是A[i],修理后是B[i],花费|A[i] – B[i]|,求最小花费.(数据有问题,代码只是单调递增的情况) #include <stdio ...

随机推荐

  1. SVN权限修复

    Description : Commit failed (details follow): Suggestion : The operation could not be completed. Tec ...

  2. 解决IE9以下ie版本不能识别新元素的方法 添加一个js -- Shiv Solution

    Thankfully, Sjoerd Visscher created the "HTML5 Enabling JavaScript", "the shiv": ...

  3. 互联网程序设计c++

    地址:ftp.sist.stdu.edu.cn用户名:lzh_hlw20133密码:lzhstdftp端口:2014

  4. Android Fragment 详解(一)

    Android从3.0开始引入fragment,主要是为了支持更动态更灵活的界面设计,比如在平板上的应用.平板机上拥有比手机更大的屏幕空间来组合和交互界面组件们.Fragment使你在做那样的设计时, ...

  5. 酷Q机器人,QQ机器人使用教程

    软件介绍: 酷Q,软件酷Q机器人是一款基于webqq开发的一款自动接收.处理qq消息的软件. 改程序使用易语言编写,精简大量不必要代码,减小了软件体积,优化程序速度,使得酷Q更加轻巧好用. 在消息处理 ...

  6. a标签调用js的几种方法

    我们常用的在a标签中有点击事件: <a> 标签的 href 属性用于指定超链接目标的 URL,href 属性的值可以是任何有效文档的相对或绝对 URL,包括片段标识符和 JavaScrip ...

  7. Xcode使用xib拖线时出现: could not insert new outlet connection

    解决方法: 1.在新建类的时候没有选择将这个类加入到对应的"Target"中. 2.重新将文件加入项目 操作步骤就是选中出问题的.m和.h文件,点删除键,然后选"Remo ...

  8. Codeforces Round #279 (Div. 2)f

    树形最大上升子序列 这里面的上生子序列logn的地方能当模板使  good #include<iostream> #include<string.h> #include< ...

  9. html+css布局小练习w3cfuns

    虽然花了很长时间,但是也知道了不少,这次也不像以前了,不知道怎么下手,虽然是照着图片做,不过也做出来了图片来自w3cfuns:网站图片url  看了w3cfuns的两天驾驭DIV+CSS 这个网站对新 ...

  10. HTML&CSS基础学习笔记1.24-input标签的单选与多选

    单选和多选 单选框和多选框是用<input>标签来实现的. <input>标签的type属性值为"checkbox"时,表示多选框,为"radio ...