#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 1e5 + 25;
struct bit {
int tree[MAXN];
void add (int x) {
for (; x < MAXN; x += x & (-x)) {
assert(x < MAXN);
tree[x]++;
}
}
int get (int x) {
int sum = 0; for (; x; x -= x & (-x)) {
assert(x > 0);
sum += tree[x];
}
return sum;
}
} cur;
int n;
string s;
vector <int> occ[26];
signed main () {
ios::sync_with_stdio(0);
cin.tie(0); memset(cur.tree, 0, sizeof(cur.tree));
cin >> n >> s;
s.insert(s.begin(), '0');
for (int i = 1; i <= n; i++) occ[s[i] - 'a'].push_back(i);
for (int i = 0; i < 26; i++) reverse(occ[i].begin(), occ[i].end());
long long cnt = 0;
for (int i = n + 1; i <= 2 * n; i++) {
int u = occ[s[i] - 'a'].back();
occ[s[i] - 'a'].pop_back();
cnt += cur.get(u); cur.add(u);
}
cout << n * (n - 1) / 2 - cnt << '\n';
}
/*aabcdffa
adaffacb
1,2,3,4,5,6,7,8
1,5,2,6,7,8,4,3*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
2140 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
2140 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
2140 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Execution timed out |
1062 ms |
1116 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
2140 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |