POJ - 3666 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
| A 1 - B 1| + | A 2 - B 2| + ... + | 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 9 Sample Output
题目链接:http://poj.org/problem?id=3666
推荐题解链接:http://www.hankcs.com/program/cpp/poj-3666-making-the-grade.html
**********************************************
题意:给定一个正整数序列a[1...n],要求你改变每一个数变成b[1...n],使得改变后的序列非严格单调,改变的代价为abs(a[1]-b[1])+...+abs(a[n]-b[n]),求代价最小值。
分析:dp+离散化。
显然b[i]必定为a[1...n]中的某个值,且由于a过大,所以离散化。我们将每个数离散化 然后排序。设dp[i][j]为第i个数改变为b[j]时代价最小值,
则dp[i][j]=min(dp[i-1][k])+abs(a[i]-b[j]),然后 b数组表示的就是 离散过的 那些数据。
AC代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<time.h>
#include<stack>
using namespace std;
#define N 12000
#define INF 0x3f3f3f3f int b[N],a[N],dp[N]; int main()
{
int n,i,j; while(scanf("%d", &n) != EOF)
{
memset(dp,,sizeof(dp));
memset(a,,sizeof(a));
memset(b,,sizeof(b)); for(i=;i<n;i++)
{
scanf("%d", &a[i]);
b[i]=a[i];
} sort(b,b+n); for(i=;i<n;i++)
dp[i]=abs(a[]-b[i]); for(i=;i<n;i++)
{
int mi=INF;
for(j=;j<n;j++)
{
mi=min(mi,dp[j]);
dp[j]=mi+abs(a[i]-b[j]);
}
} int minn=INF;
for(i=;i<n;i++)
minn=min(dp[i],minn); printf("%d\n",minn);
}
return ;
}
POJ - 3666 Making the Grade(dp+离散化)的更多相关文章
- [poj 3666] Making the Grade (离散化 线性dp)
		
今天的第一题(/ω\)! Description A straight dirt road connects two fields on FJ's farm, but it changes eleva ...
 - poj 3666 Making the Grade(dp)
		
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
 - Poj 3666 Making the Grade  (排序+dp)
		
题目链接: Poj 3666 Making the Grade 题目描述: 给出一组数,每个数代表当前位置的地面高度,问把路径修成非递增或者非递减,需要花费的最小代价? 解题思路: 对于修好的路径的每 ...
 - POJ 3666 Making the Grade(数列变成非降序/非升序数组的最小代价,dp)
		
传送门: http://poj.org/problem?id=3666 Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total ...
 - poj 3666 Making the Grade(离散化+dp)
		
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
 - poj 3666 Making the Grade(dp离散化)
		
Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7068 Accepted: 3265 ...
 - POJ 3666 Making the Grade (线性dp,离散化)
		
Making the Grade Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) T ...
 - POJ 3666 Making the Grade(二维DP)
		
题目链接:http://poj.org/problem?id=3666 题目大意:给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调不降或者单调不增(题目BUG,只能求 ...
 - POJ 3666 Making the Grade (DP)
		
题意:输入N, 然后输入N个数,求最小的改动这些数使之成非严格递增即可,要是非严格递减,反过来再求一下就可以了. 析:并不会做,知道是DP,但就是不会,菜....d[i][j]表示前 i 个数中,最大 ...
 
随机推荐
- windows 环境下搭建django 错误分析总结
			
最近对于python核心编程学习完后,想进一步学习django的web开发,考虑再三还是决定在本机(win7)上搭建环境. 刚接触难免会出现问题,最大的一个问题是安装完django的包后,在cmd命令 ...
 - C# 二维数组和集合
			
本次课我主要学习了二维数组和集合的部分内容. 在二维数组的部分中,我主要学习了二维数组的定义方法:int [,] array=new int [4,2];做了一个小练习:用二维数组打印自己的姓氏. s ...
 - 练习3:修改withdraw 方法
			
练习目标-使用有返回值的方法:在本练习里,将修改withdraw方法以返回一个布尔值来指示交易是否成功. 任务 1.修改Account类 a.修改deposit 方法返回true(意味所有存款是成功的 ...
 - AIR文件操作:使用文件对象操作文件和目录 .
			
来源:http://blog.csdn.net/zdingxin/article/details/6635376 在AIR中可以方便的对本地文件操作,不过上次做了个项目,发现还是有不少不方便的地方,比 ...
 - 正则匹配 sql语句参数
			
List<string> listcommand = new List<string>(); string sql = "update BMDMB set bmdmb ...
 - 十四、oracle 数据库管理--管理表空间和数据文件
			
一.概念表空间是数据库的逻辑组成部分.从物理上讲,数据库数据存放在数据文件中:从逻辑上讲,数据库数据则是存放在表空间中,表空间由一个或多个数据文件组成. 二.数据库的逻辑结构oracle中逻辑结构包括 ...
 - Oracle字符串操作[转:http://www.cnblogs.com/xd502djj/archive/2010/08/11/1797577.html]
			
ORACLE 字符串操作 1 字符串连接 SQL> select 'abc' || 'def' from dual; 'ABC'|------abcdef 2 小写SQL>select ...
 - Android状态选择器用法总结
			
原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6284682.html 本文首先列出常见状态选择器的创建,然后按照常用控件来分别列出状态选择器的具体 ...
 - C# 反射相关的东西
			
public class PlugingManager { //插件装载器 public ArrayList Plugins = new ArrayList(); ...
 - Sublime Text: [Decode error - output not utf-8]
			
今天编译Python时, 输出窗口信息出现: [Decode error - output not utf-8][Decode error - output not utf-8] 发现是print ...