CF699A

来源:互联网 发布:centos制作u盘启动 编辑:程序博客网 时间:2024/06/05 00:24
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
题意:一直有n个粒子,他们分别向左或向右以1格/秒的速度移动,坐标各不相同
若存在任何两个粒子能相撞,则输出最短相撞时间,否则输出-1;

思路:由于坐标是升序,故只有当相邻粒子为RL时,才有可能相撞。
求最短相撞时间,就是求出可能相撞粒子的最小距离,然后分奇偶性计算消耗时间


#include <cstdio>#include <cstring>#include <algorithm>#define N 200005using namespace std;char dir[N];int x[N];int dis[N];int main(){int n;while (~scanf("%d", &n)){getchar();int ans = -1,cnt = 0;scanf("%s", dir + 1);for (int i = 1; i <= n; i++)scanf("%d", &x[i]);for (int i = 1; i < n; i++){if (dir[i] == 'R' && dir[i + 1] == 'L')dis[cnt++] = x[i + 1] - x[i];}if (cnt == 0){printf("-1\n");continue;}sort(dis, dis + cnt);ans = dis[0] & 1 ? dis[0] / 2 + 1 : dis[0] / 2;printf("%d\n", ans);}return 0;}

0 0
原创粉丝点击