#include <bits/stdc++.h>
using namespace std;
const int N = (1e5 + 5) * 2;
int n, cnt[26];
long long ans;
string str;
vector<char> strv;
vector<int> indx[26], v;
struct Fenwick{
int tree[N];
int getsum(int r){
int sum = 0;
for(int i = r; i > 0; i -= i & -i) sum += tree[i];
return sum;
}
int query(int l, int r){
if(l > r) return 0;
return getsum(r) - getsum(l - 1);
}
void update(int pos, int val){
for(int i = pos; i < N; i += i & -i) tree[i] += val;
}
};
Fenwick fw;
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n;
cin >> str;
n *= 2;
for(auto c : str) cnt[c - 'a']++;
for(int i = 0; i < n; i++){
if(cnt[str[i] - 'a'] > 0){
strv.push_back(str[i]);
cnt[str[i] - 'a'] -= 2;
}
}
for(int i = 0, m = strv.size(); i < m; i++) strv.push_back(strv[i]);
for(int i = 0; i < n; i++) indx[strv[i] - 'a'].push_back(i + 1);
for(int i = 0; i < 26; i++) reverse(indx[i].begin(), indx[i].end());
for(int i = 0; i < n; i++){
v.push_back(indx[str[i] - 'a'].back());
indx[str[i] - 'a'].pop_back();
}
for(auto i : v){
ans += fw.query(i + 1, n);
fw.update(i, 1);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
1612 KB |
Output is correct |
3 |
Correct |
9 ms |
2424 KB |
Output is correct |
4 |
Correct |
12 ms |
3296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
1612 KB |
Output is correct |
3 |
Correct |
9 ms |
2424 KB |
Output is correct |
4 |
Correct |
12 ms |
3296 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
1612 KB |
Output is correct |
3 |
Correct |
9 ms |
2424 KB |
Output is correct |
4 |
Correct |
12 ms |
3296 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
13 ms |
3584 KB |
Output is correct |
7 |
Correct |
13 ms |
3588 KB |
Output is correct |
8 |
Correct |
13 ms |
3532 KB |
Output is correct |
9 |
Correct |
14 ms |
3472 KB |
Output is correct |
10 |
Correct |
14 ms |
3540 KB |
Output is correct |
11 |
Correct |
13 ms |
3464 KB |
Output is correct |
12 |
Correct |
12 ms |
3436 KB |
Output is correct |
13 |
Correct |
13 ms |
3540 KB |
Output is correct |
14 |
Correct |
12 ms |
3412 KB |
Output is correct |
15 |
Correct |
14 ms |
3348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
1612 KB |
Output is correct |
3 |
Correct |
9 ms |
2424 KB |
Output is correct |
4 |
Correct |
12 ms |
3296 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
13 ms |
3584 KB |
Output is correct |
11 |
Correct |
13 ms |
3588 KB |
Output is correct |
12 |
Correct |
13 ms |
3532 KB |
Output is correct |
13 |
Correct |
14 ms |
3472 KB |
Output is correct |
14 |
Correct |
14 ms |
3540 KB |
Output is correct |
15 |
Correct |
13 ms |
3464 KB |
Output is correct |
16 |
Correct |
12 ms |
3436 KB |
Output is correct |
17 |
Correct |
13 ms |
3540 KB |
Output is correct |
18 |
Correct |
12 ms |
3412 KB |
Output is correct |
19 |
Correct |
14 ms |
3348 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
13 ms |
3504 KB |
Output is correct |
33 |
Correct |
12 ms |
3652 KB |
Output is correct |
34 |
Correct |
13 ms |
3592 KB |
Output is correct |
35 |
Correct |
14 ms |
3548 KB |
Output is correct |
36 |
Correct |
14 ms |
3516 KB |
Output is correct |
37 |
Correct |
13 ms |
3584 KB |
Output is correct |
38 |
Correct |
13 ms |
3492 KB |
Output is correct |
39 |
Correct |
12 ms |
3328 KB |
Output is correct |
40 |
Correct |
13 ms |
3412 KB |
Output is correct |
41 |
Correct |
13 ms |
3400 KB |
Output is correct |