Submission #823351

# Submission time Handle Problem Language Result Execution time Memory
823351 2023-08-12T11:12:20 Z rainboy Line Town (CCO23_day1problem3) C
25 / 25
181 ms 23880 KB
#include <stdio.h>
#include <string.h>

#define N	500000
#define INF	0x3f3f3f3f3f3f3f3fLL

int abs_(int a) { return a > 0 ? a : -a; }
long long min(long long a, long long b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

int aa[N];

int compare(int i, int j) {
	int ai = abs_(aa[i]), aj = abs_(aa[j]);

	if (ai != aj)
		return aj - ai;
	if (aa[i] != aa[j])
		return aa[i] - aa[j];
	return i - j;
}

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k) {
			int c = compare(ii[j], i_);

			if (c == 0)
				j++;
			else if (c < 0) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		}
		sort(ii, l, i);
		l = k;
	}
}

int ft[N];

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 int ii[N], ii1[N], ii2[N], kkp1[N + 1], kkp2[N + 1], kkq1[N + 1], kkq2[N + 1];
	static long long dp[2], dq[2], vvp[N + 1], vvq[N + 1];
	int n, n0, n1, n2, h, h_, h1, r, s, i, k, k1, k2, l1, l2, a;
	long long v, ans;

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d", &aa[i]);
		if (i % 2 != 0)
			aa[i] = -aa[i];
	}
	for (i = 0; i < n; i++)
		ii[i] = i;
	sort(ii, 0, n);
	memset(dp, 0x3f, sizeof dp), dp[0] = 0;
	for (h = 0; h < n; h = h_) {
		a = abs_(aa[ii[h]]);
		if (a == 0)
			break;
		h_ = h, n1 = 0;
		while (h_ < n && aa[i = ii[h_]] == -a)
			ii1[n1++] = i - query(i), h_++;
		n2 = 0;
		while (h_ < n && aa[i = ii[h_]] == a)
			ii2[n2++] = i - query(i), h_++;
		n0 = n - h_;
		for (h1 = h; h1 < h_; h1++)
			update(ii[h1], n);
		memset(dq, 0x3f, sizeof dq);
		for (r = 0; r < 2; r++) {
			if (dp[r] == INF)
				continue;
			kkp1[0] = k1 = 0, kkp2[0] = k2 = 0, vvp[0] = v = 0;
			for (k = 1, l1 = 0, l2 = 0; k <= n1 + n2; k++) {
				if ((k - 1) % 2 == r) {
					if (k1 < n1) {
						i = ii1[k1];
						while (l2 < n2 && ii2[l2] < i)
							l2++;
						v += i - k1 - min(l2, k2);
					}
					k1++;
				} else {
					if (k2 < n2) {
						i = ii2[k2];
						while (l1 < n1 && ii1[l1] < i)
							l1++;
						v += i - k2 - min(l1, k1);
					}
					k2++;
				}
				kkp1[k] = k1, kkp2[k] = k2, vvp[k] = v;
			}
			kkq1[n1 + n2] = k1 = n1, kkq2[n1 + n2] = k2 = n2, vvq[n1 + n2] = v = 0;
			for (k = n1 + n2 - 1, l1 = n1, l2 = n2; k >= 0; k--) {
				if ((n0 + k) % 2 != r) {
					k1--;
					if (k1 >= 0) {
						i = ii1[k1];
						while (l2 > 0 && ii2[l2 - 1] >= i)
							l2--;
						v += n0 - (i - k1 - l2) + max(l2 - k2, 0);
					}
				} else {
					k2--;
					if (k2 >= 0) {
						i = ii2[k2];
						while (l1 > 0 && ii1[l1 - 1] >= i)
							l1--;
						v += n0 - (i - k2 - l1) + max(l1 - k1, 0);
					}
				}
				kkq1[k] = k1, kkq2[k] = k2, vvq[k] = v;
			}
			for (k = 0; k <= n1 + n2; k++)
				if (kkp1[k] == kkq1[k] && kkp2[k] == kkq2[k]) {
					s = (r + k) % 2;
					dq[s] = min(dq[s], dp[r] + vvp[k] + vvq[k]);
				}
		}
		memcpy(dp, dq, sizeof dq);
	}
	ans = min(dp[0], dp[1]);
	if (ans == INF)
		ans = -1;
	printf("%lld\n", ans);
	return 0;
}

Compilation message

Main.c: In function 'main':
Main.c:76:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
Main.c:78:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 488 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 488 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 126 ms 23644 KB Output is correct
16 Correct 138 ms 23652 KB Output is correct
17 Correct 126 ms 23644 KB Output is correct
18 Correct 121 ms 23668 KB Output is correct
19 Correct 121 ms 23880 KB Output is correct
20 Correct 94 ms 23752 KB Output is correct
21 Correct 89 ms 23676 KB Output is correct
22 Correct 92 ms 23648 KB Output is correct
23 Correct 111 ms 23752 KB Output is correct
24 Correct 88 ms 23648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 488 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 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 340 KB Output is correct
25 Correct 1 ms 340 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 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 488 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 126 ms 23644 KB Output is correct
16 Correct 138 ms 23652 KB Output is correct
17 Correct 126 ms 23644 KB Output is correct
18 Correct 121 ms 23668 KB Output is correct
19 Correct 121 ms 23880 KB Output is correct
20 Correct 94 ms 23752 KB Output is correct
21 Correct 89 ms 23676 KB Output is correct
22 Correct 92 ms 23648 KB Output is correct
23 Correct 111 ms 23752 KB Output is correct
24 Correct 88 ms 23648 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 340 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 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 0 ms 212 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 123 ms 14884 KB Output is correct
45 Correct 118 ms 14944 KB Output is correct
46 Correct 109 ms 15016 KB Output is correct
47 Correct 119 ms 14944 KB Output is correct
48 Correct 127 ms 17888 KB Output is correct
49 Correct 96 ms 23756 KB Output is correct
50 Correct 79 ms 13920 KB Output is correct
51 Correct 80 ms 13976 KB Output is correct
52 Correct 96 ms 13924 KB Output is correct
53 Correct 79 ms 13920 KB Output is correct
54 Correct 83 ms 17244 KB Output is correct
55 Correct 85 ms 17192 KB Output is correct
56 Correct 98 ms 17204 KB Output is correct
57 Correct 76 ms 4124 KB Output is correct
58 Correct 70 ms 4244 KB Output is correct
59 Correct 71 ms 4180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 292 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 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 292 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 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 108 ms 10992 KB Output is correct
15 Correct 162 ms 10664 KB Output is correct
16 Correct 152 ms 8984 KB Output is correct
17 Correct 150 ms 8832 KB Output is correct
18 Correct 160 ms 9716 KB Output is correct
19 Correct 153 ms 10176 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 119 ms 11216 KB Output is correct
23 Correct 116 ms 10188 KB Output is correct
24 Correct 103 ms 9272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 488 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 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 340 KB Output is correct
25 Correct 1 ms 340 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 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 292 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 312 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 0 ms 212 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 316 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 0 ms 340 KB Output is correct
57 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 488 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 126 ms 23644 KB Output is correct
16 Correct 138 ms 23652 KB Output is correct
17 Correct 126 ms 23644 KB Output is correct
18 Correct 121 ms 23668 KB Output is correct
19 Correct 121 ms 23880 KB Output is correct
20 Correct 94 ms 23752 KB Output is correct
21 Correct 89 ms 23676 KB Output is correct
22 Correct 92 ms 23648 KB Output is correct
23 Correct 111 ms 23752 KB Output is correct
24 Correct 88 ms 23648 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 340 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 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 0 ms 212 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 123 ms 14884 KB Output is correct
45 Correct 118 ms 14944 KB Output is correct
46 Correct 109 ms 15016 KB Output is correct
47 Correct 119 ms 14944 KB Output is correct
48 Correct 127 ms 17888 KB Output is correct
49 Correct 96 ms 23756 KB Output is correct
50 Correct 79 ms 13920 KB Output is correct
51 Correct 80 ms 13976 KB Output is correct
52 Correct 96 ms 13924 KB Output is correct
53 Correct 79 ms 13920 KB Output is correct
54 Correct 83 ms 17244 KB Output is correct
55 Correct 85 ms 17192 KB Output is correct
56 Correct 98 ms 17204 KB Output is correct
57 Correct 76 ms 4124 KB Output is correct
58 Correct 70 ms 4244 KB Output is correct
59 Correct 71 ms 4180 KB Output is correct
60 Correct 0 ms 340 KB Output is correct
61 Correct 0 ms 292 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 1 ms 340 KB Output is correct
64 Correct 1 ms 340 KB Output is correct
65 Correct 1 ms 340 KB Output is correct
66 Correct 1 ms 312 KB Output is correct
67 Correct 1 ms 340 KB Output is correct
68 Correct 0 ms 212 KB Output is correct
69 Correct 0 ms 340 KB Output is correct
70 Correct 1 ms 340 KB Output is correct
71 Correct 1 ms 316 KB Output is correct
72 Correct 1 ms 340 KB Output is correct
73 Correct 108 ms 10992 KB Output is correct
74 Correct 162 ms 10664 KB Output is correct
75 Correct 152 ms 8984 KB Output is correct
76 Correct 150 ms 8832 KB Output is correct
77 Correct 160 ms 9716 KB Output is correct
78 Correct 153 ms 10176 KB Output is correct
79 Correct 0 ms 340 KB Output is correct
80 Correct 0 ms 340 KB Output is correct
81 Correct 119 ms 11216 KB Output is correct
82 Correct 116 ms 10188 KB Output is correct
83 Correct 103 ms 9272 KB Output is correct
84 Correct 1 ms 340 KB Output is correct
85 Correct 1 ms 340 KB Output is correct
86 Correct 1 ms 340 KB Output is correct
87 Correct 1 ms 340 KB Output is correct
88 Correct 1 ms 340 KB Output is correct
89 Correct 1 ms 340 KB Output is correct
90 Correct 1 ms 340 KB Output is correct
91 Correct 1 ms 340 KB Output is correct
92 Correct 1 ms 340 KB Output is correct
93 Correct 1 ms 340 KB Output is correct
94 Correct 0 ms 340 KB Output is correct
95 Correct 0 ms 340 KB Output is correct
96 Correct 125 ms 13100 KB Output is correct
97 Correct 132 ms 13192 KB Output is correct
98 Correct 125 ms 13124 KB Output is correct
99 Correct 109 ms 9532 KB Output is correct
100 Correct 91 ms 20308 KB Output is correct
101 Correct 160 ms 8132 KB Output is correct
102 Correct 172 ms 10120 KB Output is correct
103 Correct 163 ms 9552 KB Output is correct
104 Correct 172 ms 8648 KB Output is correct
105 Correct 181 ms 7380 KB Output is correct
106 Correct 159 ms 8028 KB Output is correct