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 个数中,最大 ...
随机推荐
- XueTr 0.45 (手工杀毒辅助工具) 绿色版
软件名称: XueTr 0.45 (手工杀毒辅助工具)软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 3.3MB图片预览: ...
- 10.按要求编写Java应用程序。 (1)创建一个叫做People的类: 属性:姓名、年龄、性别、身高 行为:说话、计算加法、改名 编写能为所有属性赋值的构造方法; (2)创建主类: 创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80; 让该对象调用成员方法: 说出“你好!” 计算23+45的值 将名字改为“李四”
package com.hanqi.test; public class People { private String name,sex; private int age; private doub ...
- JPA 系列教程14-自定义类型-@Embedded+@Embeddable
自定义类型 在hibernate中实现自定义类型,需要去实现UserType接口即可或者以Component的形式提供. JPA的@Embedded注解有点类似,通过此注解可以在Entity模型中使用 ...
- nmon的安装与使用
nmon的安装与使用 1.下载 nmon:http://nmon.sourceforge.net/pmwiki.php?n=Site.Download nmonanalyser http://www. ...
- mysql 常用命令用法总结积木学院整理版
一.启动与退出 1.进入MySQL:启动MySQL Command Line Client(MySQL的DOS界面),直接输入安装时的密码即可.此时的提示符是:mysql> 2.退出MySQL: ...
- ASP.NET弹出提示点击确定之后再跳转页面的方法
//ASP.NET弹出提示点击确定之后再跳转页面的方法 //弹出了提示并且通过location.href转到了DeskTop.aspx页面 Response.Write("<scrip ...
- 利用朴素贝叶斯算法进行分类-Java代码实现
http://www.crocro.cn/post/286.html 利用朴素贝叶斯算法进行分类-Java代码实现 鳄鱼 3个月前 (12-14) 分类:机器学习 阅读(44) 评论(0) ...
- hdu_2243_考研路茫茫——单词情结(AC自动机+矩阵)
题目链接:hdu_2243_考研路茫茫——单词情结 题意: 让你求包含这些模式串并且长度不小于L的单词种类 题解: 这题是poj2788的升级版,没做过的强烈建议先做那题. 我们用poj2778的方法 ...
- hdu_5293_Tree chain problem(DFS序+树形DP+LCA)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5293 被这题打蹦了,看着题解写的,很是爆炸,确实想不到,我用的DFS序+LCA+树形DP,当然也可以写 ...
- OPENWRT make defconfig错误之一
make defconfig rm: cannot remove `tmp/.host.mk': Permission denied 退到trunk上级目录 su root sudo chown -R ...