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. Mac Dock 效果及原理(勾股定理)

    这个是苹果机上的 Dock 效果,Windows 上也有一款专门的模拟软件——RocketDock. 代码如下: <!doctype html> <html> <head ...

  2. 设计模式 - 命令模式(command pattern) 具体解释

    命令模式(command pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 命令模式(command pattern) : 将请求封装成对 ...

  3. MySQL数据库的双向加密方式

    如果你正在运行使用MySQL的Web应用程序,那么你把密码或者其他敏感信息保存在应用程序里的机会就很大.保护这些数据免受或者窥探者的获取 是一个令人关注的重要问题,因为您既不能让未经授权的人员使用或者 ...

  4. Java 之文件目录操作

    1.判断文件是否存在 File file = new File("d:\\study\\temp\\test.java"); boolean bl = file.exists(); ...

  5. Excel02-快速无误输入多个零

    第一步:设置单元格格式-->小数位数为0,货币符号为¥ 第二步:在单元格输入数据:1**5回车即显示为¥100,000 **N 表示后面有N个零,会自动加入我们设置的货币符号¥ 这对我们在输入巨 ...

  6. linux/unix运行级别

    在SYSTEM V 风格的UNIX系统中,系统被分为不同的运行级别,这和BSD分支的UNIX有所不同,常用的为0~6七个级别:0关机 1单用户 2不带网络的多用户 3带网络的多用户 4保留,用户可以自 ...

  7. NS2仿真:使用NS仿真软件模拟简单网络模型

    NS2仿真实验报告1 实验名称:使用NS仿真软件模拟简单网络模型 实验日期:2015年3月2日~2015年3月7日 实验报告日期:2015年3月8日 一.实验环境(网络平台,操作系统,网络拓扑图) 运 ...

  8. 验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 <machineKey>

    转自:http://hi.baidu.com/taotaowyx/blog/item/074bb8d83907bb3233fa1ce6.html 验证视图状态 MAC 失败.如果此应用程序由网络场或群 ...

  9. (转)【已解决】关于SQL2008 “不允许保存更改。您所做的更改要求删除并重新创建以下表。您对无法重新创建的标进行了更改或者启用了‘阻止保存要求重新创建表的更改’” 解决方案

    近日在使用sql2008的过程中,要对已经创建完成的表结构进行修改,却一直提示弹出如下提示: “ 不允许保存更改.您所做的更改要求删除并重新创建以下表.您对无法重新创建的标进行了更改或者启用了“阻止保 ...

  10. (转)SQL Server 2005附加2008的数据库

    1. 生成for 2005版本的数据库脚本  2008 的manger studio  -- 打开"对象资源管理器"(没有的话按F8), 连接到你的实例  -- 右键要转到2005 ...