Submission #684851

# Submission time Handle Problem Language Result Execution time Memory
684851 2023-01-22T16:58:07 Z rainboy Long Distance Coach (JOI17_coach) C
100 / 100
192 ms 17864 KB
#include <stdio.h>

#define N	200002
#define M	200001
#define INF	0x3f3f3f3f3f3f3f3fLL

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

unsigned int X = 12345;

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

long long yy[N], aa[N]; int cc[N], n;
long long qq[M], rr[M]; int m;
long long l, t;

long long *vv;

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 (vv[ii[j]] == vv[i_])
				j++;
			else if (vv[ii[j]] < vv[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;
	}
}

long double cross(long long x1, long long y1, long long x2, long long y2) {
	return (long double) x1 * y2 - (long double) x2 * y1;
}

long long dp[N], dq[N];

long double crossp(int i, int j, int k) {
	return cross(j - i, dp[j] - dp[i], k - i, dp[k] - dp[i]);
}

long double crossq(int i, int j, int k) {
	return cross(j - i, dq[j] - dq[i], k - i, dq[k] - dq[i]);
}

long long evalp(int i, long long x) {
	return dp[i] + x * i;
}

long long evalq(int i, long long x) {
	return dq[i] + x * i;
}

int main() {
	static int hh[M], ii[N], qu1[N], qu2[N];
	int cnt1, cnt2, w, g, h, i, j, lower, upper;
	long long x, a, ans;

	scanf("%lld%d%d%d%lld", &l, &m, &n, &w, &t), n += 2, m++;
	for (h = 0; h + 1 < m; h++) {
		scanf("%lld", &x);
		qq[h] = x / t, rr[h] = x % t;
	}
	qq[m - 1] = l / t, rr[h] = l % t;
	for (h = 0; h < m; h++)
		hh[h] = h;
	vv = rr, sort(hh, 0, m);
	yy[0] = 0, cc[0] = 0;
	for (i = 1; i < n; i++)
		scanf("%lld%d", &yy[i], &cc[i]);
	yy[n - 1] = t, cc[n - 1] = 0;
	for (i = 0; i < n; i++)
		ii[i] = i;
	vv = yy, sort(ii, 0, n);
	for (i = 0; i + 1 < n; i++)
		aa[i] = qq[m - 1] + (rr[m - 1] >= yy[ii[i]] ? 1 : 0);
	cnt1 = cnt2 = 0;
	for (j = 0, h = 0; j < n; j++) {
		if (j == 0)
			dp[0] = aa[0] * w - cc[0], dq[0] = INF;
		else {
			a = INF;
			while (h < m && rr[hh[h]] < yy[ii[j]])
				a = min(a, qq[hh[h++]]);
			if (a == INF)
				dq[j] = min(dq[j - 1] + aa[j - 1] * w, dp[j - 1]);
			else {
				dq[j] = INF;
				if (cnt1) {
					lower = -1, upper = cnt1 - 1;
					while (upper - lower > 1) {
						g = (lower + upper) / 2;
						if (evalp(qu1[g], -a * w) > evalp(qu1[g + 1], -a * w))
							lower = g;
						else
							upper = g;
					}
					g = upper;
					dq[j] = min(dq[j], evalp(qu1[g], -a * w) + a * w * (j - 1));
				}
				if (cnt2) {
					lower = -1, upper = cnt2 - 1;
					while (upper - lower > 1) {
						g = (lower + upper) / 2;
						if (evalq(qu2[g], -a * w) > evalq(qu2[g + 1], -a * w))
							lower = g;
						else
							upper = g;
					}
					g = upper;
					dq[j] = min(dq[j], evalq(qu2[g], -a * w) + a * w * j);
				}
			}
			dp[j] = dq[j] + aa[j] * w - cc[ii[j]];
		}
		if (dp[j] != INF) {
			while (cnt1 >= 2 && crossp(qu1[cnt1 - 2], qu1[cnt1 - 1], j) <= 0)
				cnt1--;
			qu1[cnt1++] = j;
		}
		if (dq[j] != INF) {
			while (cnt2 >= 2 && crossq(qu2[cnt2 - 2], qu2[cnt2 - 1], j) <= 0)
				cnt2--;
			qu2[cnt2++] = j;
		}
	}
	ans = dp[n - 1];
	for (i = 0; i < n; i++)
		ans += cc[i];
	printf("%lld\n", ans);
	return 0;
}

Compilation message

coach.c: In function 'main':
coach.c:67:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |  scanf("%lld%d%d%d%lld", &l, &m, &n, &w, &t), n += 2, m++;
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
coach.c:69:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |   scanf("%lld", &x);
      |   ^~~~~~~~~~~~~~~~~
coach.c:78:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |   scanf("%lld%d", &yy[i], &cc[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 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 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 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 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 0 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 0 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 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 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 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 0 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 0 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 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 1 ms 340 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 1 ms 368 KB Output is correct
48 Correct 2 ms 340 KB Output is correct
49 Correct 2 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 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 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 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 0 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 0 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 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 1 ms 340 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 1 ms 368 KB Output is correct
48 Correct 2 ms 340 KB Output is correct
49 Correct 2 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 149 ms 11992 KB Output is correct
53 Correct 118 ms 17752 KB Output is correct
54 Correct 168 ms 16928 KB Output is correct
55 Correct 155 ms 17032 KB Output is correct
56 Correct 151 ms 17356 KB Output is correct
57 Correct 149 ms 17144 KB Output is correct
58 Correct 154 ms 17624 KB Output is correct
59 Correct 153 ms 17100 KB Output is correct
60 Correct 143 ms 16868 KB Output is correct
61 Correct 154 ms 17024 KB Output is correct
62 Correct 166 ms 17024 KB Output is correct
63 Correct 139 ms 17484 KB Output is correct
64 Correct 107 ms 17768 KB Output is correct
65 Correct 124 ms 17816 KB Output is correct
66 Correct 123 ms 17712 KB Output is correct
67 Correct 123 ms 17748 KB Output is correct
68 Correct 119 ms 17772 KB Output is correct
69 Correct 154 ms 17568 KB Output is correct
70 Correct 192 ms 17564 KB Output is correct
71 Correct 162 ms 17468 KB Output is correct
72 Correct 152 ms 17444 KB Output is correct
73 Correct 161 ms 17508 KB Output is correct
74 Correct 170 ms 17524 KB Output is correct
75 Correct 152 ms 17556 KB Output is correct
76 Correct 154 ms 17864 KB Output is correct
77 Correct 159 ms 17360 KB Output is correct
78 Correct 155 ms 17652 KB Output is correct
79 Correct 150 ms 17100 KB Output is correct
80 Correct 152 ms 17100 KB Output is correct