问题: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)问题的更多相关文章

  1. POJ3016-K-Monotonic(左偏树+DP)

    我觉得我要改一下签名了……怎么会有窝这么啰嗦的人呢? 做这题需要先学习左偏树<左偏树的特点及其应用> 然后做一下POJ3666,这题的简单版. 思路: 考虑一下维护中位数的过程原数组为A, ...

  2. POJ3666-Making the Grade(左偏树 or DP)

    左偏树 炒鸡棒的论文<左偏树的特点及其应用> 虽然题目要求比论文多了一个条件,但是……只需要求非递减就可以AC……数据好弱…… 虽然还没想明白为什么,但是应该觉得应该是这样——求非递减用大 ...

  3. 洛谷P1552 [APIO2012] 派遣 [左偏树,树形DP]

    题目传送门 忍者 Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都 ...

  4. 51Nod1802 左偏树计数

    题目大意 求$n$个点的无标号左偏树个数 既然你都点进来了,那么估计也是奔着题解来的.... 废话少说.... 首先,左偏树有这么一些性质 设最右链长度为$r[p]$ 1.左偏树的子树仍然是左偏树 2 ...

  5. Luogu P1552 [APIO2012]派遣【左偏树】By cellur925

    题目传送门 $Chat$ 哈哈哈我xj用dfs序乱搞竟然炸出了66分....(其实还是数据水,逃) $Sol$ 首先我们应该知道,一个人他自己的满意度与他子树所有节点的领导力是无关的,一个人的满意度受 ...

  6. 洛谷$P4331\ [BOI2004]\ Sequence$ 数字序列 左偏树

    正解:左偏树 解题报告: 传送门$QwQ$ 开始看到的时候$jio$得长得很像之前做的一个$dp$,,, 但是$dp$那题是说不严格这里是严格? 不难想到我们可以让$a_{i},b_{i}$同时减去$ ...

  7. BZOJ 1455 罗马游戏 ——左偏树

    [题目分析] 左偏树的模板题目,大概就是尽量维护树的深度保持平衡,以及尽可能的快速合并的一种堆. 感觉和启发式合并基本相同. 其实并没有快很多. 本人的左偏树代码自带大常数,借鉴请慎重 [代码] #i ...

  8. 【BZOJ-1455】罗马游戏 可并堆 (左偏树)

    1455: 罗马游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1355  Solved: 561[Submit][Status][Discuss] ...

  9. 【bzoj2809】[Apio2012]dispatching 左偏树

    2016-05-31  15:56:57 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2809 直观的思想是当领导力确定时,尽量选择薪水少的- ...

随机推荐

  1. leetcode: Path Sum II 迭代法

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  2. 谁可以说出HashMap和HashSet的相同点和不同点。

    谁可以说出HashMap和HashSet的相同点和不同点. 2011-11-15 20:46ruoshui_t | 浏览 20310 次  Perl 2011-11-15 21:17 #知道行家专业创 ...

  3. 005医疗项目-模块一:用户的查找:1.用户表查询的sql语句

    这是医疗项目的第一个模块:做一个用户的查询,可以根据用户的账号,用户的名称,单位的名称,用户的类型去查询.要求效果如下:

  4. Python中的sort()方法使用基础

    一.基本形式 sorted(iterable[, cmp[, key[, reverse]]]) iterable.sort(cmp[, key[, reverse]]) 参数解释: (1)itera ...

  5. 小程序基础02:全局配置app.json

    1.配置 我们使用app.json文件来对来微信小程序进行全局配置. 作用:他决定了页面文件的路径,窗口表现,设置网络超时时间,设置多tab等 每一个小程序页面也可以使用 .json 文件来对本页面的 ...

  6. JS案例之8——从一个数组中随机取数

    近期项目中遇到一个需求,从一个列表中随机展示列表的部分内容,需求不大,JS也非常容易实现.主要是运用到了Math对象的random方法,和Array的splice方法. 思路是先新建一个数组,存放所有 ...

  7. Caffe学习系列(16):caffemodel可视化

    通过前面的学习,我们已经能够正常训练各种数据了.设置好solver.prototxt后,我们可以把训练好的模型保存起来,如lenet_iter_10000.caffemodel. 训练多少次就自动保存 ...

  8. Activiti系列:如何把Activiti工程转换为maven工程以解决依赖项找不到的问题

    在eclipse中安装了Activiti插件之后,就可以新建Activiti工程,但是在实际使用时发现,在该工程中间新建Activiti Diagram,绘制好该图形之后,右键,新建单元测试,选择ju ...

  9. [MetaHook] Load DTX texture to OpenGL

    This function load a LithTech *.dtx texture file and convert to OpenGL pixel format, compressed supp ...

  10. WiFi QC 自动测试:Qt控制无线路由器

    在测试wifi的时候,测试人员一般要使用很多不同型号的AP,并且需要不断地切换Chariot的配置. 这里的思路是致力于提供一个友好的GUI界面来自动控制AP,并且自动控制Chariot进行Throu ...