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. Dotfuscator混淆加密

    混淆加密 1. 需要安装Dotfuscator软件 2. 安装好后打开软件,找到编译好的DLL文件 3. 打开[setting]设置属性,如下图: 把 Disable String Encryptio ...

  2. 给view添加类似系统上拉快捷菜单的手势

    iOS7以后从屏幕最下方上划会滑出快捷菜单,感觉这个效果不错,就想做个类似的效果,这个东西技术含量不高,每次都写一遍的话就太浪费时间了,所以就把它写成了一个分类,用起来会方便一点. demo地址:ht ...

  3. NSNotificationCenter应用总结

    通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,例如 View 加载完后会触发 viewDidLoad.  Apple 还为我们提供了另一种通知响应方式,那就是 NSNot ...

  4. iOS 打电话、发短信、邮件、打开网址、调用应用等合集

    iOS中的很多功能都是非常简单的,几行代码就搞定了,比如打电话.打开网址.发邮件.发短信等,这里总结几个比较常用的: 1.打电话 方式一:最简单最直接的方式:直接跳到拨号界面 NSURL *url = ...

  5. 安卓学习-- RecyclerView简单入门

    一.加入JAR包 第一感觉这个东东,好复杂,没ListView来的快,方便 在项目中加入android-support-v7-recyclerview.jar包 这个包在extras\android\ ...

  6. nginx 的信号控制概述

    <nginx 在ubuntu 上的启动,停止,重启>中的停止和重启命令基本都是用信号来控制的.这是一些简单的信号控制. 在Nginx服务器中,通常情况都是通过对其发送控制信号进行控制的,除 ...

  7. Java基础知识学习(二)

    Java语法基础 数据类型.类型转换.运算符.逻辑运算符.参考C#,基本一致 输入输出 输出 System.out.print("abc"); System.out.printf( ...

  8. npm常用命令小结

    目录(更新于2016.09.23): 1.认识和使用NPM 2.npm包安装模式 3.npm包管理(package的安装.卸载.升级.查看.搜索.发布,其他等) npm install [-g] 本地 ...

  9. Sqlite学习笔记(五)&&SQLite封锁机制

    概述 SQLite虽然是一个轻量的嵌入式数据库,但这并不影响它支持事务.所谓支持事务,即需要在并发环境下,保持事务的ACID特性.事务的原子性,隔离性都需要通过并发控制来保证.那么Sqlite的并发控 ...

  10. [转]CentOS 6.4下PXE+Kickstart无人值守安装操作系统

    一.简介 1.1 什么是PXE PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持 ...