#include <bits/stdc++.h>
using namespace std;
const int kN = 1e5;
int n, aib[1 + kN], p[kN + 1];
void update(int x) {
for (int i = x; i <= n; i += i & -i) {
++aib[i];
}
}
int query(int x) {
int ans = 0;
for (int i = x; i > 0; i = i & (i - 1)) {
ans += aib[i];
}
return ans;
}
void testCase() {
string a;
cin >> n >> a;
vector<int> pos[26];
for (int i = 0; i < 2 * n; ++i) {
pos[a[i] - 'a'].emplace_back(i);
}
vector<bool> seq(2 * n);
for (int i = 0; i < 26; ++i) {
for (int j = pos[i].size() / 2; j < (int)pos[i].size(); ++j) {
seq[pos[i][j]] = true;
}
}
string s = "$", t = "$";
int64_t ans = 0;
int cnt2 = 0;
for (int i = 0; i < 2 * n; ++i) {
if (!seq[i]) {
ans += cnt2;
s += a[i];
} else {
++cnt2;
t += a[i];
}
}
vector<int> inS[26], inT[26];
for (int i = 1; i <= n; ++i) {
inS[s[i] - 'a'].emplace_back(i);
}
for (int i = 1; i <= n; ++i) {
inT[t[i] - 'a'].emplace_back(i);
}
for (int i = 0; i < 26; ++i) {
for (int j = 0; j < (int)inT[i].size(); ++j) {
int poz = inT[i][j];
p[poz] = inS[i][j];
}
}
for (int i = n; i > 0; --i) {
ans += query(p[i]);
update(p[i]);
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
3 ms |
1544 KB |
Output is correct |
3 |
Correct |
6 ms |
2644 KB |
Output is correct |
4 |
Correct |
9 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
3 ms |
1544 KB |
Output is correct |
3 |
Correct |
6 ms |
2644 KB |
Output is correct |
4 |
Correct |
9 ms |
3540 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
3 ms |
1544 KB |
Output is correct |
3 |
Correct |
6 ms |
2644 KB |
Output is correct |
4 |
Correct |
9 ms |
3540 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
10 ms |
3392 KB |
Output is correct |
7 |
Correct |
9 ms |
3532 KB |
Output is correct |
8 |
Correct |
9 ms |
3424 KB |
Output is correct |
9 |
Correct |
9 ms |
3384 KB |
Output is correct |
10 |
Correct |
13 ms |
3400 KB |
Output is correct |
11 |
Correct |
13 ms |
3436 KB |
Output is correct |
12 |
Correct |
11 ms |
3360 KB |
Output is correct |
13 |
Correct |
11 ms |
3300 KB |
Output is correct |
14 |
Correct |
9 ms |
3368 KB |
Output is correct |
15 |
Correct |
9 ms |
3280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
3 ms |
1544 KB |
Output is correct |
3 |
Correct |
6 ms |
2644 KB |
Output is correct |
4 |
Correct |
9 ms |
3540 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
10 ms |
3392 KB |
Output is correct |
11 |
Correct |
9 ms |
3532 KB |
Output is correct |
12 |
Correct |
9 ms |
3424 KB |
Output is correct |
13 |
Correct |
9 ms |
3384 KB |
Output is correct |
14 |
Correct |
13 ms |
3400 KB |
Output is correct |
15 |
Correct |
13 ms |
3436 KB |
Output is correct |
16 |
Correct |
11 ms |
3360 KB |
Output is correct |
17 |
Correct |
11 ms |
3300 KB |
Output is correct |
18 |
Correct |
9 ms |
3368 KB |
Output is correct |
19 |
Correct |
9 ms |
3280 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
324 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
9 ms |
3528 KB |
Output is correct |
33 |
Correct |
9 ms |
3416 KB |
Output is correct |
34 |
Correct |
10 ms |
3396 KB |
Output is correct |
35 |
Correct |
9 ms |
3408 KB |
Output is correct |
36 |
Correct |
9 ms |
3416 KB |
Output is correct |
37 |
Correct |
9 ms |
3412 KB |
Output is correct |
38 |
Correct |
9 ms |
3416 KB |
Output is correct |
39 |
Correct |
9 ms |
3284 KB |
Output is correct |
40 |
Correct |
9 ms |
3288 KB |
Output is correct |
41 |
Correct |
9 ms |
3364 KB |
Output is correct |