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. ruby(&gem) koala安装

    1.ruby下载安装 下载地址:http://railsinstaller.org/en 选择合适版本 2.gem(安装ruby时自带gem) 删除原镜像:gem sources --remove h ...

  2. HTML5移动开发学习笔记

    最近做webapp项目过程中,发现坑还是挺多的,以下是网络收集一些开发中的常见问题及知识汇总,以便查阅,慢慢更新:). meta 基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 < ...

  3. iOS 3DES加密解密(一行代码搞定)

    3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称.它相当于是对每个数据块应用三次DES加密算法.由于计 ...

  4. .NET破解之图片下载器

    自去年五月加入吾爱后,学习了三个月,对逆向破解产生了深厚的兴趣,尤其是对.NET方面的分析:但由于这一年,项目比较忙,事情比较多,破解这方面又停滞了许久,不知道还要好久. 前些天,帮忙批量下载QQ相册 ...

  5. 将Win10变回Win7/WinXP界面

    前往 Classic Shell 的网站(传送门:http://www.classicshell.net/)进行下载安装.第一次开启 时,程序会让你选择一款面板:第一个是 Windows 2000 的 ...

  6. Snort - manual 笔记(五)

    1.9 Miscellaneous 1.9.1 Running Snort as a Daemon 如果你想让Snort作为守护程序运行,你可以在最后加上 -D 选项.清注意如果你想通过发送一个 SI ...

  7. iOS界面传值的方式(7种)

    iOS传值的方式 属性传值 方法传值 代理传值(delegate) block传值 单例模式方式 通知notification方式 UserDefault或者文件方式 1.属性传值 情况:A页面跳转到 ...

  8. [android]AndroidInject框架——我的第一个android小型框架

    作为一个移动应用开发者,随着需求的日益增多,Android项目的越来越臃肿,代码量越来越大, 现在冷静下来回头看看我们的代码,有多少代码跟业务逻辑没什么关系的 所以,本人自不量力,在github上建了 ...

  9. Xcode cannot launch because the device is locked.

    When you plug in your iPhone, it will ask you to trust the computer. If you already trust and unlock ...

  10. 【原】iOS设计模式之:建造者模式Builder Pattern,用于改进初始化参数

    本文主要讨论一下iOS中的Builder Pattern.与网上很多版本不同,本文不去长篇大论地解释建造者模式的概念,那些东西太虚了.设计模式这种东西是为了解决实际问题的,不能为了设计模式而设计模式, ...