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. 精通 CSS 选择器(二)

    补充了一些之前遗漏掉的选择器以及一些在 Selectors Level 4 中新定义的选择器. 属性选择器不区分大小写 [attribute="value" i],在 Select ...

  2. 【CoreData】 简单地使用

    先介绍一下什么是CoreData —— 它是在iOS5之后出现的一个框架,提供了对象-关系映射(ORM)的功能,既能够将OC对象转化成数据,保存在SQLite数据库文件中,也能将保存在数据库中的数据还 ...

  3. TexturePacker大图还原成小图工具带源码

    TexturePacker是一个把好多小图打成大图的软件,生成的是大图以及小图在大图位置的.plist描述文件,但是不支持把大图还原成小图.网上偷的图一般都是大图和plist,想得到小图比较麻烦,于是 ...

  4. 第二个activity

    Android四大组件 Activity BroadCastReceiver Service ContentProvider 创建第二个Activity 创建第2个Activity的过程 1. 创建c ...

  5. CGAffineTransformMakeRotation 实现旋转

    UIImageView *image = [[UIImageView alloc]init]; image.frame = CGRectMake(50, 50, 200, 200); image.im ...

  6. java jdbc 连接mysql数据库 实现增删改查

    好久没有写博文了,写个简单的东西热热身,分享给大家. jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打 ...

  7. MySQL Proxy

    最近翻看了mysql proxy的资料,特发上来. mysql proxy的推荐使用方式是采用配置文件来完成,当前在命令行的执行如下:mysql-proxy -P 192.168.1.101:3306 ...

  8. MySql 中 case when then else end 的用法

    解释: SELECT                case                   -------------如果    when sex='1' then '男' ---------- ...

  9. junit 使用

    今天用jsoup做了一个‘网络抓取实例’,然而,当作者把junit-4.11.jar 导入项目中,在类中方法上加入@Test,运行时却报错,报错代码如下: java.lang.NoClassDefFo ...

  10. mapreduce流程中的几个关键点

    MapReduce中数据流动    (1)最简单的过程:  map - reduce    (2)定制了partitioner以将map的结果送往指定reducer的过程: map - partiti ...