Submission #550658

# Submission time Handle Problem Language Result Execution time Memory
550658 2022-04-18T19:11:48 Z rainboy JOI15_keys (JOI15_keys) C
100 / 100
1 ms 340 KB
#include <stdio.h>

#define N	2000
#define INF	0x3f3f3f3f

int min(int a, int b) { return a < b ? a : b; }

unsigned int X = 12345;

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

int xx[N * 2], xx_[N * 2], yy[N * 2];

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)
			if (xx[ii[j]] == xx[i_])
				j++;
			else if (xx[ii[j]] < xx[i_]) {
				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 main() {
	static int ii[N * 2], dp[N + 1][2];
	int n, m, k, i, i_, j, c;

	scanf("%d%d%d", &n, &m, &k), k = n - k;
	for (i = 0; i < n * 2; i++) {
		scanf("%d", &xx[i]);
		ii[i] = i;
	}
	sort(ii, 0, n * 2);
	for (i = 0; i < n * 2; i++)
		xx_[ii[i]] = i;
	for (i = 0; i + 1 < n * 2; i++)
		yy[i] = xx[ii[i + 1]] - xx[ii[i]];
	for (c = 0; c <= k; c++)
		dp[c][0] = dp[c][1] = INF;
	dp[0][0] = 0;
	for (i = 0; i + 1 < n * 2; i++)
		if ((ii[i] & 1) == 0) {
			if ((ii[i] ^ ii[i + 1]) == 1)
				for (c = k; c >= 0; c--)
					dp[c][0] = min(dp[c][0], c == 0 ? INF : dp[c - 1][0] + yy[i]);
			else if ((ii[i + 1] & 1) == 0) {
				for (i_ = i; (ii[i_] & 1) == 0; i_ = j) {
					j = xx_[ii[i_] ^ 1] - 1;
					for (c = k; c >= 0; c--) {
						dp[c][0] = min(dp[c][0], dp[c][1]);
						dp[c][1] = c == 0 ? INF : min(min(dp[c - 1][0] + yy[i_], dp[c - 1][1]) + yy[j], INF);
					}
				}
				for (c = k; c >= 0; c--)
					dp[c][0] = min(dp[c][0], dp[c][1]), dp[c][1] = INF;
			}
		}
	printf("%d\n", m - dp[k][0]);
	return 0;
}

Compilation message

keys.c: In function 'main':
keys.c:39:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |  scanf("%d%d%d", &n, &m, &k), k = n - k;
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
keys.c:41:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |   scanf("%d", &xx[i]);
      |   ^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 300 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 296 KB Output is correct
11 Correct 1 ms 292 KB Output is correct
12 Correct 1 ms 292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 300 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 296 KB Output is correct
11 Correct 1 ms 292 KB Output is correct
12 Correct 1 ms 292 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct