Ski-Trails for Robots

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+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}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,0,n+1)dp[i]=1e18;
    p.insert(0),p.insert(n+1),p.insert(s);
    dp[s]=0;
    while(k--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        if(a>1)
        {
            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,1,n)if(mi>dp[i])mi=dp[i];
    printf("%lld\n",mi);
    //system("pause");
    return 0;
}
时间: 2024-10-16 07:51:32

Ski-Trails for Robots的相关文章

Python 爬虫-Robots协议

2017-07-25 21:08:16 一.网络爬虫的规模 二.网络爬虫的限制 ? 来源审查:判断User‐Agent进行限制 检查来访HTTP协议头的User‐Agent域,只响应浏览器或友好爬虫的访问? 发布公告:Robots协议 告知所有爬虫网站的爬取策略,要求爬虫遵守 三.Robots 协议 作用:网站告知网络爬虫哪些页面可以抓取,哪些不行形式:在网站根目录下的robots.txt文件 如果网站不提供Robots协议则表示该网站允许任意爬虫爬取任意次数. 类人类行为原则上可以不遵守Rob

robots.txt的介绍和写作

目前很多网站管理者似乎对robots.txt并没有引起多大重视,甚至不知道这么一个文件的作用.本来应该保密的信息被爬虫抓取了,公布在公网上,本应该发布到公网的信息却迟迟不被搜索引擎收录.所以下面这篇文章,就来介绍robots.txt的作用和写作 robots.txt基本介绍 robots 是一个纯文本文件,是用来告诉搜索引擎:当前这个网站上哪些部分可以被访问.哪些不可以,robots文件是存放在网站根目录下的一个纯文本文件.当搜索引擎访问一个网站时,它首先会检查该网站根目录下是否存在robots

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 elevation in the range 0 .. 100. In the winter, since there is abundant snow on these hills, FJ routinely operates a ski training camp. Unfortunately, FJ h

Place the Robots 需要较强的建图能力

Place the Robots 思路:在任意一个点格子放机器人,那么它所在的行和列被控制了.我们对每一行或每一列连续的空地(草地忽视)称之为块,给每一行和每一列的块标号, 每一行的快与每一列的快相交的话,才有只有一个交点.  我们把交点当边,把行块和列块连接起来.每一个空第都是一条边.详细细节见代码. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath&

Codeforces 91C Ski Base 加边求欧拉回路数量

题目链接:点击打开链接 题意: 给出n个点m条无向边的图 开始图里没有边,每次加一条边,然后输出图里欧拉回路的条数. 思路: We will count the number of ski bases including the base consisted of empty subset of edges (before printing just subtract one). In the beginning the number of bases is equal to 1. If we

django1.5添加robots.txt

方法一:(The best way) urlpatterns = patterns( (r’^robots\.txt$’, TemplateView.as_view(template_name=’robots.txt’, content_type=’text/plain’)), ) 其他: http://stackoverflow.com/questions/6867468/setting-up-mimetype-when-using-templateview-in-django

seo课程之robots.txt的格式

其实很多人刚刚开始从事seo的时候,根本就不知道什么是robots.txt,就算知道了也不懂得robots.txt的文件格式是什么,今天小编我就来和大家分享一下吧(本文来自于e良师益友网). "robots.txt"文件包含一条或更多的记录,这些记录通过空行分开(以CR,CR/NL, or NL作为结束符),每一条记录的格式如下所示: "<field>:<optional space><value><optionalspace>

UVA10599 - Robots(II)(变形的LIS)

题意:一个机器人在n * m的网格里面捡垃圾,机器人只能向右或向下走,求出能捡到的垃圾数量的最大值,有多少条路径可以达到最大值,以及输出其中一条路径. 思路:按照题意可以看出,因为机器人只能向右和向下走,所以纵坐标就不重要的,而横坐标是递增的.当将所有拥有垃圾的格子经过计算得到它的一维值(唯一的),得到一组的数组.那就可以转化为求最长上升子序列.但这个LIS的条件是mod(m)要大于前一个.计算数量时,当d[i] = d[j] + 1时,就相当于以i为结束时的最长上升子序列比以j结束时的最长上升

实例分析Robots.txt写法

题意:经典八数码问题 思路:HASH+BFS #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 500000; const int size = 1000003; typedef int State[9]; char str[30]; int state[9],goal[9]={