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

|A1 - B1| + |A2 - B2| + ... + |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.

题意:给你一个序列,求最少的代价让其变成单调不递增序列或单调不递减序列。

看到这道题目应该会想到用dp处理,dp[i][max]表示处理到前i位的最大值位max,于是只要两个for就可以,i=1~n,j=1~max。
dp[i][j]=min(dp[i][1~j]) + abs(a[i]-j),然后再从dp[n][1~max]中找最小的。
这题数据有点大如果用max会TLE,所以要将数据离散化一下。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define Abs(a) ((a)>0?(a):-(a))
using namespace std;
typedef long long ll;
const int M = 2e3 + 10;
ll dp[M][M];
ll a[M] , b[M] , c[M];
int main()
{
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++) {
cin >> a[i];
c[n + 1 - i] = a[i];
b[i] = a[i];
}
sort(b + 1 , b + n + 1);
for(int i = 1 ; i <= n ; i++) {
ll MIN = dp[i - 1][1];
for(int j = 1 ; j <= n ; j++) {
MIN = min(MIN , dp[i - 1][j]);
dp[i][j] = MIN + Abs((a[i] - b[j]));
}
}
ll ans = dp[n][1];
for(int i = 1 ; i <= n ; i++) {
ans = min(ans , dp[n][i]);
}
memset(dp , 0 , sizeof(dp));
for(int i = 1 ; i <= n ; i++) {
ll MIN = dp[i - 1][1];
for(int j = 1 ; j <= n ; j++) {
MIN = min(MIN , dp[i - 1][j]);
dp[i][j] = MIN + Abs((c[i] - b[i]));
}
}
for(int i = 1 ; i <= n ; i++) {
ans = min(ans , dp[n][i]);
}
cout << ans << endl;
return 0;
}

poj3666 Making the Grade(基础dp + 离散化)的更多相关文章

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

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

  2. poj3666/CF714E/hdu5256/BZOJ1367(???) Making the Grade[线性DP+离散化]

    给个$n<=2000$长度数列,可以把每个数改为另一个数代价是两数之差的绝对值.求把它改为单调不增or不减序列最小代价. 话说这题其实是一个结论题..找到结论应该就很好做了呢. 手玩的时候就有感 ...

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

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

  4. [poj3666]Making the Grade(DP/左偏树)

    题目大意:给你一个序列a[1....n],让你求一个序列b[1....n],满足 bi =a && bc,则最小的调整可以是把b变成c. 所以归纳可知上面结论成立. dp[i][j] ...

  5. 「kuangbin带你飞」专题十二 基础DP

    layout: post title: 「kuangbin带你飞」专题十二 基础DP author: "luowentaoaa" catalog: true tags: mathj ...

  6. 基础dp

    队友的建议,让我去学一学kuangbin的基础dp,在这里小小的整理总结一下吧. 首先我感觉自己还远远不够称为一个dp选手,一是这些题目还远不够,二是定义状态的经验不足.不过这些题目让我在一定程度上加 ...

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

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

  8. 基础DP(初级版)

    本文主要内容为基础DP,内容来源为<算法导论>,总结不易,转载请注明出处. 后续会更新出kuanbin关于基础DP的题目...... 动态规划: 动态规划用于子问题重叠的情况,即不同的子问 ...

  9. hdu 5586 Sum 基础dp

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Desc ...

随机推荐

  1. centos6.5-7编译安装Ansible详细部署

    一.基础介绍==========================================================================================ansi ...

  2. .net持续集成测试篇之Nunit常见断言

    系列目录 Nunit测试基础之简单断言 在开始本篇之前需要补充一些内容,通过前面搭建Nunit测试环境我们知道要使一个方法成为单元测试方法首先要在此方法所在类加上TestFixture注解,并且在该方 ...

  3. go 学习笔记之工作空间

    搭建好 Go 的基本环境后,现在可以正式开始 Go 语言的学习之旅,初学时建议在默认的 GOPATH 工作空间规范编写代码,基本目录结构大概是这个样子. . |-- bin | `-- hello.e ...

  4. 恐怖的Hibernate和JavaFX Table CallBack!

    目录 [隐藏] 1 Hibernate 2 JavaFX Table Hibernate 最近在做 JavaFX 应用,不管再怎么避免数据持久化,但面对几十万的数据量的时候也只能乖乖的去配置持久层框架 ...

  5. maven 打包并导出 lib 第三方jar

    一. maven 导出lib 包 执行命令 mvn dependency:copy-dependencies -DoutputDirectory=target/lib 或者在 eclipse 中执行, ...

  6. .Net异步编程详解入门

    前言 今天周五,早上起床晚了.赶着挤公交上班.但是目前眼前有这么几件事情.刷牙洗脸.泡牛奶.煎蛋.在同步编程眼中.先刷牙洗脸,然后烧水泡牛奶.再煎蛋,最后喝牛奶吃蛋.毫无疑问,在时间紧促的当下.它完了 ...

  7. APPCAN   版本控制SVN

      1.检出代码 checkout 常规的操作     appcan 中,在官网新建一个项目后,就会有一项目的svn 地址,而且已经是主干分支了,这个是项目的位移目录,不能再trunk目录同级创建分子 ...

  8. 一文读懂JS中的原型和原型链(图解)

    讲原型的时候,我们应该先要记住以下几个要点,这几个要点是理解原型的关键: 1.所有的引用类型(数组.函数.对象)可以自由扩展属性(除null以外). 2.所有的引用类型都有一个’_ _ proto_ ...

  9. 使用mybatis实现分页查询示例代码分析

    *******************************************分页查询开始*************************************************** ...

  10. element ui 登录验证,路由守卫

    <template> <!-- el-form :label-position="labelPosition" 设置label的位置 :model 用来给表单设置 ...