思路:只要考虑相邻的点即可,如果相邻的两个点左边在向右运动,右半在向左运动,那么算出相遇时间,与最小值进行比较。
#include <iostream>
#include <string>
using namespace std;
const int MaxN=200005;
const int MaxX=1e9+5;
int n,MinTime;
string dir;
int x[MaxN],d[MaxN];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
cin>>dir;
for (int i=1; i<=n; i++) {
if (dir[i-1]=='L') d[i]=0;
else d[i]=1;
cin>>x[i];
}
MinTime=MaxX;
for (int i=2; i<=n; i++) {
if (dir[i-1]=='L' && dir[i-2]=='R') {
if (((x[i]-x[i-1])/2)<MinTime) MinTime=(x[i]-x[i-1])/2;
}
}
if (MinTime<MaxX) cout<<MinTime<<"\n";
else cout<<"-1\n";
}