-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathc.cpp
73 lines (70 loc) · 2.01 KB
/
c.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
#define endl "\n"
#define eat cin
#define moo cout
#define f first
#define s second
using namespace std;
int T, N, M, x[300000], id[300000];
char c[300000];
int32_t main(){
eat.tie(0) -> sync_with_stdio(0);
eat >> T;
while(T--){
eat >> N >> M;
for(int i = 0; i < N; i++){
eat >> x[i];
}
for(int i = 0; i < N; i++){
eat >> c[i];
}
iota(id, id+N, 0);
sort(id, id+N, [](int a, int b){
return x[a] < x[b];
});
stack<int> s[2];
queue<int> q[2];
int ans[N];
fill(ans, ans+N, -1);
for(int i = 0; i < N; i++){
int cur = id[i], pol = x[cur] % 2;
if(c[cur] == 'R'){
s[pol].push(cur);
}else{
if(s[pol].empty()){
q[pol].push(cur);
}else{
int col = s[pol].top();
s[pol].pop();
ans[cur] = ans[col] = (x[cur] - x[col]) / 2;
}
}
}
for(int i = 0; i < 2; i++){
while((int)s[i].size() >= 2){
int col1 = s[i].top(); s[i].pop();
int col2 = s[i].top(); s[i].pop();
int x1 = M + M - x[col1], x2 = x[col2];
ans[col1] = ans[col2] = (x1 - x2) / 2;
}
}
for(int i = 0; i < 2; i++){
while((int)q[i].size() >= 2){
int col1 = q[i].front(); q[i].pop();
int col2 = q[i].front(); q[i].pop();
int x1 = -x[col1], x2 = x[col2];
ans[col1] = ans[col2] = (x2 - x1) / 2;
}
}
for(int i = 0; i < 2; i++){
if(q[i].size() && s[i].size()){
int c1 = q[i].front(), c2 = s[i].top();
ans[c1] = ans[c2] = (M + M - x[c2] + x[c1]) / 2;
}
}
for(int i = 0; i < N; i++){
moo << ans[i] << " ";
}
moo << endl;
}
}