#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
const int N = 1e5 + 5;
int cnt[50], a[N], st[4 * N];
vector<int> v1, v2, pos[50];
void update(int x, int tl, int tr, int idx) {
//cout << x << " " << tl << " " << tr << " " << idx << "faw\n";
if (tl == tr) st[x]++;
else {
int tm = (tl + tr) / 2;
if (idx <= tm) update(2 * x, tl, tm, idx);
else update(2 * x + 1, tm + 1, tr, idx);
st[x]++;
}
}
int get(int x, int tl, int tr, int l, int r) {
if (l > r or tr < l or r < tl) return 0;
if (l <= tl and tr <= r) return st[x];
int tm = (tl + tr) / 2;
return get(2 * x, tl, tm, l, r) + get(2 * x + 1, tm + 1, tr, l, r);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n;
ll ans = 0;
string s, t1 = "", t2 = "";
cin >> n >> s;
for (int i = 0; i < 2 * n; i++) pos[s[i] - 'a'].push_back(i);
for (int i = 0; i < 26; i++) {
for (int j = 0; j < (int)pos[i].size() / 2; j++) v1.push_back(pos[i][j]);
}
sort(v1.begin(), v1.end());
for (auto x : v1) t1 += s[x];
for (int i = 0; i < n; i++) {
pos[t1[i] - 'a'][cnt[t1[i] - 'a']] = i;
cnt[t1[i] - 'a']++;
}
for (int i = 0; i < n; i++) ans += v1[i] - i;
for (int i = 0; i < 26; i++) {
for (int j = (int)pos[i].size() / 2; j < (int)pos[i].size(); j++) v2.push_back(pos[i][j]);
}
sort(v2.begin(), v2.end());
for (auto x : v2) t2 += s[x];
for (int i = 0; i < n; i++) {
a[i] = pos[t2[i] - 'a'][cnt[t2[i] - 'a'] - (int)pos[t2[i] - 'a'].size() / 2];
cnt[t2[i] - 'a']++;
//cout << t2[i] << " " << a[i] << " " << "flpawkq\n";
ans += get(1, 0, n - 1, a[i] + 1, n - 1);
update(1, 0, n - 1, a[i]);
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
7 ms |
1944 KB |
Output is correct |
3 |
Correct |
14 ms |
3192 KB |
Output is correct |
4 |
Correct |
22 ms |
4032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
7 ms |
1944 KB |
Output is correct |
3 |
Correct |
14 ms |
3192 KB |
Output is correct |
4 |
Correct |
22 ms |
4032 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
7 ms |
1944 KB |
Output is correct |
3 |
Correct |
14 ms |
3192 KB |
Output is correct |
4 |
Correct |
22 ms |
4032 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
31 ms |
4144 KB |
Output is correct |
7 |
Correct |
30 ms |
4096 KB |
Output is correct |
8 |
Correct |
36 ms |
4152 KB |
Output is correct |
9 |
Correct |
33 ms |
4144 KB |
Output is correct |
10 |
Correct |
33 ms |
4052 KB |
Output is correct |
11 |
Correct |
29 ms |
3984 KB |
Output is correct |
12 |
Correct |
35 ms |
4008 KB |
Output is correct |
13 |
Correct |
28 ms |
3988 KB |
Output is correct |
14 |
Correct |
28 ms |
3996 KB |
Output is correct |
15 |
Correct |
30 ms |
4020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
328 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 |
332 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 |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
7 ms |
1944 KB |
Output is correct |
3 |
Correct |
14 ms |
3192 KB |
Output is correct |
4 |
Correct |
22 ms |
4032 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
31 ms |
4144 KB |
Output is correct |
11 |
Correct |
30 ms |
4096 KB |
Output is correct |
12 |
Correct |
36 ms |
4152 KB |
Output is correct |
13 |
Correct |
33 ms |
4144 KB |
Output is correct |
14 |
Correct |
33 ms |
4052 KB |
Output is correct |
15 |
Correct |
29 ms |
3984 KB |
Output is correct |
16 |
Correct |
35 ms |
4008 KB |
Output is correct |
17 |
Correct |
28 ms |
3988 KB |
Output is correct |
18 |
Correct |
28 ms |
3996 KB |
Output is correct |
19 |
Correct |
30 ms |
4020 KB |
Output is correct |
20 |
Correct |
0 ms |
324 KB |
Output is correct |
21 |
Correct |
1 ms |
328 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 |
332 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 |
328 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
34 ms |
4132 KB |
Output is correct |
33 |
Correct |
31 ms |
4092 KB |
Output is correct |
34 |
Correct |
36 ms |
4048 KB |
Output is correct |
35 |
Correct |
30 ms |
4100 KB |
Output is correct |
36 |
Correct |
30 ms |
3996 KB |
Output is correct |
37 |
Correct |
30 ms |
4112 KB |
Output is correct |
38 |
Correct |
29 ms |
4060 KB |
Output is correct |
39 |
Correct |
28 ms |
4064 KB |
Output is correct |
40 |
Correct |
28 ms |
4028 KB |
Output is correct |
41 |
Correct |
28 ms |
3912 KB |
Output is correct |