#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 1e5 + 5;
const int oo = 1e9, mod = 1e9 + 7;
int n, cnt[30], p[N], ans = 0;
string s, a, b;
vector<int> t1[30], t2[30];
int bittree[N];
void update(int i) {
for (++i; i <= n; i += i & -i)
++bittree[i];
}
int getSum(int i) {
int r = 0;
for (++i; i; i -= i & -i)
r += bittree[i];
return r;
}
void process() {
cin >> n >> s;
for (auto c : s) cnt[c - 'a']++;
foru(i, 0, 25) cnt[i] /= 2;
int x = 0;
foru(i, 0, 2 * n - 1) {
if (cnt[s[i] - 'a']) {
a += s[i], ans += x; cnt[s[i] - 'a']--;
} else {
b += s[i]; x++;
}
}
foru(i, 0, n - 1) {
t1[a[i] - 'a'].pb(i);
t2[b[i] - 'a'].pb(i);
}
foru(i, 0, 25) {
for (int j = 0; j < t1[i].size(); ++j) {
p[t2[i][j]] = t1[i][j];
}
}
foru(i, 0, n - 1) {
ans += i - getSum(p[i]);
update(p[i]);
}
cout << ans;
return;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
/*
Xét các trường hợp đặc biệt
Kiểm tra lại input/output
Cố gắng trâu
Lật ngược bài toán
Keep calm and get VOI
Flow:
*/
Compilation message
Main.cpp: In function 'void process()':
Main.cpp:58:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int j = 0; j < t1[i].size(); ++j) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
1940 KB |
Output is correct |
3 |
Correct |
4 ms |
3100 KB |
Output is correct |
4 |
Correct |
5 ms |
4380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
1940 KB |
Output is correct |
3 |
Correct |
4 ms |
3100 KB |
Output is correct |
4 |
Correct |
5 ms |
4380 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
1940 KB |
Output is correct |
3 |
Correct |
4 ms |
3100 KB |
Output is correct |
4 |
Correct |
5 ms |
4380 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
4324 KB |
Output is correct |
7 |
Correct |
6 ms |
4324 KB |
Output is correct |
8 |
Correct |
6 ms |
4328 KB |
Output is correct |
9 |
Correct |
6 ms |
4368 KB |
Output is correct |
10 |
Correct |
6 ms |
4328 KB |
Output is correct |
11 |
Correct |
6 ms |
4328 KB |
Output is correct |
12 |
Correct |
6 ms |
4328 KB |
Output is correct |
13 |
Correct |
6 ms |
4324 KB |
Output is correct |
14 |
Correct |
5 ms |
4328 KB |
Output is correct |
15 |
Correct |
6 ms |
4092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
472 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
1940 KB |
Output is correct |
3 |
Correct |
4 ms |
3100 KB |
Output is correct |
4 |
Correct |
5 ms |
4380 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
6 ms |
4324 KB |
Output is correct |
11 |
Correct |
6 ms |
4324 KB |
Output is correct |
12 |
Correct |
6 ms |
4328 KB |
Output is correct |
13 |
Correct |
6 ms |
4368 KB |
Output is correct |
14 |
Correct |
6 ms |
4328 KB |
Output is correct |
15 |
Correct |
6 ms |
4328 KB |
Output is correct |
16 |
Correct |
6 ms |
4328 KB |
Output is correct |
17 |
Correct |
6 ms |
4324 KB |
Output is correct |
18 |
Correct |
5 ms |
4328 KB |
Output is correct |
19 |
Correct |
6 ms |
4092 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
472 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
504 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
476 KB |
Output is correct |
32 |
Correct |
6 ms |
4584 KB |
Output is correct |
33 |
Correct |
6 ms |
4324 KB |
Output is correct |
34 |
Correct |
6 ms |
4328 KB |
Output is correct |
35 |
Correct |
6 ms |
4328 KB |
Output is correct |
36 |
Correct |
6 ms |
4360 KB |
Output is correct |
37 |
Correct |
6 ms |
4328 KB |
Output is correct |
38 |
Correct |
6 ms |
4324 KB |
Output is correct |
39 |
Correct |
6 ms |
4328 KB |
Output is correct |
40 |
Correct |
5 ms |
4328 KB |
Output is correct |
41 |
Correct |
5 ms |
4524 KB |
Output is correct |