This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
int R,N,X[600006]; long long S[600006];
char C[600006],P[200006]; int pi[200006];
long long abs(long long a){return a < 0 ? -a : a;}
int main()
{
scanf ("%d %d",&R,&N); N *= 2;
for (int i=N;i<2*N;i++){
scanf ("%d %c",&X[i],&P[i-N]);
X[i-N] = X[i] - R;
X[i+N] = X[i] + R;
C[i-N] = C[i+N] = C[i] = P[i-N];
P[i-N] = (P[i-N] == 'B') ? 'W' : 'B';
}
S[0] = X[0];
for (int i=1;i<3*N;i++) S[i] = S[i-1] + X[i];
pi[0] = -1;
for (int i=1,k=-1;i<N;i++){
while (k >= 0 && P[k+1] != P[i]) k = pi[k];
if (P[k+1] == P[i]) k++;
pi[i] = k;
}
long long ans = 1ll << 62;
for (int i=0,k=-1;i<6*N;i++){
while (k >= 0 && P[k+1] != C[i]) k = pi[k];
if (P[k+1] == C[i]){
k++;
if (k == N-1){
long long a = S[2*N-1] - S[N-1];
long long b = S[i] - S[i-N];
long long c = abs(a - b);
if (ans > c) ans = c;
k = pi[k];
}
else continue;
}
}
if (ans == 1ll << 62) ans = -1;
printf ("%lld\n",ans);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |