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
Source
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(x,i,j) cout<<#x<<"["<<i<<"]["<<j<<"] = "<<x[i][j]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
int num[],p[];
int n;
int dp[][];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&num[i]);
p[i]=num[i];
}
sort(p+,p++n);
int m=unique(p+,p++n)-p-;
for(int i=;i<=n;i++){
int t=lower_bound(p+,p++n,num[i])-p-;
for(int j=;j<=m;j++){
dp[i][j]=inf;
for(int k=;k<=j;k++){
dp[i][j]=min(dp[i-][k]+abs(num[i]-p[k]),dp[i][j]);
}
}
}
printf("%d\n",dp[n][m]);
return ;
}
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(x,i,j) cout<<#x<<"["<<i<<"]["<<j<<"] = "<<x[i][j]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
int num[],p[];
int n;
int dp[][];
int minn[];
int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&num[i]);
p[i]=num[i];
}
sort(p+,p++n);
int m=unique(p+,p++n)-p-;
for(int i=;i<=n;i++){
int t=lower_bound(p+,p++n,num[i])-p-;
minn[]=inf;
for(int j=;j<=m;j++){
minn[j]=min(minn[j-],dp[i-][j]+abs(num[i]-p[j]));
}
for(int j=;j<=m;j++){
dp[i][j]=minn[j];
}
}
printf("%d\n",dp[n][m]);
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
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)
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 ...
随机推荐
- python 面试题
1.os.path与sys.path的区别是什么? os.path 主要用于系统文件路径的操作 sys.path 主要是python解释器的系统环境参数的操作 2.re模块中match和search方 ...
- async await详解
async await本身就是promise + generator的语法糖. 本文主要讲述以下内容 async awiat 实质 async await 主要特性 async await 实质 下面 ...
- Python查找指定文件
在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径: import os testfiles = [] testfilepaths = [] L = len(os.p ...
- android常犯错误记录(三)
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionVie ...
- maven+springMVC(二)
[目录]
- Rabbitmq集群高可用
转载:https://www.cnblogs.com/flat_peach/archive/2013/04/07/3004008.html RabbitMQ是用erlang开发的,集群非常方便,因为e ...
- pytorch的函数中的group参数的作用
1.当设置group=1时: conv = nn.Conv2d(in_channels=, out_channels=, kernel_size=, groups=) conv.weight.data ...
- 在Winform开发框架中下拉列表绑定字典以及使用缓存提高界面显示速度
在我们开发Winform界面的时候,往往需要绑定数据字典操作,也就是绑定一些下拉列表或者一些列表显示等,以便我们方便选择数据操作,常见的字典绑定操作就是对下拉列表的处理,本篇随笔是基于DevExpre ...
- JS 转换HTML转义符
JS转换HTML转义符 //去掉html标签 1 2 3 function removeHtmlTab(tab) { return tab.replace(/<[^<>]+?& ...
- pycharm 远程调试代码
我们在本地开发的时候,有时候需要使用到远程服务器的环境,如我们在调试微信或支付宝支付的时候. 那我们如何通过本地pycharm环境连接远程服务器进行调试呢? 1.pycharm和远程服务器连接 1)点 ...