Codeforces Bubble Cup 8 - Finals [Online Mirror] F. Bulbo DP
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
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; //¡ìߡ쨦¡ì¨¤¡ì¨¦¡§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的更多相关文章
- 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)是倒过来的). ...
- 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 ...
- 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 ...
- 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][ ...
- Bubble Cup 11 - Finals [Online Mirror, Div. 1]题解 【待补】
Bubble Cup 11 - Finals [Online Mirror, Div. 1] 一场很好玩的题啊! I. Palindrome Pairs 枚举哪种字符出现奇数次. G. AI robo ...
- Bubble Cup X - Finals [Online Mirror]
来自FallDream的博客,未经允许,请勿转载,谢谢. 组了个菜鸡队打cf上的ACM比赛 比较快做完了8题但是菜的抠脚罚时巨多,所以最后被顶到了19名(居然没出首页) 自己的号自从上次疯狂掉分就没动 ...
- 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 ...
- 【简单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),将其转化 ...
- 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 ...
随机推荐
- 漂亮灵活设置的jquery通知提示插件toastr
toastr是一款非常棒的基于jquery库的非阻塞通知提示插件,toastr可设定四种通知模式:成功,出错,警告,提示,而提示窗口的位置,动画效果都可以通过能数来设置,在官方站可以通过勾选参数来生成 ...
- HDU 5407 CRB and Candies
题意:给一个正整数k,求lcm((k, 0), (k, 1), ..., (k, k)) 解法:在oeis上查了这个序列,得知答案即为lcm(1, 2, ..., k + 1) / (k + 1),而 ...
- oracle 11g在安装过程中出现监听程序未启动或数据库服务未注册到该监听程序
15511477451 原文 oracle 11g在安装过程中出现监听程序未启动或数据库服务未注册到该监听程序? 环境:win7 64位系统.oracle11g数据库 问题描述:在win7 64位系统 ...
- [OFBiz]开发 五
1.初学者例程:OFBiz Tutorial - A Beginners Development Guidehttps://cwiki.apache.org/confluence/display/OF ...
- 试验Windows Embedded Standard 7 Service Pack 1 Evaluation Edition
=========================================== 是否支持再使用 RT 7 Lite 精简 ? ================================= ...
- CMDB反思2
当云灭掉CMDB http://blog.vsharing.com/xqscool/A1193910.html 虽然之前也思考过当运维底层都被替换为云时,现有的传统运维可能就消失了,其所依赖的ITIL ...
- ASP.NET MVC之Html.RenderAction
WEB窗体模式开发惯了,切入MVC模式,好多东西都不懂,每一步都要查资料. 初步得来的一些知识点体会是: _Layout.cshtml就相当于母版页 然后partical视图(部分视图)就是用户控件. ...
- Canvas入门(3):图像处理和绘制文字
来源:http://www.ido321.com/997.html 一.图像处理(非特别说明,所有结果均来自最新版Google) 在HTML 5中,不仅可以使用Canvas API绘制图形,也可以用于 ...
- wuzhicms访问统计实现方法
实现目标:程序实现了对整站页面pv的统计文件的位置:coreframe/app/content/pv.php代码预览: /** * 总站访问次数统计 */ defined('IN_WZ') or ex ...
- 检查ept
cat /proc/cpuinfo | grep ept 检查cpu是否支持ept cat /sys/module/kvm_intel/p ...