A. Launch of Collider Codeforces Round #363 (Div2)

A. 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

4 RLRL 2 4 6 10

Output

1

Input

3 LLR 40 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个粒子,第二行表示他们分别向R(ight)或L(eft)方向移动,第三个是他们从哪个点开始移动。问当第一个粒子相撞的时候时间是多少?

 

——————————————————————

 

这道题我蠢了,一开始写了个O(n^2)级别的代码,枚举了每两个数的各种可能取最小值。以为数据20W*20W是4e9左右,实际上是4e10……要是4e10根本不会去试O(n^2)了……当时大概知道O(n^2)应该过不了,但是想着A题应该不会用高级算法吧(我甚至想到了线段树之类的) , 然后想着优化一下或许能过。

附O(n^2)自带超级大常数代码(后来还试了几下是不是cin cout 效率低换了 scanf printf ):

这道题最蠢在英语上……后来去查了下题解,发现别人写的if ( a[j] == 'R' && a[j+1] == 'L' ) 这样的代码才意识到我遗漏了什么。

回去重新读题发现坐标是按升序排列,也就是说只要找相邻的RL区间最小就是答案。

被自己气哭了……

 

附AC代码:

 

#include<cstdio>
struct
{
    char a ;
    int b;
} x[200010];

int main()
{
    int n ;
    scanf("%d",&n);
    getchar();   // 吞掉键盘缓冲区的换行符
    //(这里曾以为CF是LINUX,所以用了两个getchar(),wrong了才改的)
    for( int i = 0 ; i < n ; i++ )
        scanf("%c",&x[i].a);
    getchar();
    int min = 1e9 + 1 ;
    int flag = 1 ;
    for( int i = 0 ; i < n ; i++)
        scanf("%d",&x[i].b);
    for( int j = 0 ; j < n ; j++)
        if( x[j+1].a == 'L' && x[j].a == 'R' && x[j].b < x[j+1].b )
        {
            if ( x[j+1].b - x[j].b < min) min = x[j+1].b - x[j].b ;
                flag = 0;
        }
    if( flag == 0 )cout << (min >> 1) << endl ;
    else cout << "-1" << endl ;
    return 0;
}

 

posted on 2017-01-12 23:18  Dark猫  阅读(88)  评论(0)    收藏  举报

导航