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. 曹工杂谈:Linux服务器上,Spring Boot 原地修改 jar 包配置文件/替换class文件,免去重复上传的麻烦

    一.前言 相信很多同学有这样的需求,现在很多公司都有多地的研发中心,经常需要跨地区部署,比如,博主人在成都,但是服务器是北京的.一般城市间网络都不怎么好,上传一个几十兆的jar包那是真的慢,别说现在微 ...

  2. go interface衍生的插件化处理

    在设计程序的许多应用场景中我们会遇到大体分为三个阶段的任务流. 第一.入口 一个或多个入口,等待阻塞的.或者主动请求方式的. ============================== 比如任务流需 ...

  3. S2:面向对象

    面向对象七大设计原则 1. 开闭原则 2. 里氏替换原则 3. 单一职责原则 4. 接口隔离原则 5. 依赖倒置原则 6. 迪米特原则 7.组合/聚合复用原则 原则一:(SRP:Single resp ...

  4. 限流降级神器,带你解读阿里巴巴开源 Sentinel 实现原理

    Sentinel 是阿里中间件团队开源的,面向分布式服务架构的轻量级高可用流量控制组件,主要以流量为切入点,从流量控制.熔断降级.系统负载保护等多个维度来帮助用户保护服务的稳定性. 大家可能会问:Se ...

  5. Spring Cloud 相关资料链接

    Spring Cloud中文网:https://springcloud.cc/ Spring Cloud API:https://springcloud.cc/spring-cloud-dalston ...

  6. 算法实战-OJ之旅

    算法虽然不是特别简单,但没有你想象中的那么难. Sort Array By Parity easy AC-17ms. 按照<算法导论>排序一章的一些概念,第二种可以称为是原址的(in-pl ...

  7. print('', end='')

    print函数的end参数,从python3才开始支持,所以如果要使用这种模式,需要对应使用python3

  8. java并发编程(一)----线程基础知识

    在任何的生产环境中我们都不可逃避并发这个问题,多线程作为并发问题的技术支持让我们不得不去了解.这一块知识就像一个大蛋糕一样等着我们去分享,抱着学习的心态,记录下自己对并发的认识. 1.线程的状态: 线 ...

  9. Vue项目中使用better-scroll

    当 better-scroll 遇见 Vue   在我们日常的移动端项目开发中,处理滚动列表是再常见不过的需求了. 以滴滴为例,可以是这样竖向滚动的列表,如图所示: 也可以是横向滚动的导航栏,如图所示 ...

  10. LeetCode——409. Longest Palindrome

    题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest ...