Codeforces-Round-#363-Launch of Collider

来源:互联网 发布:开淘宝保证金怎么退 编辑:程序博客网 时间:2024/06/01 08:08
 Launch of Collider
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
4RLRL2 4 6 10
output
1
input
3LLR40 50 60
output
-1
Note

In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.

In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.


题意:给出在同一直线上的n个微粒的位置(单位位置)以及运动方向,微粒运动速度也是单位的,求第一次任意两个微粒发生碰撞所需要的时间,如果没有,输出-1.


题目链接:Launch of Collider

解题思路:只有连续的两个微粒左边的向左运动,右边的向右运动才有可能发生碰撞,
碰撞所需时间 t = (a[i] - a[i - 1] + 1) / 2,然后取最小值即可。
一开始我保证碰运气的态度,把ans = 99999999,然后取min(ans, (a[i] - a[i - 1] + 1) / 2),结果被人Hack掉了。。。。。。搞得浪费了好多分,所以就用定义一个 fir ,判断ans是否第一次出现,第一次出现的话就直接ans =  (a[i] - a[i - 1] + 1) / 2即可。


代码:
#include<cstdio>using namespace std;const int N = 1e5 + 5;char s[2 * N];int a[2 * N];int main(){    int n,i,ans,f,fir;    while(~scanf("%d",&n)){        scanf("%s",s);        f = fir = 0;        for(i = 0;i < n;i++){            scanf("%d",&a[i]);           if(i && s[i] == 'L' && s[i - 1] == 'R') {                if(!fir) ans = (a[i] - a[i - 1] + 1) / 2, fir = 1; //第一次出现                else                    ans = min(ans,(a[i] - a[i - 1] + 1) / 2);  //取最优                f = 1;            }        }        if(f) printf("%d\n",ans);  //有解        else printf("-1\n");    }    return 0;}


0 0
原创粉丝点击