Submission #503338

# Submission time Handle Problem Language Result Execution time Memory
503338 2022-01-07T16:48:23 Z rainboy Ekoeko (COCI21_ekoeko) C
30 / 110
1000 ms 2432 KB
#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, a, subtask1;
	long long ans;

	scanf("%d%s", &n, cc);
	subtask1 = 1;
	for (i = 0; i < n * 2; i++)
		if (cc[i] != (i < n ? 'a' : 'b')) {
			subtask1 = 0;
			break;
		}
	if (subtask1) {
		printf("%lld\n", (long long) n * n / 4);
		return 0;
	}
	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 - 1; i >= 0; i--) {
		ans += query(rr[i] - n - 1);
		update(rr[i] - n, n);
	}
	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);
      |  ^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 292 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 292 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Execution timed out 1086 ms 204 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 292 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 7 ms 2380 KB Output is correct
7 Correct 7 ms 2392 KB Output is correct
8 Correct 7 ms 2312 KB Output is correct
9 Correct 6 ms 2288 KB Output is correct
10 Correct 6 ms 2380 KB Output is correct
11 Correct 7 ms 2272 KB Output is correct
12 Correct 6 ms 2252 KB Output is correct
13 Correct 6 ms 2428 KB Output is correct
14 Correct 6 ms 2396 KB Output is correct
15 Correct 6 ms 2432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Execution timed out 1069 ms 268 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 292 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Execution timed out 1086 ms 204 KB Time limit exceeded
7 Halted 0 ms 0 KB -