Submission #502846

# Submission time Handle Problem Language Result Execution time Memory
502846 2022-01-06T16:06:19 Z rainboy Tortoise (CEOI21_tortoise) C
0 / 100
0 ms 284 KB
/* https://hsin.hr/ceoi/competition/ceoi2021_day2_editorial.pdf */
#include <stdio.h>

#define N	500000
#define N_	(1 << 19)	/* N_ = pow2(ceil(log2(N))) */
#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 tt[N], dd[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)
			if (dd[ii[j]] == dd[i_])
				j++;
			else if (dd[ii[j]] < dd[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 sum[N_ * 2], pref[N_ * 2], ss[N_], kk[N_], n_;

void pul(int i) {
	int l = i << 1, r = l | 1;

	sum[i] = sum[l] + sum[r];
	pref[i] = min(pref[l], sum[l] + pref[r]);
}

void build(int n) {
	int i;

	n_ = 1;
	while (n_ < n)
		n_ <<= 1;
	for (i = 1; i < n; i++)
		ss[i] = 1;
	for (i = 0; i < n_; i++)
		sum[n_ + i] = ss[i], pref[n_ + i] = kk[i] == 0 ? INF : ss[i];
	for (i = n_ - 1; i > 0; i--)
		pul(i);
}

void update(int i, int s, int k) {
	ss[i] += s, kk[i] += k;
	sum[n_ + i] = ss[i], pref[n_ + i] = kk[i] == 0 ? INF : ss[i];
	i += n_;
	while (i > 1)
		pul(i >>= 1);
}

int main() {
	static int aa[N], ii[N], ll[N], rr[N], pp[N];
	int n, n_, i, j;
	long long ans;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (i = 0; i < n; i++)
		ll[i] = aa[i] == -1 ? i : (i == 0 ? -INF : ll[i - 1]);
	for (i = n - 1; i >= 0; i--)
		rr[i] = aa[i] == -1 ? i : (i + 1 == n ? INF : rr[i + 1]);
	n_ = 0;
	for (i = 0; i < n; i++)
		if (aa[i] > 0) {
			if (i - ll[i] <= rr[i] - i)
				tt[i] = -1, dd[i] = i - ll[i];
			else
				tt[i] = 1, dd[i] = rr[i] - i;
			ii[n_++] = i;
		}
	ans = 0;
	for (i = 0; i < n; i++)
		if (aa[i] > 0)
			ans += aa[i];
	for (j = 0; j < n; j++)
		if (aa[j] == -1) {
			i = j - 1;
			while (i >= 0 && aa[i] == 0)
				i--;
			if (i >= 0 && aa[i] > 0)
				pp[j] = i, kk[i]++, ans--;
			else
				pp[j] = -1;
		}
	sort(ii, 0, n_);
	build(n);
	for (i = 0; i < n_; i++) {
		int i_ = ii[i], r = rr[i_], p;

		while (kk[i_] <= aa[i_])
			if (tt[i_] == -1) {
				if (kk[i_] == aa[i_])
					break;
				if (kk[i_] == 0) {
					update(i_, 0, 1);
					if (i_ + 1 < n)
						update(i_ + 1, -dd[i_] * 2, 0);
				} else
					update(i_, -dd[i_] * 2, 1);
				if (pref[1] >= 0)
					ans--;
				else {
					if (kk[i_] == 1) {
						update(i_, 0, -1);
						if (i_ + 1 < n)
							update(i_ + 1, dd[i_] * 2, 0);
					} else
						update(i_, dd[i_] * 2, -1);
					break;
				}
			} else {
				p = r == INF ? INF : pp[r];
				if (i_ != p)
					break;
				while (p >= 0 && kk[p] == aa[p])
					p--;
				if (p < 0 || aa[p] == -1)
					break;
				update(i_, -dd[i_] * 2, 1);
				if (pref[1] >= 0)
					pp[r] = p, ans--;
				else {
					update(i_, dd[i_] * 2, -1);
					break;
				}
			}
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

tortoise.c: In function 'main':
tortoise.c:73:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
tortoise.c:75:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 284 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 284 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 284 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 284 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 284 KB Output isn't correct
2 Halted 0 ms 0 KB -