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. flex-手机端主页布局实例---构造页面结构

    Flexbox页面布局实例,成本效果图如下, 源码下载在最下面. 源码下载:https://pan.baidu.com/s/18o5hVuWtflUpgvMk3LzQ5w  提取码:wiyc样本地址: ...

  2. java 线程方法 ---- sleep()

    class MyThread implements Runnable{ @Override public void run() { for (int i = 0; i < 5; i++){ Sy ...

  3. Android ViewPager+Fragment 在Activity中获取Fragment的控件

    如果ViewPager+Fragment实现Tab切换,在activity中利用adapter.getItem获取到fragment然后再根据fragment.的方法获取控件 //隐藏求租,以下代码用 ...

  4. PM过程能力成熟度3级

    2级PM已经可以把项目管理起来了.如果这时候,企业只有一个PM,那这种程度的管理,只要逐渐深化,就可以一直维持下去. 然鹅,现实总是复杂很多... 公司会在PM建立2级的过程管理后,提高所有项目的可视 ...

  5. simulink创建简单模型

    创建简单模型 您可以使用 Simulink® 对系统建模,然后仿真该系统的动态行为.Simulink 允许您创建模块图,图中的各个连接模块代表系统的各个部分,信号代表这些模块之间的输入/输出关系.Si ...

  6. 【已采纳】charles工具使用心得

    1.下载charles 可以去charles官网下载,下载地址:http://www.charlesproxy.com/download/    根据自己的操作系统下载对应的版本,然后进行安装,然后打 ...

  7. DVWA 黑客攻防实战(十五) 绕过内容安全策略 Content Security Policy (CSP) Bypass

    看到标题,是否有点疑惑 CPS 是什么东东.简单介绍一下就是浏览器的安全策略,如果 标签,或者是服务器中返回 HTTP 头中有 Content-Security-Policy 标签 ,浏览器会根据标签 ...

  8. Git 密钥对处理

    生成密钥对: ssh-keygen -t rsa cd .ssh ls      id_rsa          私钥      id_rsa.pub   公钥

  9. Java基础系列--07_String、StringBuffer和StringBuilder

    String类  (1)字符串:字符串是常量:它们的值在创建之后不能更改,存储在堆中.          如果字符串多次赋值,其实是每次重新赋值的时候程序都先在内存中寻找已开辟的空间是否存在该值;如果 ...

  10. Django入门之路

    Web框架开发-Django基础之web应用,Http协议 web框架开发-web框架简介,wsgiref模块,DIY一个web框架 web框架开发-Django简介 web框架开发-静态文件配置 w ...