#include <stdio.h>
#include <string.h>
#define N 100000
#define A 26
int ft[N * 2];
void update(int i, int n) {
while (i < n) {
ft[i]++;
i |= i + 1;
}
}
int query(int i) {
int x = 0;
while (i >= 0) {
x += ft[i];
i &= i + 1, i--;
}
return x;
}
int main() {
static char cc[N * 2 + 1];
static int kk[A], hh[A], ii[N * 2], aa[N * 2], rr[N * 2];
int n, h, h_, i, k, a;
long long ans;
scanf("%d%s", &n, cc);
for (i = 0; i < n * 2; i++)
kk[cc[i] - 'a']++;
for (a = 1; a < A; a++)
hh[a] = hh[a - 1] + kk[a - 1];
for (i = 0; i < n * 2; i++)
ii[aa[i] = hh[cc[i] - 'a']++] = i;
memset(rr, -1, n * 2 * sizeof *rr);
for (a = 0, h = 0; a < A; h += kk[a], a++)
for (h_ = h; h_ < h + kk[a] / 2; h_++)
rr[ii[h_]] = ii[h_ + kk[a] / 2];
ans = 0;
for (i = n * 2 - 1, k = 0; i >= 0; i--)
if (rr[i] != -1)
k++;
else
ans += k;
for (i = n * 2 - 1, k = 0; i >= 0; i--)
if (rr[i] != -1) {
ans += query(rr[i] - 1);
update(rr[i], n * 2);
}
printf("%lld\n", ans);
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:32:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | scanf("%d%s", &n, cc);
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
972 KB |
Output is correct |
3 |
Correct |
4 ms |
1740 KB |
Output is correct |
4 |
Correct |
6 ms |
2380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
972 KB |
Output is correct |
3 |
Correct |
4 ms |
1740 KB |
Output is correct |
4 |
Correct |
6 ms |
2380 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
276 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
972 KB |
Output is correct |
3 |
Correct |
4 ms |
1740 KB |
Output is correct |
4 |
Correct |
6 ms |
2380 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
8 ms |
2380 KB |
Output is correct |
7 |
Correct |
6 ms |
2420 KB |
Output is correct |
8 |
Correct |
7 ms |
2380 KB |
Output is correct |
9 |
Correct |
6 ms |
2328 KB |
Output is correct |
10 |
Correct |
6 ms |
2252 KB |
Output is correct |
11 |
Correct |
6 ms |
2252 KB |
Output is correct |
12 |
Correct |
6 ms |
2252 KB |
Output is correct |
13 |
Correct |
6 ms |
2252 KB |
Output is correct |
14 |
Correct |
7 ms |
2252 KB |
Output is correct |
15 |
Correct |
6 ms |
2252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
972 KB |
Output is correct |
3 |
Correct |
4 ms |
1740 KB |
Output is correct |
4 |
Correct |
6 ms |
2380 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
276 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
8 ms |
2380 KB |
Output is correct |
11 |
Correct |
6 ms |
2420 KB |
Output is correct |
12 |
Correct |
7 ms |
2380 KB |
Output is correct |
13 |
Correct |
6 ms |
2328 KB |
Output is correct |
14 |
Correct |
6 ms |
2252 KB |
Output is correct |
15 |
Correct |
6 ms |
2252 KB |
Output is correct |
16 |
Correct |
6 ms |
2252 KB |
Output is correct |
17 |
Correct |
6 ms |
2252 KB |
Output is correct |
18 |
Correct |
7 ms |
2252 KB |
Output is correct |
19 |
Correct |
6 ms |
2252 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
0 ms |
204 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
8 ms |
2592 KB |
Output is correct |
33 |
Correct |
7 ms |
2508 KB |
Output is correct |
34 |
Correct |
7 ms |
2508 KB |
Output is correct |
35 |
Correct |
6 ms |
2508 KB |
Output is correct |
36 |
Correct |
6 ms |
2488 KB |
Output is correct |
37 |
Correct |
7 ms |
2508 KB |
Output is correct |
38 |
Correct |
6 ms |
2476 KB |
Output is correct |
39 |
Correct |
6 ms |
2360 KB |
Output is correct |
40 |
Correct |
6 ms |
2380 KB |
Output is correct |
41 |
Correct |
6 ms |
2380 KB |
Output is correct |