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.
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.
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.
4
RLRL
2 4 6 10
1
3
LLR
40 50 60
-1
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.
#include <stdio.h> //找最短碰撞时间,就找相邻的且移动方向不同的
#include <stdlib.h>
#include <string.h>
#include <math.h>
int a[200005];
char s[200005];
int f(int x,int y)
{
return x>y ? y:x;
}
int main()
{
int n,i,min,flag=0;
min=0x3f3f3f3f;
scanf("%d",&n);
scanf("%s",s);
for(i=0; i<n; i++)
scanf("%d",&a[i]);
for(i=0; i<n-1; i++)
{
if(s[i]=='R'&&s[i+1]=='L'&&a[i]<a[i+1])
{
min=f(min,(a[i+1]-a[i])/2);
flag=1;
}
else if(s[i]=='L'&&s[i+1]=='R'&&a[i]>a[i+1])
{
min=f(min,(a[i]-a[i+1])/2);
flag=1;
}
}
if(flag)
printf("%d\n",min);
else
printf("-1\n");
return 0;
}
本文介绍了一个算法问题,即计算在一个直线上发射的多个粒子中,任意两个粒子首次发生碰撞的时间。粒子的位置由坐标表示,每个粒子都有确定的移动方向。通过分析粒子的位置和移动方向,该文提供了一个C语言实现的程序,用于找出首次碰撞的确切时间。
3387

被折叠的 条评论
为什么被折叠?



