Submission #788182

# Submission time Handle Problem Language Result Execution time Memory
788182 2023-07-19T21:52:50 Z rainboy Fruits (NOI22_fruits) C
35 / 100
1000 ms 15320 KB
#include <stdio.h>
#include <string.h>

#define N	400000

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

long long ss[N + 1];
int xx[N], yy[N], dd[N]; long long dp[N]; int pp[N], qq[N], hh[N], tt[N];
int n, m;

int cross(int i, int j) {
	int lower = -1, upper = m - yy[j] + 1;

	while (upper - lower > 1) {
		int d = (lower + upper) / 2;

		if (dp[i] - ss[yy[i] + d] > dp[j] - ss[yy[j] + d])
			upper = d;
		else
			lower = d;
	}
	return lower;
}

void merge(int i, int j) {
	int u, v, d;

	u = tt[i], v = hh[j];
	while (1) {
		d = cross(u, v);
		if (u != hh[i] && cross(pp[u], u) <= d)
			u = pp[u];
		else if (v != tt[j] && cross(v, qq[v]) >= d)
			v = qq[v];
		else {
			qq[u] = v, pp[v] = u;
			break;
		}
	}
	hh[j] = hh[i];
}

long long query(int i, int d) {
	while (hh[i] != tt[i] && cross(hh[i], qq[hh[i]]) >= d)
		hh[i] = qq[hh[i]];
	return dp[hh[i]] - ss[yy[hh[i]] + d];
}

int main() {
	static int aa[N], ii[N], ww[N], qu[N];
	int cnt, h, i, i_, j, a, x, y, d;
	long long z;

	scanf("%d", &n);
	memset(ii, -1, n * sizeof *ii);
	for (i = 0; i < n; i++) {
		scanf("%d", &aa[i]), aa[i]--;
		if (aa[i] >= 0)
			ii[aa[i]] = i;
	}
	for (a = 0; a < n; a++)
		scanf("%d", &ww[a]);
	m = 0;
	for (a = 0; a < n; a++)
		if ((i = ii[a]) != -1)
			xx[i] = m;
		else
			ss[m + 1] = ss[m] + ww[a], m++;
	if (m == n) {
		for (i = 0; i < n; i++)
			printf("%lld ", ss[n] - ss[n - 1 - i]);
		printf("\n");
		return 0;
	}
	for (i = 0; i < n; i++)
		pp[i] = qq[i] = -1, hh[i] = tt[i] = i;
	y = 0, a = -1, cnt = 0, h = 0;
	for (j = 0; j < n; j++) {
		if (aa[j] == -2)
			y++;
		else if (a > aa[j])
			aa[j] = -1;
		else {
			a = aa[j];
			yy[j] = y, dd[j] = xx[j] - yy[j];
			if (dd[j] < 0)
				aa[j] = -1;
		}
		if (aa[j] >= 0) {
			i_ = -1;
			while (cnt && dd[i = qu[cnt - 1]] < dd[j]) {
				if (i_ == -1)
					i_ = i;
				else
					merge(i, i_);
				cnt--;
			}
			dp[j] = cnt ? dp[i] - ss[xx[i]] : -ss[dd[j]];
			if (i_ != -1)
				dp[j] = max(dp[j], query(i_, dd[j]));
			dp[j] += ss[xx[j]] + ww[aa[j]];
			if (i_ != -1)
				merge(i_, j);
			qu[cnt++] = j;
		}
		x = m, d = x - y;
		h = min(h, cnt);
		while (h < cnt && dd[qu[h]] >= d)
			h++;
		ii[j] = h == 0 ? -1 : qu[h - 1];
		z = 0;
		for (i = j; i > ii[j]; i--) {
			if (aa[i] < 0)
				continue;
			if (dd[i] >= d)
				break;
			z = max(z, dp[i] + ss[x] - ss[yy[i] + d]);
		}
		if (i >= 0)
			z = max(z, dp[i] + ss[x] - ss[xx[i]]);
		else
			z = max(z, ss[x] - ss[d]);
		printf("%lld ", z);
	}
	printf("\n");
	return 0;
}

Compilation message

Main.c: In function 'main':
Main.c:56:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
Main.c:59:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |   scanf("%d", &aa[i]), aa[i]--;
      |   ^~~~~~~~~~~~~~~~~~~
Main.c:64:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |   scanf("%d", &ww[a]);
      |   ^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 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 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 5 ms 940 KB Output is correct
4 Correct 46 ms 6892 KB Output is correct
5 Correct 88 ms 13520 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 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 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 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 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 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 1 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 1 ms 424 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 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 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 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 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 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 1 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 1 ms 424 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 468 KB Output is correct
29 Correct 3 ms 468 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 2 ms 432 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 2 ms 340 KB Output is correct
39 Correct 2 ms 340 KB Output is correct
40 Correct 2 ms 468 KB Output is correct
41 Correct 2 ms 468 KB Output is correct
42 Correct 3 ms 468 KB Output is correct
43 Correct 3 ms 468 KB Output is correct
44 Correct 3 ms 468 KB Output is correct
45 Correct 4 ms 480 KB Output is correct
46 Correct 3 ms 468 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1080 ms 15320 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 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 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 5 ms 940 KB Output is correct
14 Correct 46 ms 6892 KB Output is correct
15 Correct 88 ms 13520 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 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 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 1 ms 424 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 468 KB Output is correct
35 Correct 3 ms 468 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 2 ms 432 KB Output is correct
40 Correct 3 ms 468 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 2 ms 468 KB Output is correct
48 Correct 3 ms 468 KB Output is correct
49 Correct 3 ms 468 KB Output is correct
50 Correct 3 ms 468 KB Output is correct
51 Correct 4 ms 480 KB Output is correct
52 Correct 3 ms 468 KB Output is correct
53 Correct 0 ms 340 KB Output is correct
54 Correct 0 ms 340 KB Output is correct
55 Execution timed out 1080 ms 15320 KB Time limit exceeded
56 Halted 0 ms 0 KB -