noi.ac-CSP analog lamp Day5T2

 

 

Be it a question of thinking, there is no algorithm inside.

Before thinking of, then you can go try to run away, walked over to turn on the lights and then go back and turn off the lights, then go three times a way to go.

 

 

However, "if you can walk far away as possible" is just a greedy yy, without any basis. Suppose to find a transition point in the middle, it is a long journey should look like this:

 

The total distance is three times the distance, did not change eh.

So long as you are in earnest, is often normal to walk, not around the circle, the walking distance is 3 times the distance.

We only need to determine can not go on it.

That being the case, it can easily turn on the lights (fog)

(No kidding ah) is really just turn on the lights, because it is only to determine whether the go, you do not need saving.

Go left and right when the lights turn after each turn on the lights to ensure that at least the next point went to
go back and leave
again come to the right from the left turn off the lights, take the time to rely on this time it is not preceded by the light is turned
extends furthest to the left in front of the light to ensure that the lamp their location

 

. 1 #include <cstdio>
 2 #include <CString>
 . 3 #include <the iostream>
 . 4 #include < String >
 . 5 #include <Queue>
 . 6 #include <algorithm>
 . 7 #include <Vector>
 . 8 #include <the cstdlib>
 . 9  the using  namespace STD;
 10  #define N 300005
 . 11  #define LL Long Long 
 12 is  #define INF 0x3f3f3f3f
 13 is  int n-;
 14  int P [N], R & lt [N];
 15  / * 
16  from left to come to the right time, sequentially lights , go after each turn on the lights to ensure that at least the next point
17  to go back and leave
 18  once again come to the right from the left turn off the lights, and this time take the time to rely on the front lights are not off
 19  extends farthest to the left in front of the lights to ensure that their location has a lamp 
 20  * / 
21 is  int main ()
 22 is  {
 23 is      Scanf ( " % D " , & n-);
 24      for ( int I = . 1 ; I <= n-; I ++ )
 25          Scanf ( " % D% D " , & P [I], & R & lt [I]);
 26 is      int FAR = P [ . 1 ]; // can extend to the furthest right side of the light can be guaranteed coordinates come to the end 
27      for ( int I = 2; I <= n-; I ++ )
 28      {
 29          FAR = max (FAR, P [I- . 1 ] + R & lt [I- . 1 ]);
 30          IF (FAR < P [I])
 31 is          {
 32              the puts ( " -1 " );
 33 is              return  0 ;
 34 is          }
 35      } 
 36      FAR = P [n-]; // can extend to the furthest left of the light can be guaranteed coordinate lights 
37 [      for ( int I = n-; I> = 2 ; i-- )
 38 is      {
 39          FAR = min (FAR, P [I] -r[i]);
40         if(far>p[i-1])
41         {
42             puts("-1");
43             return 0;
44         }
45     }
46     printf("%lld\n",(p[n]-p[1])*3ll);
47     return 0;
48 }
Code

 

 

 

Guess you like

Origin www.cnblogs.com/lyttt/p/11796685.html