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

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

题意:

给定一个序列a,现在想要构造一个新序列是单调不增或单调不减的

定义S为原序列和新序列对应位置数之差的绝对值之和

要求S最小时的S

思路:

求单调不减和单调不增两个值 取最小【这道题有bug啊,就求了一个单调不减我就交了居然过了,不过反正同理】

虐狗宝典上的方法一我没怎么看懂 n^3的算法还难理解 算了弃了

引理:构造的新序列B中的数都在A中出现过 【数学归纳法可证】

方法二就是一个二维的dp dp[i][j]表示以长度为i的子序列,且Bi == j时的S最小值

dp[i][j] = a[i] - j + val, 其中val为min(dp[i - 1][k]), k < j

然后先把a中出现的数离散化一下就行了

今天学习到了一个新的离散化的方法

先对数组sort, 然后使用unique函数

unique函数将一个数组中相邻的相同的数删掉,只剩唯一一个

unique(num, num + n) - num表示返回的数组的长度

 //#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<map> #define inf 0x3f3f3f3f
using namespace std;
typedef long long int LL; const double eps = 1e-; int sgn(double x)
{
if(fabs(x) < eps) return ;
if(x < ) return -;
else return ;
}
struct point{
double x, y;
point(){}
point(double _x, double _y)
{
x = _x;
y = _y;
}
point operator -(const point &b)const
{
return point(x - b.x, y - b.y);
}
double operator ^(const point &b)const
{
return x * b.y - y * b.x;
}
double operator *(const point &b)const
{
return x * b.x + y * b.y;
}
void input()
{
scanf("%lf%lf", &x, &y);
}
}; struct line{
point s, e;
line(){}
line(point _s, point _e)
{
s = _s;
e = _e;
}
pair<int, point>operator &(const line &b)const
{
point res = s;
if(sgn((s - e) ^ (b.s - b.e)) == ){
if(sgn((s - b.e) ^ (b.s - b.e)) == ){
return make_pair(, res);
}
else return make_pair(, res);
}
double t = ((s - b.s) ^ (b.s - b.e)) / ((s - e) ^ (b.s - b.e));
res.x += (e.x - s.x) * t;
res.y += (e.y - s.y) * t;
return make_pair(, res);
}
}; bool inter(line l1, line l2)
{
return
max(l1.s.x, l1.e.x) >= min(l2.s.x, l2.e.x) &&
max(l2.s.x, l2.e.x) >= min(l1.s.x, l1.e.x) &&
max(l1.s.y, l1.e.y) >= min(l2.s.y, l2.e.y) &&
max(l2.s.y, l2.e.y) >= min(l1.s.y, l1.e.y) &&
sgn((l2.s - l1.s) ^ (l1.e - l1.s)) * sgn((l2.e - l1.s) ^ (l1.e - l1.s)) <= &&
sgn((l1.s - l2.s) ^ (l2.e - l1.s)) * sgn((l1.e - l2.s) ^ (l2.e - l2.s)) <= ;
} double area(point a, point b, point c)
{
return fabs((1.0 / ) * (a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)));
} const int maxn = ;
int n;
int a[maxn], dp[maxn][maxn], num[maxn];
map<int, int> mp; int main()
{
while(scanf("%d", &n) != EOF){
int cnt = ;
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
num[i] = a[i];
}
sort(num + , num + + n);
cnt = unique(num + , num + + n) - num - ;
//cout<<cnt<<endl; memset(dp, inf, sizeof(dp));
dp[][] = ;
for(int i = ; i <= n; i++){
int val = dp[i - ][];
for(int j = ; j <= cnt; j++){
if(dp[i - ][j] < val){
val = dp[i - ][j];
}
dp[i][j] =val + abs(a[i] - num[j]);
}
} int ans = inf;
for(int i = ; i <= cnt; i++){
ans = min(ans, dp[n][i]);
}
printf("%d\n", ans);
}
return ;
}

poj3666 Making the grade【线性dp】的更多相关文章

  1. LG2893/POJ3666 「USACO2008FEB」Making the Grade 线性DP+决策集优化

    问题描述 LG2893 POJ3666 题解 对于\(A\)中的每一个元素,都将存在于\(B\)中. 对\(A\)离散化. 设\(opt_{i,j}\)代表\([1,i]\),结尾为\(j\)的最小代 ...

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

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

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

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

  4. LightOJ1044 Palindrome Partitioning(区间DP+线性DP)

    问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...

  5. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

  6. hdu1712 线性dp

    //Accepted 400 KB 109 ms //dp线性 //dp[i][j]=max(dp[i-1][k]+a[i][j-k]) //在前i门课上花j天得到的最大分数,等于max(在前i-1门 ...

  7. 动态规划——线性dp

    我们在解决一些线性区间上的最优化问题的时候,往往也能够利用到动态规划的思想,这种问题可以叫做线性dp.在这篇文章中,我们将讨论有关线性dp的一些问题. 在有关线性dp问题中,有着几个比较经典而基础的模 ...

  8. POJ 2479-Maximum sum(线性dp)

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33918   Accepted: 10504 Des ...

  9. poj 1050 To the Max(线性dp)

    题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...

  10. nyoj44 子串和 线性DP

    线性DP经典题. dp[i]表示以i为结尾最大连续和,状态转移方程dp[i] = max (a[i] , dp[i - 1] + a[i]) AC代码: #include<cstdio> ...

随机推荐

  1. Linux下改动Matlab配置文件支持C++ 11标准以生成mex

    进入matlab 输入mex -v命令查看当前配置 输入命令改动配置文件 命令: !sudo gedit /usr/local/MATLAB/R2013a/bin/mexopts.sh 加入下面蓝色内 ...

  2. navicat 手动设置索引unique,报错duplicate entry "" for key ""

    错误场景:仅限于手动设置unique时.在navicat中根据流程:右键表名 -> 设计表 -> 索引 -> 设置某列为unique -> 保存错误图示: 错误原因:这句错误提 ...

  3. Java并发之彻底搞懂偏向锁升级为轻量级锁

    网上有许多讲偏向锁,轻量级锁的文章,但对偏向锁如何升级讲的不够明白,有些文章还相互矛盾,经过对jvm源码(biasedLocking.cpp)的仔细分析和追踪,基本升级过程有了一个清晰的过程,现将升级 ...

  4. 关于浏览器对静态HTML页面的缓存问题

    症状: 刚才为了测试TOMCAT的BASIC安全验证,修改了tomcat-users.xml和/WEB-INF/web.xml之后进行测试,<url-pattern>/*<url-p ...

  5. ibatis时间比较大小

     <![CDATA[          A.RFID_Time >= #StartTime#          ]]>时间搜索功能 A.RFID_Time <![CDATA[  ...

  6. oracle初始操作

    oracle登录 sqlplus  sys/oracle as sysdba 这个登录之后呢 会出现这个: Connected to an idle instance. 这一步是连接上 [oracle ...

  7. [kernel]内核日志及printk结构分析

    一直都知道内核printk分级机制,但是没有去了解过,前段时间和一个同事聊到开机启动打印太多,只需要设置一下等级即可:另外今天看驱动源码,也看到类似于Printk(KERN_ERR "... ...

  8. 去掉Scala的糖衣(4) -- Type Aliase

    我的新博客地址:http://cuipengfei.me/blog/2013/12/23/desugar-scala-4/ Scala中有一个type关键字,用来给类型或者是操作起别名,用起来很是方便 ...

  9. This表示当前对象

    This表示当前对象. Public void printNum(){ Int number=40: System.out.println(this.number); } 此时打印的是实例变量,而非局 ...

  10. 股票指数kdj,sar,macd

    http://blog.eastmoney.com/gulingqianketong2011/blog_120832611.html http://blog.sina.com.cn/s/blog_a3 ...