Submission #473622

# Submission time Handle Problem Language Result Execution time Memory
473622 2021-09-15T17:35:24 Z rainboy Cover (COCI18_cover) C
120 / 120
3 ms 332 KB
#include <stdio.h>

#define N	5000
#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;
}

int xx[N], yy[N];

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 = xx[ii[j]] != xx[i_] ?  xx[ii[j]] - xx[i_] : yy[ii[j]] - yy[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;
	}
}

long long xx_[N], yy_[N];

double cross(int i, int j, int k) {
	return (double) (xx_[j] - xx_[i]) * (yy_[k] - yy_[i]) - (double) (xx_[k] - xx_[i]) * (yy_[j] - yy_[i]);
}

long long eval(int i, int x) {
	return xx_[i] * x + yy_[i];
}

int main() {
	static int ii[N], qu[N];
	static long long dp[N + 1];
	int n, n_, i, j, head, cnt;

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d%d", &xx[i], &yy[i]);
		if (xx[i] < 0)
			xx[i] = -xx[i];
		if (yy[i] < 0)
			yy[i] = -yy[i];
		ii[i] = i;
	}
	sort(ii, 0, n);
	n_ = 0;
	for (i = 0; i < n; i++) {
		while (n_ > 0 && yy[ii[n_ - 1]] <= yy[ii[i]])
			n_--;
		ii[n_++] = ii[i];
	}
	n = n_;
	dp[0] = 0;
	head = cnt = 0;
	for (j = 1; j <= n; j++) {
		int x;

		xx_[j - 1] = yy[ii[j - 1]], yy_[j - 1] = dp[j - 1];
		while (cnt >= 2 && cross(qu[head + cnt - 2], qu[head + cnt - 1], j - 1) >= 0)
			cnt--;
		qu[head + cnt++] = j - 1;
		x = xx[ii[j - 1]];
		while (cnt >= 2 && eval(qu[head], x) >= eval(qu[head + 1], x))
			head++, cnt--;
		dp[j] = eval(qu[head], x);
	}
	printf("%lld\n", dp[n] * 4);
	return 0;
}

Compilation message

cover.c: In function 'main':
cover.c:53:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
cover.c:55:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |   scanf("%d%d", &xx[i], &yy[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 3 ms 332 KB Output is correct