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. arcgis api for js入门开发系列十九图层在线编辑

    本篇主要讲述的是利用arcgis api实现图层在线编辑功能模块,效果图如下: 实现思路: 1.arcgis server发布的FeatureServer服务提供的图层在线编辑能力: 2.实现的在线编 ...

  2. Android Studio教程06-布局,监听器以及基本控件

    目录 2. 监听器 3. 布局 3.1. 布局分类 (1). Linear Layout (2). Relative Layout (3). ListView (4). Grid View 4. 其他 ...

  3. Java中的守护线程

    守护线程的概念 在java中有两种线程,守护线程和非守护线程,其两者并没有本质的区别,唯一的区别就是当前的用户线程退出的时候,若只存在唯一的A线程,若A线程为守护线程,那么JVM将会直接退出,否则JV ...

  4. Jmeter接口测试实战-数据传递

    Jmeter接口测试实战-数据传递 接口与接口之间没有关联的测试是缺乏意义和没有灵魂的,只有数据在不同接口之间传递才能勾画出业务场景重要的链路. 我们用较为通用的http/https协议,接口普遍返回 ...

  5. Vue组织架构图组件

    vue-tree-chart   :deciduous_tree: Vue2树形图组件 安装 npm i vue-tree-chart --save 使用 in template: <TreeC ...

  6. angularjs兼容thickbox 插件

    ThickBox是一个基于JQuery类库的扩展,它能在浏览器界面上显示非常棒的UI框, 它可以显示单图片,多图片,ajax请求内容或链接内容.ThickBox 是用超轻量级的 jQuery 库 编写 ...

  7. Pycharm 常用快捷键

    常用快捷键 快捷键 功能 Ctrl + Q 快速查看文档 Ctrl + F1 显示错误描述或警告信息 Ctrl + / 行注释(可选中多行) Ctrl + Alt + L 代码格式化 Ctrl + A ...

  8. java-retry实现

    有这样一个需求,当调用某个方法抛出异常,比如通过 HttpClient 调用远程接口时由于网络原因报 TimeOut 异常:或者所请求的接口返回类似于“处理中”这样的信息,需要重复去查结果时,我们希望 ...

  9. 一起刷LeetCode

    题目列表: 题目 解答 26. 删除排序数组中的重复项 https://www.cnblogs.com/powercai/p/10791735.html 25. k个一组翻转链表 https://ww ...

  10. 使用axios 的post请求下载文件,

    axios({ method: 'post', data: param, responseType:'blob', url: _urls + '/Downloaddata' }).then(data= ...