Making the Grade
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6445   Accepted: 2994

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


和那道CF#371(div.2)E一样,只是不严格单增单减各一遍
//
// main.cpp
// poj3666
//
// Created by Candy on 9/22/16.
// Copyright © 2016 Candy. All rights reserved.
// #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=,INF=1e9+;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,a[N],mp[N],k,ans=INF;
int f[N][N];
void dp(){
sort(mp+,mp++n);
for(int i=;i<=n;i++){
int mn=INF;
for(int j=;j<=k;j++){
mn=min(mn,f[i-][j]);
f[i][j]=mn+abs(a[i]-mp[j]);
}
}
for(int j=;j<=k;j++) ans=min(ans,f[n][j]); for(int i=;i<=n;i++){
int mn=INF;
for(int j=k;j>=;j--){
mn=min(mn,f[i-][j]);
f[i][j]=mn+abs(a[i]-mp[j]);
}
}
for(int j=;j<=k;j++) ans=min(ans,f[n][j]);
}
int main(int argc, const char * argv[]) {
n=read();
for(int i=;i<=n;i++){
a[i]=mp[i]=read();
}
k=unique(mp+,mp++n)-mp-;
dp();
printf("%d",ans);
return ;
}

POJ3666Making the Grade[DP 离散化 LIS相关]的更多相关文章

  1. Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]

    E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

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

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

  3. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  4. 【题解】Making The Grade(DP+结论)

    [题解]Making The Grade(DP+结论) VJ:Making the Grade HNOI-D2-T3 原题,禁赛三年. 或许是我做过的最简单的DP题了吧(一遍过是什么东西) 之前做过关 ...

  5. CF13C Sequence(DP+离散化)

    题目描述 给定一个序列,每次操作可以把某个数+1-1.要求把序列变成非降数列.求最少的修改次数. 输入输出样例 输入 #1 - 输出 #1 4 输入 #2 输出 #2 1 解题思路 这题是一道非常好题 ...

  6. B. Once Again... 解析(思維、DP、LIS、矩陣冪)

    Codeforce 582 B. Once Again... 解析(思維.DP.LIS.矩陣冪) 今天我們來看看CF582B 題目連結 題目 給你一個長度為\(n\)的數列\(a\),求\(a\)循環 ...

  7. POJ3666 Making the Grade [DP,离散化]

    题目传送门 Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9090   Accepted: ...

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

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7068   Accepted: 3265 ...

  9. poj3666 Making the Grade(基础dp + 离散化)

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

随机推荐

  1. JS的window.location应用实例

    window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面. Window Locationwindow.location 对象在编写时可不使用 window ...

  2. 编写jquery常用插件的基本格式

    写jquery插件需要明白的那些知识点 1.jquery中$是神马?$.fn又是神马? 稍微有jquery经验的都知道在jquery中$等价于jQuery,在控制台一试便知: 我们在jquery(1. ...

  3. EXCEL经纬度转化

    =LEFT(A1,FIND("°",A1)-1)+MID(A1,FIND("°",A1)+1,FIND(CHAR(39),A1)-1-FIND("°& ...

  4. Arcgis创建SDE_Geometry、SDO_Geometry的区别【转】

    1. SDO_GEOMETRY Oracle Spatial在MDSYS模式下定义了一系列几何类型.函数来支持空间数据的存储和使用,最为人耳熟能详的就是SDO_GEOMETRY这种类型——当然,Arc ...

  5. 服务 {49A27252-A326-4EF1-B698-6EBC7068833C} 的计时器作业 id {573BE459-DF82-481C-84BD-CA14D287450B} 配置刷新的上一个实例仍在运行,因此将跳过当前的实例。请考虑增加作业之间的时间间隔。

    在SharePoint2007的错误日志中发现大量如下错误: 07/02/2013 16:17:25.99     OWSTIMER.EXE (0x0958)     0x097C    Window ...

  6. Android Studio常用快捷键

    Ctrl+U :如果你的光标在重写父类的一个方法内(如:Activity#onCreate()),这个将会跳到父类的实现上. 如果你的光标在类名上,它将会跳到父类. Ctrl+Alt+Home:它可以 ...

  7. 之一:CABasicAnimation - 基本动画

    嗷呜嗷呜嗷呜 // 将视图作为属性方便后面执行多个不同动画 _myView = [[UIView alloc] init]; _myView.layer.position = CGPointMake( ...

  8. 关于iOS中的时间

    两类 绝对时间 [NSDate date].CFAbsoluteTimeGetCurrent(),或者gettimeofday(). 返回的是从某一个时刻开始,度过的秒数.会随着用户设置的系统时间更改 ...

  9. phonegap创建的ios项目推送消息出现闪退现象

    使用phonegap创建的ios项目,推送消息时,当程序在前台运行或者在后台运行状态下,推送消息过来,可以解析并且跳转: 但是在程序从后台退出的状态下,当消息推送过来的时候,点击通知栏,打开程序,程序 ...

  10. iOS开发之百度地图的集成——地图标注&POI检索

    本篇分为两部分: 一.地图标注 第一步:首先创建 BMKMapView 视图 第二步:在视图完全显示出来后设置,并实现代理方法 第三步:运行程序,此时大头针效果可以正常显示 二.POI检索 第一步:延 ...