给出n个粒子,对应下一行给出每个粒子移动的方向,每个粒子的移动速度都是1单位,接下来一行对应给出每个粒子初始的位子(保证递增),问第一次反生粒子碰撞的时间。
思路:
因为保证粒子位子的递增性,还有粒子移动方向的特性,从第一个粒子开始便利,遇到R之后记录当前R的位子posr,之后每一次遇到L posl,对应和前边遇到的R进行一次计算:(posl-posr)/2,然后维护其最小值即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
char a[10000000];
int aa[10000000];
int main()
{
int n;
while(~scanf("%d",&n))
{
scanf("%s",a);
for(int i=0;i<n;i++)
{
scanf("%d",&aa[i]);
}
int rr=0;
int ll=0;
for(int i=0;i<n;i++)
{
if(rr==0)
{
if(a[i]=='R')
{
rr=1;
}
}
else
{
if(a[i]=='L')
{
ll=1;
}
}
}
if(rr==1&&ll==1)
{
int output=0x3f3f3f3f;
int posr=-1;
for(int i=0;i<n;i++)
{
if(a[i]=='R')
{
posr=i;
}
else
{
if(posr==-1)continue;
else
{
output=min(output,(aa[i]-aa[posr])/2);
}
}
}
printf("%d\n",output);
}
else printf("-1\n");
}
}