Ski-Trails for Robots

Time limit: 1.0 second
Memory limit: 64 MB
One of the stages of the Robot Cross-Country World Cup was held at the Uktus Ski Lodge in Yekaterinburg.
Professor Popov's laboratory sent its newest Robot NS6 to take part in the race. The neural networks of this robot were well-trained in the classic style skiing. The robot was not very lucky with the drawing: he was one of the last racers to start and the trails had been already heaped up with the participants who hadn't been able to make their way to the finish. This created a serious problem, as the robot now had to keep switching between the ski trails in order to skirt the obstacles. As a result, it lost the precious time because moving to an adjacent trail each time took one second.
Given the places where the fallen robots lie, determine the optimal way to skirt them all in the minimum time.

Input

The first line contains integers ns, and k separated with a space (2 ≤ n ≤ 105; 1 ≤ s ≤ n; 0 ≤ k ≤ 105). There are n parallel ski trails that lead from start to finish. They are numbered successively from 1 to n. Robot NS6 starts along the trail with number s. The integer k is the number of robots which fell down on the trails.
The following k lines describe the lying robots in the order from start to finish. In each line there are integers l and r, which mean that a robot blocked the trails with numbers from l to rinclusive (1 ≤ l ≤ r ≤ n). You can assume that all the fallen robots lie at a sufficient distance from each other (and from the start) so that Robot NS6 can perform the necessary maneuvers. If some robot blocks an outermost trail, it can be skirted on one side only. No robot blocks all the trails simultaneously.

Output

Output the minimum time in seconds that Robot NS6 spent for switching from trail to trail in order to skirt all the fallen contestants and successfully complete the race.

Sample

input output
5 3 2
2 5
1 4
6

分析:参考http://blog.csdn.net/xcszbdnl/article/details/38494201;

   对于当前障碍物,在障碍物旁边的点必然是可到达的最短的路程的点;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t,s;
set<int>p,q;
set<int>::iterator now,pr,la;
ll dp[maxn];
int main()
{
int i,j;
scanf("%d%d%d",&n,&s,&k);
rep(i,,n+)dp[i]=1e18;
p.insert(),p.insert(n+),p.insert(s);
dp[s]=;
while(k--)
{
int a,b;
scanf("%d%d",&a,&b);
if(a>)
{
a--;
p.insert(a);
now=p.find(a);
pr=--now;
++now;
la=++now;
--now;
if(dp[*now]>dp[*pr]+(*now)-(*pr))dp[*now]=dp[*pr]+(*now)-(*pr);
if(dp[*now]>dp[*la]+(*la)-(*now))dp[*now]=dp[*la]+(*la)-(*now);
a++;
}
if(b<n)
{
b++;
p.insert(b);
now=p.find(b);
pr=--now;
++now;
la=++now;
--now;
if(dp[*now]>dp[*pr]+(*now)-(*pr))dp[*now]=dp[*pr]+(*now)-(*pr);
if(dp[*now]>dp[*la]+(*la)-(*now))dp[*now]=dp[*la]+(*la)-(*now);
b--;
}
q.clear();
for(now=p.lower_bound(a);now!=p.end()&&*now<=b;now++)q.insert(*now);
for(int x:q)p.erase(x),dp[x]=1e18;
}
ll mi=1e18;
rep(i,,n)if(mi>dp[i])mi=dp[i];
printf("%lld\n",mi);
//system("pause");
return ;
}

ural1772 Ski-Trails for Robots的更多相关文章

  1. 网站 robots.txt 文件编写

    网站 robots.txt 文件编写 Intro robots.txt 是网站根目录下的一个纯文本文件,在这个文件中网站管理者可以声明该网站中不想被robots访问的部分,或者指定搜索引擎只收录指定的 ...

  2. Robots.txt - 禁止爬虫(转)

    Robots.txt - 禁止爬虫 robots.txt用于禁止网络爬虫访问网站指定目录.robots.txt的格式采用面向行的语法:空行.注释行(以#打头).规则行.规则行的格式为:Field: v ...

  3. (转载)robots.txt写法大全和robots.txt语法的作用

    1如果允许所有搜索引擎访问网站的所有部分的话 我们可以建立一个空白的文本文档,命名为robots.txt放在网站的根目录下即可.robots.txt写法如下:User-agent: *Disallow ...

  4. 2016 ccpc 网络选拔赛 F. Robots

    Robots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  5. Codeforces 209 C. Trails and Glades

    Vasya went for a walk in the park. The park has n glades, numbered from 1 to n. There are m trails b ...

  6. robots.txt文件没错,为何总提示封禁

    大家好,我的robots.txt文件没错,为何百度总提示封禁,哪位高人帮我看看原因,在此谢过. 我的站点www.haokda.com,robots.txt如下: ## robots.txt for P ...

  7. robots笔记以免忘记

    html头部标签写法: <meta name="robots" content="index,follow" /> content中的值决定允许抓取 ...

  8. [题解]USACO 1.3 Ski Course Design

    Ski Course Design Farmer John has N hills on his farm (1 <= N <= 1,000), each with an integer ...

  9. springMVC robots.txt 处理

    正常情况这样就好使 <mvc:resources mapping="/robots.txt" location="/lib/robots.txt"/> ...

随机推荐

  1. 数据查找之80-20原则的JavaScript代码实现

    作为前端开发人员,无论在工作还是找工作(笔试/面试),或多或少会涉及一些数据结构的知识. 数据结构即计算机存储和组织数据的方式. 常用的结构:数组.栈.队列.链表.树.图.堆和散列表 关于数据,我们常 ...

  2. Code Blocks 多文件联合编译

        code::blocks是一款据说灰常强大的IDE,以前虽然也经常使用,但一没用过高度功能,二来没用它写过工程性的东西,简单点说就是一个以上的源文件并且加入其他非标准的头文件,今天想做一个多文 ...

  3. leetcode387

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  4. Storm官方文档翻译之创建Storm项目

    本文将讲述如何在开发环境创建一个Storm项目,下面是步骤: 1.将Storm的相关jar包添加到classpath中: 2.如果使用多语言开发,也要将多语言目录添加到classpath; 点击sto ...

  5. HTML/CSS font-family对应的中英文名称 宋体 微软雅黑

    宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMingLiU 细明体 Ming ...

  6. 转 Windows 7设置定时自动执行任务方法

    在使用电脑的时候可能会遇到一些需要无人值守让电脑自行执行任务后定时关机的情形,在Win7系统中,我们可以使用"任务计划"设置功能结合 shutdown命令灵活设置任务计划,让Win ...

  7. spring @Autowired或@Resource 的区别

    1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属于spring的),默认情况下必 ...

  8. position的absolute与fixed共同点与不同点

    这个问题面试被问到过~~ A:共同点: 1.改变行内元素的呈现方式,display被置为block: 2.让元素脱离普通流,不占据空间: 3.默认会覆盖到非定位元素上   B不同点: absolute ...

  9. Dockerfile编写语法

    docker镜像本质上就是一个个基础镜像的堆叠,为了做出我们想要的镜像,我们需要考虑最终镜像所需的所有基础环境,然后一层层堆叠.也就是不断以基础镜像搭建上层镜像. 先看例子: # Version: # ...

  10. HDU2586 How far away ? 邻接表+DFS

    题目大意:n个房子,m次询问.接下来给出n-1行数据,每行数据有u,v,w三个数,代表u到v的距离为w(双向),值得注意的是所修建的道路不会经过一座房子超过一次.m次询问,每次询问给出u,v求u,v之 ...