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 ...
随机推荐
- web.config 配置
一.认识Web.config文件 Web.config 文件是一个xml文本文件,它用来储存 asp.NET Web 应用程序的配置信息(如最常用的设置asp.NET Web 应用程序的身份验证方 ...
- IOS中的编码规范
1.指导原则 [原则1-]首先是为人编写程序,其次才是计算机. 说明:这是软件开发的基本要点,软件的生命周期贯穿产品的开发.测试.生产.用户使用.版本升级和后期维护等长期过程,只有易读.易维护的软件代 ...
- 硬件抽象层——HAL
(一)为什么要在android中加入HAL Linux系统中Linux驱动有两种类型的代码:访问硬件寄存器的代码——调用的Linux内核的标准函数进行的标准操作 业务逻辑代码——有些企业或个人并不想 ...
- 用Java编程找到两个字符串中共有的字符
这道题的算法思想是把字符串1中的每个字符与字符串2中的每个字符进行比较,遇到共同拥有的字符,放入另一个数组中,最后顺序输出即可 但是这道题的难点在于怎么排除重复的字符 public class bot ...
- 认识Java
java出生地:SUN Microsystems Inc<开源> -SUN : Standford University Network java之父:James Gosling ...
- iOS摇一摇手机,播放微信摇一摇音效
实现微信摇一摇播放音效,代码如下:- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ if (motio ...
- Baskets of Gold Coins_暴力
Problem Description You are given N baskets of gold coins. The baskets are numbered from 1 to N. In ...
- js 小技巧
如果想让js每次加载时,都要执行, 那么在 <script type="text/javascript" >中加一个属性reload="1", &l ...
- 回文数组(Rotate Array (JS))
旋转一个数组. function rotate(array,n){ var l =array.length,a=array.map(function(x){return x}),arr=[]; n=n ...
- Ansible-Tower快速入门-1.概览【翻译】
概览 Tower Ansible-Tower是作为Ansible的一个web接口界面,并采用REST API作为端点接入.通过使用开源的orchestration engine,无论是与你的团队共享操 ...