kaungbin_DP S (POJ 3666) Making the Grade
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 显然这题的难点在于抉择第i点到底提升自己还是降低之前的
那么干脆就把所有可能考虑到 用dp[i][j]表示 第i点以j结尾的最小cost
但是题中给的数据量来看 这个数组实在太大 所以再加上离散化 那么就是O(n^2)的方法了 这题数据很水 只要非降序就能过
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std; int n, arry[], cast[];
int dp[][]; int main()
{
ios::sync_with_stdio(false);
while(cin >> n){
for(int i = ; i < n; ++i){
cin >> arry[i];
}
memcpy(cast, arry, sizeof arry);
sort(cast, cast + n); for(int i = ; i < n; i++){
dp[][i] = abs(arry[] - cast[i]);
} for(int i = ; i < n; i++){
int mini = dp[i-][];
for(int j = ; j < n; j++){
mini = min(dp[i-][j], mini);
dp[i][j] = abs(arry[i] - cast[j]) + mini;
}
} cout << *min_element(dp[n-], dp[n-] + n) << endl;
}
return ;
}
kaungbin_DP S (POJ 3666) Making the Grade的更多相关文章
- 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)
题目链接:http://poj.org/problem?id=3666 题目大意:给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调不降或者单调不增(题目BUG,只能求 ...
- POJ 3666 Making the Grade
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- 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 (动态规划)
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
- 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 : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) T ...
随机推荐
- Error Domain=ASIHTTPRequestErrorDomain Code=8 "Failed to move file from"xxx/xxx"to"xxx/xxx"
今天真的好高兴呀 我解决了一个折磨了我一周的问题,真的是激动地要哭出来了,为了这个问题,我嘴也烂了,头发抓了一地啊.虽然解决方法,最后还是展现出了“百度”的伟大,但是我还是很开心,在这里我展示一下我的 ...
- JAVA 内部类 泛型 实现堆栈
堆栈类: package c15; public class LinkedStack<T> { private static class Node<T> { T item ; ...
- AFNetworking之缓存篇
苦苦看了我两天,最近后台为了减轻压力,要我做缓存,我说好吧...... 借鉴了别人的说法找到一张图可以看明白好多: 这个是我比较战成一种方案. 好了直接上代码了 首先我们要有自己缓存的类 ...
- 一步一步搭框架(asp.netmvc+easyui+sqlserver)-01
一步一步搭框架(asp.netmvc+easyui+sqlserver)-01 要搭建的框架是企业级开发框架,适用用企业管理信息系统的开发,如:OA.HR等 1.框架名称:sampleFrame. 2 ...
- LED BIN code
在LED选型时,一开始我们都知道要选个某种颜色,在选个尺寸,但在到具体选的时候,就会被那些个各种各样的参数搞得迷糊,这个主要对一个最让人困惑的参数-BIN CODE做整理. BIN就是bining的缩 ...
- android基础(五)网络数据解析方法
在网络上传输数据时最常用的方法有两种:XML和JSON,下面就对这两种类型的数据解析进行讲解. 一.XML数据解析 在Android中,常见的XML解析器分别为SAX解析器.DOM解析器和PULL解析 ...
- UCanCode发布升级E-Form++可视化源码组件库2015全新版 (V23.01)!
2015年4月. 成都 UCanCode发布升级E-Form++可视化源码组件库2015全新版 (V23.01)! --- 全面性能提升,UCanCode有史以来最强大的版本发布! E-Form++可 ...
- 数据结构《17》---- 自动补齐之《二》----Ternary Search Tree
一. 序言 上一篇文章中,给出了 trie 树的一个实现.可以看到,trie 树有一个巨大的弊病,内存占用过大. 本文给出另一种数据结构来解决上述问题---- Ternary Search Tree ...
- 关于npm
转载自AlloyTeam:http://www.alloyteam.com/2016/03/master-npm/ 这是我学npm觉得最好的一篇文章啦-大家一起学起来吧 npm本来是Node.js的包 ...
- inux如何查看当前占用CPU或内存最多的进程
一.可以使用以下命令查使用内存最多的进程 方法1: ps -aux | sort -k4nr | head -K 如果是10个进程,K=10,如果是最高的三个,K=3 说明:ps -aux中(a指代a ...