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 ;
}
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,只能求 ...
- 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 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 ...
随机推荐
- webstorm 10 更改默认端口
快捷键 ctrl + Alt + s :打开配置 如图下可以更改 默认端口.快捷键可能跟qq的快捷键冲突,可以关闭qq来设置端口
- C# treeview 绑定数据 【转】
private void bindTreeView1() { string sql = "select * from dm_category"; DataTable dt = db ...
- c++继承,多态,重载的作用
继承:用户通过继承,可以从一个类派生出多个子类,减少了重新定义类的次数,并且在不影响子类的相应功能的情况下,保护了基类中的数据安全性.用户还可以在子类中,使用与基类相同的行为实现不同的功能,因此,使用 ...
- Linux 命令行快捷键
说明 Ctrl – k: 先按住 Ctrl 键,然后再按 k 键: Alt – k: 先按住 Alt 键,然后再按 k 键: M – k:先单击 Esc 键,然后再按 k 键. 移动光标 Ctrl – ...
- Struts 2 Spring Hibernate三大框架的执行流程以及原理
Struts2框架 一.简介 Struts2是一个相当强大的Java Web开源框架,是一个基于POJO的Action的MVC Web框架.它基于当年的WebWork和XWork框架,继承其优点,同时 ...
- PHP之数组array
$switching = array( 10, // key = 0 5 => 6, 3 ...
- Jquery.Form和jquery.validate 的使用
有些功能需要我们利用Ajax技术进行POST提交表单,这时候就需要用到jquery.Form ,它有两种方式进行提交, AjaxForm和AjaxSubmit方式. AjaxFo ...
- Python 基礎 - 淺copy補充說明
在 import copy 這個模塊裡 基於第一個列表來做淺copy,實際上第二個列表裡的元素,是第一個列表的 引用. 接下來介紹 淺copy有三種方式可以使用 #!/usr/bin/env pyth ...
- OGRE启动过程详解(OGRE HelloWorld程序原理解析)
本文介绍 OGRE 3D 1.9 程序的启动过程,即从程序启动到3D图形呈现,背后有哪些OGRE相关的代码被执行.会涉及的OGRE类包括: Root RenderSystem RenderWindow ...
- Elasticsearch Network Settings
网络设置 Elasticsearch 缺省情况下是绑定 localhost.对于本地开发服务是足够的(如果你在相同机子上启动多个节点,它还可以形成一个集群),但是你需要配置基本的网络设置,为了能够在实 ...