Ski-trails for Robotstime limit:1.0 second
Memory limit:64 MBOne of the stages of the Robot cross-country world Cup is 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 wasn't very lucky with the Drawing:he is one of the last racers to start and the trails had been already heap Ed up and the participants who hadn ' t been able-make their-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 OBSTAC Les. 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 the "to" skirt them all in the minimum time. Inputthe First line contains integers
N,
s, and
kSeparated with a space (2≤
N≤105; 1≤
s≤
N; 0≤
k≤105). There is
NParallel ski trails that leads from start to finish. They is numbered successively from 1 to
N. Robot NS6 starts along the trail with number
s. The integer
kIs the number of robots which fell down on the trails. The following
kLines describe the lying robots in the order from start to finish. In each line there is integers
Land
R, which mean that a robot blocked the trails with numbers from
LTo
RInclusive (1≤
L≤
R≤
N). You can assume this all the fallen robots lie @ 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 is skirted on one side only. No robot blocks all the trails simultaneously. Outputoutput the minimum time in seconds this Robot NS6 spent for switching from trail to trail on order to skirt all the Fallen contestants and successfully complete the race. Sample
input |
Output |
5 3 22) 51 4 |
6 |
Analysis: Reference http://blog.csdn.net/xcszbdnl/article/details/38494201;
For the current obstacle, the point next to the obstacle is necessarily the shortest distance that can be reached;
Code:
#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>#defineRep (I,m,n) for (i=m;i<=n;i++)#defineRSP (It,s) for (Set<int>::iterator It=s.begin (); It!=s.end (); it++)#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineVI vector<int>#definePB Push_back#defineMP Make_pair#defineFi first#defineSe Second#definell Long Long#definePi ACOs (-1.0)#definePII pair<int,int>#defineLson L, Mid, rt<<1#defineRson mid+1, R, rt<<1|1Const intmaxn=1e5+Ten;Const intdis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};using namespacestd;ll gcd (ll p,ll q) {returnq==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;}returnF;}intn,m,k,t,s;Set<int>p,q;Set<int>:: Iterator now,pr,la;ll DP[MAXN];intMain () {inti,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--) { intb; 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(intX: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;}
Ski-trails for Robots