左偏树(DP)问题
问题: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
回答:题意给定一个序列,以最小代价将其变成单调不增或单调不减序列。
#include "stdio.h"
#include "iostream"
#include "algorithm"
using namespace std;
__int64 dp[2][2003];
int main()
{
freopen("aaa.txt","r",stdin);
__int64 m,temp;
__int64 a[2003],b[2003];
int n,i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=1; i<=n; i++)
scanf("%I64d",&a[i]), b[i]=a[i];
sort(b+1,b+n+1);
for(i=1; i<=n; i++)
{
dp[1][i] = a[1]-b[i];
if(dp[1][i]<0) dp[1][i]=-dp[1][i];
}
for(i=2; i<=n; i++)
{
temp=dp[(i+1)%2][1];
for(j=1; j<=n; j++)
{
temp=min(temp,dp[(i+1)%2][j]);
m=a[i]-b[j];
if(m<0) m=-m;
dp[i%2][j]=temp+m;
}
}
temp=dp[n%2][n];
for(i=n; i>=1; i--)
temp = min(temp,dp[n%2][i]);
printf("%I64d\n",temp);
}
return 0;
}
左偏树(DP)问题的更多相关文章
- POJ3016-K-Monotonic(左偏树+DP)
我觉得我要改一下签名了……怎么会有窝这么啰嗦的人呢? 做这题需要先学习左偏树<左偏树的特点及其应用> 然后做一下POJ3666,这题的简单版. 思路: 考虑一下维护中位数的过程原数组为A, ...
- POJ3666-Making the Grade(左偏树 or DP)
左偏树 炒鸡棒的论文<左偏树的特点及其应用> 虽然题目要求比论文多了一个条件,但是……只需要求非递减就可以AC……数据好弱…… 虽然还没想明白为什么,但是应该觉得应该是这样——求非递减用大 ...
- 洛谷P1552 [APIO2012] 派遣 [左偏树,树形DP]
题目传送门 忍者 Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都 ...
- 51Nod1802 左偏树计数
题目大意 求$n$个点的无标号左偏树个数 既然你都点进来了,那么估计也是奔着题解来的.... 废话少说.... 首先,左偏树有这么一些性质 设最右链长度为$r[p]$ 1.左偏树的子树仍然是左偏树 2 ...
- Luogu P1552 [APIO2012]派遣【左偏树】By cellur925
题目传送门 $Chat$ 哈哈哈我xj用dfs序乱搞竟然炸出了66分....(其实还是数据水,逃) $Sol$ 首先我们应该知道,一个人他自己的满意度与他子树所有节点的领导力是无关的,一个人的满意度受 ...
- 洛谷$P4331\ [BOI2004]\ Sequence$ 数字序列 左偏树
正解:左偏树 解题报告: 传送门$QwQ$ 开始看到的时候$jio$得长得很像之前做的一个$dp$,,, 但是$dp$那题是说不严格这里是严格? 不难想到我们可以让$a_{i},b_{i}$同时减去$ ...
- BZOJ 1455 罗马游戏 ——左偏树
[题目分析] 左偏树的模板题目,大概就是尽量维护树的深度保持平衡,以及尽可能的快速合并的一种堆. 感觉和启发式合并基本相同. 其实并没有快很多. 本人的左偏树代码自带大常数,借鉴请慎重 [代码] #i ...
- 【BZOJ-1455】罗马游戏 可并堆 (左偏树)
1455: 罗马游戏 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1355 Solved: 561[Submit][Status][Discuss] ...
- 【bzoj2809】[Apio2012]dispatching 左偏树
2016-05-31 15:56:57 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2809 直观的思想是当领导力确定时,尽量选择薪水少的- ...
随机推荐
- 利用OpacityMask制作打洞效果
起因 项目上存在一个连线功能,在设计的原型中,在连线中间文字上下各有15像素的空白.接手的同事觉得没思路,问我能不能在不影响连线后面的背景情况下解决该问题.我就抽了点时间给他写了个Demo.回家后趁热 ...
- java 16 - 9 增强for的概述和使用
JDK5的新特性:自动拆装箱,泛型,增强for,静态导入,可变参数,枚举 增强for:是for循环的一种. 格式: for(元素数据类型 变量 : 数组或者Collection集合) { 使用变量即可 ...
- js模拟手机触摸屏
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- WebResource-asp.net自定义控件引用外部资源方法
rom:http://www.lmwlove.com/ac/ID879 在asp.net中开发自定义控件时,如果我们要用到图片,外部css,js等文件,那么最好的方式就是将这些文件作为自定义控件嵌入的 ...
- CSS3实现10种Loading效果
昨晚用CSS3实现了几种常见的Loading效果,虽然很简单,但还是分享一下,顺便也当是做做笔记…… 第1种效果: 代码如下: <div class="loading"> ...
- Hashtable 数据遍历的几种方式
Hashtable 在集合中称为键值对,它的每一个元素的类型是 DictionaryEntry,由于Hashtable对象的键和值都是Object类型,决定了它可以放任何类型的数据, 下面我就把Has ...
- zabbix架构
监控系统: 数据的采集---存储---展示和分析---报警 snmp cacti:rrd agent nagios:不保存数据,mysql icmp/ssh ...
- js中的预加载与懒加载(延迟加载)
js中加载分两种:预加载与延迟加载 一. 预加载,增强用户的体验,但会加载服务器的负担.一般会使用多种 CSS(background).JS(Image).HTML(<img />) . ...
- java 顺序 读写 Properties 配置文件
java 顺序 读写 Properties 配置文件 支持中文 不乱码 java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不 ...
- 一道c语言运算符优先级问题
一道c语言运算符优先级问题 #include <iostream> using namespace std; int main() { char test[] = {"This ...