poj3666 Making the grade【线性dp】
| 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
|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.
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】的更多相关文章
- LG2893/POJ3666 「USACO2008FEB」Making the Grade 线性DP+决策集优化
问题描述 LG2893 POJ3666 题解 对于\(A\)中的每一个元素,都将存在于\(B\)中. 对\(A\)离散化. 设\(opt_{i,j}\)代表\([1,i]\),结尾为\(j\)的最小代 ...
- poj3666/CF714E/hdu5256/BZOJ1367(???) Making the Grade[线性DP+离散化]
给个$n<=2000$长度数列,可以把每个数改为另一个数代价是两数之差的绝对值.求把它改为单调不增or不减序列最小代价. 话说这题其实是一个结论题..找到结论应该就很好做了呢. 手玩的时候就有感 ...
- [poj3666]Making the Grade(DP/左偏树)
题目大意:给你一个序列a[1....n],让你求一个序列b[1....n],满足 bi =a && bc,则最小的调整可以是把b变成c. 所以归纳可知上面结论成立. dp[i][j] ...
- LightOJ1044 Palindrome Partitioning(区间DP+线性DP)
问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
- 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门 ...
- 动态规划——线性dp
我们在解决一些线性区间上的最优化问题的时候,往往也能够利用到动态规划的思想,这种问题可以叫做线性dp.在这篇文章中,我们将讨论有关线性dp的一些问题. 在有关线性dp问题中,有着几个比较经典而基础的模 ...
- POJ 2479-Maximum sum(线性dp)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33918 Accepted: 10504 Des ...
- poj 1050 To the Max(线性dp)
题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...
- nyoj44 子串和 线性DP
线性DP经典题. dp[i]表示以i为结尾最大连续和,状态转移方程dp[i] = max (a[i] , dp[i - 1] + a[i]) AC代码: #include<cstdio> ...
随机推荐
- IOS基于XMPP协议开发--XMPPFramewok框架(三):用户注册
接着上面说 用户注册是比较简单的,成功连接上服务器后,设置好JID,即可调用 [_xmppStream registerWithPassword:pwd error:&err] 进行注册 -( ...
- Windows下ADB默认的5037port被占用,解决方式。
Windows下可能会因为系统版本号不一样的原因导致有的系统5037port被系统进程占用.导致ADB无法使用5037port,从而导致ADB不能打开.在eclipse上跑Android程序的时候显示 ...
- Typeface-为自定义字体提供字体内存缓存
Android 上自定义字体的代码一般如下: TextView textview = (TextView) findViewById(R.id.your_referenced_textview); / ...
- CConfig类
#ifndef __CONFIG_H__ #define __CONFIG_H__ #include "GameFrameHead.h" //图片信息 struct ImageIn ...
- python文件目录操作大全
python只获取当前目录下的文件夹及文件名 list = os.listdir(rootdir)#列出目录下的所有文件和目录 for line in list: filepath = os.path ...
- Handler实例
Handler使用例1这个例子是最简单的介绍handler使用的,是将handler绑定到它所建立的线程中.本次实验完成的功能是:单击Start按钮,程序会开始启动线程,并且线程程序完成后延时1s会继 ...
- GPIO 输入—按键检测
这里要用到一定的模电知识.电容两端电压不能突变,电感两端电流不能突变.这里利用了电容的放电延时实现硬件消抖.按键按下会有抖动,波形有毛刺,使得高低电平显现不明显,而按键按下时,电容放电一下,马上又被充 ...
- Ecshop安装的坑,建议不要使用!
最近因为工作的需要,安装了下ecshop,这个曾经的火爆开源程序,现在也呈现出疲态. 1.请看官方的运行环境推荐: 服务器端运行环境推荐·php版本5.0以上5.3以下的版本(推荐使用5.2系列版本) ...
- Scrum培训心得体会
# Scrum培训心得体会 非常荣幸能够参加公司组织的这场为期两天的培训,赛宝的老师讲的非常好.通过这次学习,理解了当前最流行的Scrum开发框架,下面总结了我对Scrum的理解. ## scrum的 ...
- Unix系统编程()在堆上分配内存
在堆上分配内存:malloc和free 一般情况下,C程序使用malloc函数族在堆上分配和释放内存.较之brk和sbrk,这些函数具备不少优点: 属于C语言标准的一部分 更易于在多线程程序中使用 接 ...