题目传送门

  

Making the Grade

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9090   Accepted: 4253

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

  分析:显然是用DP来做。当然蒟蒻DP本来就蒻,讲的可能不太清楚。

  根据题意,构造的只能是广义单调数列,那么就考虑单调递增的情况。

  如果要让结果尽可能小,那么肯定要求构造的序列中最大的数maxx最小,同时满足每个位置上构造的数x最小。那么设状态转移方程为dp[i][j],表示当前到了第i个位置,序列中最大的数为j,状态转移方程为dp[i][j]=abs(j-w[i])+min(d[i-1][k]) (k<=j)。当然数据的范围太大,需要离散化。但是三重循环复杂度为O(nm^2),那么每次枚举k时直接在j的循环中设置一个minn=min(min,dp[i-1][j]),把方程改为dp[i][j]=min{abs(j-w[i])+minn},可以将复杂度降低到O(nm),离散化以后就是O(n^2)。讲的肯定听不懂,那就看代码吧。

  Code:

//It is made by HolseLee on 21st May 2018
//POJ 3666
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
#define Fi(i,a,b) for(int i=a;i<=b;i++)
#define Abs(a) ((a)>0?(a):-(a))
using namespace std;
typedef long long ll;
const int N=;
int n,m,a[N],b[N];
ll dp[N][N];
void work()
{
Fi(i,,n){ll mn=dp[i-][];
Fi(j,,n){mn=min(mn,dp[i-][j]);
dp[i][j]=Abs(a[i]-b[j])+mn;}}
ll ans=dp[n][];
Fi(i,,n)ans=min(ans,dp[n][i]);
printf("%lld\n",ans);
}
int main()
{
ios::sync_with_stdio(false);
cin>>n;Fi(i,,n){cin>>a[i];b[i]=a[i];}
sort(b+,b+n+);work();return ;
}

POJ3666 Making the Grade [DP,离散化]的更多相关文章

  1. POJ3666Making the Grade[DP 离散化 LIS相关]

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6445   Accepted: 2994 ...

  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. POJ3666 Making the Grade

    POJ3666 Making the Grade 题意: 给定一个长度为n的序列A,构造一个长度为n的序列B,满足b非严格单调,并且最小化S=∑i=1N |Ai-Bi|,求出这个最小值S,1<= ...

  5. 【题解】Making The Grade(DP+结论)

    [题解]Making The Grade(DP+结论) VJ:Making the Grade HNOI-D2-T3 原题,禁赛三年. 或许是我做过的最简单的DP题了吧(一遍过是什么东西) 之前做过关 ...

  6. CF13C Sequence(DP+离散化)

    题目描述 给定一个序列,每次操作可以把某个数+1-1.要求把序列变成非降数列.求最少的修改次数. 输入输出样例 输入 #1 - 输出 #1 4 输入 #2 输出 #2 1 解题思路 这题是一道非常好题 ...

  7. poj3666 Making the Grade(基础dp + 离散化)

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

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

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

  9. poj3666 Making the grade【线性dp】

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:10187   Accepted: 4724 ...

随机推荐

  1. PostHtml 中的一些有用的插件

    记录一下 PostHtml 中的一些比较有用的插件 参考自PostHtml文档 posthtml-pug 将pug转化成html posthtml-md 将md语法转化为html语法 posthtml ...

  2. maven工程pom.xml报Missing artifact net.sf.jasperreports:jasperreports:jar:6.2.0

    有时maven工程的pom.xml报以下类型错误: Description Resource Path Location TypeMissing artifact net.sf.jasperrepor ...

  3. C语言数据结构-栈

    一.栈的定义 栈(statck)这种数据结构在计算机中是相当出名的.栈中的数据是先进后出的(First In Last Out, FILO).栈只有一个出口,允许新增元素(只能在栈顶上增加). 移出元 ...

  4. 「6月雅礼集训 2017 Day4」寻找天哥

    [题目大意] 给出$n$个三维向量,设当前向量长度为$L$,每次沿着向量等概率走$[0,L]$个长度.一个球每秒半径增加1个长度,直到覆盖位置,每秒耗能为球体积,求总耗能的期望. 设最后半径为R,那么 ...

  5. 【BZOJ】1574: [Usaco2009 Jan]地震损坏Damage

    [算法]搜索 [题意]给定无向图,现在可能有一些点已经被删除,只给出信息是c个点不能到达结点1,求最少的不能到达结点1的个数(含已删除点). [题解] 真是一道奥妙重重的题目. 每个点不能到达结点1, ...

  6. jQuery取值的一些奇奇怪怪的操作

    语法解释:1. $("#select_id").change(function(){//code...});   //为Select添加事件,当选择其中一项时触发2. var ch ...

  7. jQuery右侧悬浮楼层滚动 电梯菜单

    http://www.kaiu.net/effectCon.aspx?id=2198 <!doctype html> <html> <head> <meta ...

  8. TDD随想录

    TDD随想录 谨以本文献给TDD的开创者与传播者 本文纯属个人经历,如有雷同纯属巧合 我从不觉得自己是一个好的程序员,甚至可能连合格都谈不上,不过在内心深处我却渴望着在编程这件事上获得成功. 可惜每次 ...

  9. It is possible that this issue is resolved by uninstalling an existi

    使用真机连接Android Studio测试时出现这样的错误: 解决方法: 设置Android Studio 中Instant Run中的选项为不选中 根据以下路径,找到Instant Run中的选项 ...

  10. nginx 伪静态rewrite

    location正则写法 一个示例:   location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所 ...