Description
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Sample Input
4
RLRL
2 4 6 10
1
3
LLR
40 50 60
-1
题意: n个碰撞机 给你每个初始的运动方向 (只有‘L’‘R’) 给你初始的位置 (位置不同且都为偶数) 每个碰撞机器每秒运动一米
问你第一次发生碰撞的时刻是什么时候。
题解:最快发生碰撞肯定是相邻的一组‘L’‘R’
所以遍历一遍 找最小值 若不存在则输出‘-1’;
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<queue> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include<algorithm> 9 #define ll __int64 10 #define mod 1e9+7 11 #define PI acos(-1.0) 12 using namespace std; 13 int n; 14 struct node 15 { 16 int pos; 17 char dir; 18 }N[200005]; 19 int main() 20 { 21 scanf("%d",&n); 22 getchar(); 23 int minx=1e9+7; 24 for(int i=1;i<=n;i++) 25 scanf("%c",&N[i].dir); 26 for(int i=1;i<=n;i++) 27 scanf("%d",&N[i].pos); 28 int flag=0; 29 for(int i=2;i<=n;i++) 30 { 31 if(N[i].dir=='L'&&N[i-1].dir=='R') 32 { 33 flag=1; 34 minx=min(minx,(N[i].pos-N[i-1].pos)/2); 35 } 36 } 37 if(flag==0) 38 cout<<"-1"<<endl; 39 else 40 cout<<minx<<endl; 41 return 0; 42 }