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

|AB1| + |AB2| + ... + |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

 
题意:
将所给数组中的某个数字加上或者减去某个数,使数组变为非降数组,问所需最小花费。
思路:
允许数组中的数字相等,那么最后最优解不会出现除了输入以外的数字,所以可以将输入的数字离散化。
dp[i][j]表示,将第i个数字,变成第j大的数字所需的最小花费。j实际上就是离散化之后的数组的下标。
dp[i][j]=min(dp[i-1][k]+abs(num[i]-p[k]),dp[i][j]);
其中num是原高度,p是离散化后的数组。k<=j;
但是这样的复杂度是n的三次方,不过还好我们可以用一个数组记录下j之前dp[i-1][k]+abs(num[i]-p[k])的最小值,这样就能优化成n方了。
TLE
#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 ;
}
AC
#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 (动态规划)的更多相关文章

  1. Poj 3666 Making the Grade (排序+dp)

    题目链接: Poj 3666 Making the Grade 题目描述: 给出一组数,每个数代表当前位置的地面高度,问把路径修成非递增或者非递减,需要花费的最小代价? 解题思路: 对于修好的路径的每 ...

  2. POJ 3666 Making the Grade(数列变成非降序/非升序数组的最小代价,dp)

    传送门: http://poj.org/problem?id=3666 Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total ...

  3. POJ - 3666 Making the Grade(dp+离散化)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  4. POJ 3666 Making the Grade(二维DP)

    题目链接:http://poj.org/problem?id=3666 题目大意:给出长度为n的整数数列,每次可以将一个数加1或者减1,最少要多少次可以将其变成单调不降或者单调不增(题目BUG,只能求 ...

  5. 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 ...

  6. POJ 3666 Making the Grade

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  7. poj 3666 Making the Grade(dp)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  8. poj 3666 Making the Grade(离散化+dp)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  9. POJ 3666 Making the Grade (线性dp,离散化)

    Making the Grade Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) T ...

随机推荐

  1. linux子系统折腾记 (二)

    今天一早起床,打开debian,居然出现 错误: 0x80070040 .不知道是怎么回事,网上有篇文章详细介绍了windows linux子系统,打算参考来做做:https://www.jiansh ...

  2. Powershell Linux正式版可用,启动名称有变

    CentOS yum install powershell 但并没有powershell这个可执行文件.通过搜索可以发现在powershell目录里有pwsh可执行文件,那么以后就要用pwsh执行了. ...

  3. UE4游戏开发基础命令

    在个人的Unrealengine账户中关联自己的GitHub账户成功之后,就可以访问UE4引擎的源码了. git clone -b release https://github.com/EpicGam ...

  4. c/c++ 多线程 ubuntu18.04 boost编译与运行的坑

    多线程 boost编译与运行的坑 背景:因为要使用boost里的多线程库,所以遇到了下面的坑. 系统版本:ubuntu18.04 一,安装boost 1,去boost官网下载 boost_1_XX_0 ...

  5. mysql export mysqldump version mismatch upgrade or downgrade your local MySQL client programs

    I use MySQL Community Edition and I solved this problem today. goto https://dev.mysql.com/downloads/ ...

  6. wireshark抓包,安装及简单使用

    跟着实验室师兄尝试做流量分析,趁着离期末考试还有几天,尽快把环境搭好. 采集:自动化测试monkeyrunner,ok 抓包 charles/Wireshark,ok 限制其他应用运行App Moun ...

  7. Mysql 字符串指定位置插入空格

    UPDATE flow_data_243 SET data_15=CONCAT(LEFT(data_15,10),' ',RIGHT(data_15,LENGTH(data_15)-10)) WHER ...

  8. Linux命令之常用篇

    一.文件和目录 1. cd命令 它用于切换当前目录,它的参数是要切换到的目录的路径,可以是绝对路径,也可以是相对路径. 指令 说明 cd /home 进入‘home’目录 cd .. 返回上一级目录 ...

  9. centos7的内核区别

    最近重新搭建环境准备测试一些东西,在网上随意下载了一个镜像,名字叫做:CentOS-7-i386-Everything-1810 下载完之后开始做实验安装软件的时候发现一直报错:[Errno 14] ...

  10. 性能测试中TPS上不去的几种原因浅析

    转:https://www.cnblogs.com/imyalost/p/8309468.html 下面就说说压测中为什么TPS上不去的原因: 1.网络带宽 在压力测试中,有时候要模拟大量的用户请求, ...