F. Bulbo

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/575/problem/F

Description

Bananistan is a beautiful banana republic. Beautiful women in beautiful dresses. Beautiful statues of beautiful warlords. Beautiful stars in beautiful nights.

In Bananistan people play this crazy game – Bulbo. There’s an array of bulbs and player at the position, which represents one of the bulbs. The distance between two neighboring bulbs is 1. Before each turn player can change his position with cost |posnew - posold|. After that, a contiguous set of bulbs lights-up and player pays the cost that’s equal to the distance to the closest shining bulb. Then, all bulbs go dark again. The goal is to minimize your summed cost. I tell you, Bananistanians are spending their nights playing with bulbs.

Banana day is approaching, and you are hired to play the most beautiful Bulbo game ever. A huge array of bulbs is installed, and you know your initial position and all the light-ups in advance. You need to play the ideal game and impress Bananistanians, and their families.

Input

The first line contains number of turns n and initial position x. Next n lines contain two numbers lstart and lend, which represent that all bulbs from interval [lstart, lend] are shining this turn.

  • 1 ≤ n ≤ 5000
  • 1 ≤ x ≤ 109
  • 1 ≤ lstart ≤ lend ≤ 109

Output

Output should contain a single number which represents the best result (minimum cost) that could be obtained by playing this Bulbo game.

Sample Input

5 4
2 7
9 16
8 10
9 17
1 6
 

Sample Output

8

HINT

题意

有n个区间,你一开始站在x位置

然后n个询问,每个询问给你一个线段,是l,r区间

然后你可以选择走到x位置,花费就是距离

然后关闭这个线段的花费是你现在的位置离线段的最短距离

然后问你依次关闭这n个线段最少需要多少花费

题解:

dp咯,dp[i][j]表示第i轮我在j位置的最小花费

dp[i][j]=min(dp[i][j],dp[i-1][k]+abs(d[i]-d[k]))

然后大概单调栈或者维护一下变成o(n^2)的转移就可以AC了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 110
#define eps 1e-9
int Num;
//const int inf=0x7fffffff; //¡ì&szlig;¡ì¨¦¡ì¨¤¡ì¨¦¡§f¡ì3
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** struct node
{
ll x,y;
};
node a[];
vector<ll> Q;
ll dp[];
map<ll,int> H;
int main()
{
int n=read();
ll x=read();
Q.push_back(x);
for(int i=;i<=n;i++)
{
a[i].x=read(),a[i].y=read();
Q.push_back(a[i].x);
Q.push_back(a[i].y);
}
sort(Q.begin(),Q.end());
Q.erase(unique(Q.begin(),Q.end()),Q.end());
for(int i=;i<Q.size();i++)
dp[i]=abs(Q[i]-x);
for(int i=;i<Q.size();i++)
H[Q[i]]=i;
for(int i=;i<=n;i++)
{
ll tmp=dp[]-Q[];
for(int j=;j<Q.size();j++)
{
dp[j]=min(dp[j],tmp+Q[j]);
tmp=min(tmp,dp[j]-Q[j]);
}
tmp=dp[Q.size()-]+Q[Q.size()-];
for(int j=Q.size()-;j>=;j--)
{
dp[j]=min(dp[j],tmp-Q[j]);
tmp=min(tmp,dp[j]+Q[j]);
}
for(int j=;j<Q.size();j++)
{
if(Q[j]<a[i].x||Q[j]>a[i].y)dp[j]+=min(abs(Q[j]-a[i].y),abs(Q[j]-a[i].x));
}
/* for(int j=0;j<Q.size();j++)
cout<<dp[j]<<" ";
cout<<endl;*/
}
ll ans = dp[];
for(int i=;i<Q.size();i++)
ans = min(ans,dp[i]);
cout<<ans<<endl;
return ;
}

Codeforces Bubble Cup 8 - Finals [Online Mirror] F. Bulbo DP的更多相关文章

  1. Codeforces Bubble Cup 8 - Finals [Online Mirror] B. Bribes lca

    题目链接: http://codeforces.com/contest/575/problem/B 题解: 把链u,v拆成u,lca(u,v)和v,lca(u,v)(v,lca(u,v)是倒过来的). ...

  2. Codeforces Bubble Cup 8 - Finals [Online Mirror]H. Bots 数学

    H. Bots Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/H Desc ...

  3. Codeforces Bubble Cup 8 - Finals [Online Mirror] D. Tablecity 数学题

    D. Tablecity Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/D ...

  4. Bubble Cup 12 - Finals Online Mirror, unrated, Div. 1

    Bubble Cup 12 - Finals Online Mirror, unrated, Div. 1 C. Jumping Transformers 我会状压 DP! 用 \(dp[x][y][ ...

  5. Bubble Cup 11 - Finals [Online Mirror, Div. 1]题解 【待补】

    Bubble Cup 11 - Finals [Online Mirror, Div. 1] 一场很好玩的题啊! I. Palindrome Pairs 枚举哪种字符出现奇数次. G. AI robo ...

  6. Bubble Cup X - Finals [Online Mirror]

    来自FallDream的博客,未经允许,请勿转载,谢谢. 组了个菜鸡队打cf上的ACM比赛 比较快做完了8题但是菜的抠脚罚时巨多,所以最后被顶到了19名(居然没出首页) 自己的号自从上次疯狂掉分就没动 ...

  7. Bubble Cup X - Finals [Online Mirror] B. Neural Network country 矩阵快速幂加速转移

    B. Neural Network country time limit per test 2 seconds memory limit per test 256 megabytes Due to t ...

  8. 【简单dfs】Bubble Cup 14 - Finals Online Mirror (Unrated, ICPC Rules, Teams Preferred, Div. 2), problem: (J) Robot Factory,

    传送门  Problem - 1600J - Codeforces 题目   题意 给定n行m列, 求每个连通块由多少格子组成,并将格子数从大到小排序输出 对于每个格子都有一个数(0~15),将其转化 ...

  9. Bubble Cup 12 - Finals [Online Mirror, unrated, Div. 1] E. Product Tuples

    题意略,题解生成函数练习题,1+(q-ai)x卷积即可,线段树优化(类似分治思想) //#pragma GCC optimize(2) //#pragma GCC optimize(3) //#pra ...

随机推荐

  1. C#中常见的winform控件命名规范

    我们知道Button 常常简称为btn,那么Winform中的其它控件呢,这篇文章在C#的winform控件命名规范 的基础上对一些控件的名称的简称进行了整理. 1. 标准控件 NO. 控件类型简写 ...

  2. <转>Python 多线程的单cpu与cpu上的多线程的区别

    你对Python 多线程有所了解的话.那么你对python 多线程在单cpu意义上的多线程与多cpu上的多线程有着本质的区别,如果你对Python 多线程的相关知识想有更多的了解,你就可以浏览我们的文 ...

  3. unicode ansi utf-8 unicode_big_endian编码的区别

      随便说说字符集和编码  快下班时,爱问问题的小朋友Nico又问了一个问题:  "sqlserver里面有char和nchar,那个n据说是指unicode的数据,这个是什么意思.&quo ...

  4. 通过gdb跟踪Linux内核装载和启动可执行程序过程

    作者:吴乐 山东师范大学 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验目的:通过对一个简单的可执 ...

  5. auto printer 自动打字机效果

    前段时间在知乎上看到了一个打字机的效果,所以,心血来潮,自己也来写了一个打字机的效果. 比较简单,但还有待优化的地方,因为自己感觉这个效果不够炫,等哪天想出好的点子了.再来更新…… 代码效果预览地址: ...

  6. PHP:产生不重复随机数的方法

    来源:http://www.ido321.com/1217.html 无论是Web应用,还是WAP或者移动应用,随机数都有其用武之地.在最近接触的几个小项目中,我也经常需要和随机数或者随机数组打交道, ...

  7. MFC使用ShowWindow(SW_MAXIMIZE)任务栏消失的处理

    ShowWindow(SW_SHOWMAXIMIZED);//窗口最大化 问题:在写程序时,如果包含了标题栏,但是没有包含最大化按钮或者最小话按钮. 那么人工用ShowWindow(SW_MAXIMI ...

  8. C#实现APK自动打包

    C#实现APK自动打包     最近做了一个安卓项目,其中有一个自动打包的功能,要把供应商id写入APK后打包.   一.思路     在AndroidMinifest.xml中加入一个标识字段,如下 ...

  9. [Hive - LanguageManual] Hive Default Authorization - Legacy Mode

    Disclaimer Prerequisites Users, Groups, and Roles Names of Users and Roles Creating/Dropping/Using R ...

  10. 【Spark学习】Apache Spark for 第三方Hadoop分发版

    Spark版本:1.1.1 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4137979.html