#include <bits/stdc++.h>
using namespace std;
const int BASE = 1 << 18;
int TREE[BASE << 1];
int n;
string monochrome;
queue<int> BPOS;
queue<int> WPOS;
void add(int v, int val){
v+=BASE;
TREE[v] += val;
v/=2;
while(v > 0){
int l = 2*v, r = 2*v+1;
TREE[v] = TREE[l] + TREE[r];
v/=2;
}
}
int query(int a, int b){
a += BASE - 1;
b += BASE + 1;
int res = 0;
while(a/2 != b/2){
if(!(a&1)) res += TREE[a+1];
if(b&1) res += TREE[b-1];
a/=2; b/=2;
}
return res;
}
int solve(){
int res = 0;
for(int i = 2*n; i >= n+1; i--){
if(monochrome[i] == 'B'){
int v = WPOS.front();
WPOS.pop();
//cout << "query: " << query(v, BASE-1) << "\n";
res += query(v, BASE-1);
add(v, 1);
}
else if(monochrome[i] == 'W'){
int v = BPOS.front();
BPOS.pop();
//cout << "query: " << query(v, BASE-1) << "\n";
res += query(v, BASE-1);
add(v, 1);
}
}
return res;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
cin >> monochrome;
monochrome = "#" + monochrome;
for(int i = n; i >= 1; i--){
if(monochrome[i] == 'B')
BPOS.push(i);
else if(monochrome[i] == 'W')
WPOS.push(i);
}
cout << solve() << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |