Submission #500647

#TimeUsernameProblemLanguageResultExecution timeMemory
500647rainboyTortoise (CEOI21_tortoise)C11
48 / 100
212 ms524292 KiB
#include <stdio.h>
#include <string.h>

#define N	5000
#define INF	0x3f3f3f3f3f3f3f3fLL

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

long long eval(long long a, long long b, long long c) { return b - a * c; }

int main() {
	static int aa[N], ll[N], rr[N], tt[N];
	static long long dp[N][N * 2 + 1], dq[N][N * 2 + 1], dr[N * 2 + 1], qu[N * 2 + 1];
	int n, i, j, k, head, cnt;
	long long ans;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (i = 0; i < n; i++) {
		for (j = i - 1; j >= 0; j--)
			if (aa[j] == -1)
				break;
		ll[i] = j;
		for (j = i + 1; j < n; j++)
			if (aa[j] == -1)
				break;
		rr[i] = j;
		tt[i] = min(ll[i] == -1 ? INF : i - ll[i], rr[i] == n ? INF : rr[i] - i) * 2;
	}
	for (i = 0; i < n; i++) {
		memset(dp[i], 0x3f, (n * 2 + 1) * sizeof *dp[i]);
		memset(dq[i], 0x3f, (n * 2 + 1) * sizeof *dq[i]);
	}
	ans = 0;
	for (i = 0; i < n; i++)
		if (aa[i] > 0) {
			for (k = 0; k <= n * 2; k++) {
				if ((j = ll[i]) >= 0)
					dp[i][k] = min(dp[i][k], dq[j][k] + i - j);
				if ((j = rr[i]) < n)
					dp[i][k] = min(dp[i][k], dq[j][k] + j - i);
			}
			dp[i][1] = min(dp[i][1], i);
			head = cnt = 0;
			for (k = 0; k <= n * 2; k++) {
				if (dp[i][k] <= i * 2) {
					while (cnt && eval(qu[head + cnt - 1], dp[i][qu[head + cnt - 1]], tt[i]) >= eval(k, dp[i][k], tt[i]))
						cnt--;
					qu[head + cnt++] = k;
				}
				while (cnt && k - qu[head] >= aa[i])
					head++, cnt--;
				dr[k] = cnt == 0 ? INF : eval(qu[head], dp[i][qu[head]], tt[i]) + (long long) tt[i] * k;
			}
			for (k = 0; k <= n * 2; k++)
				dp[i][k] = dr[k];
			for (k = 0; k <= n * 2; k++) {
				long long t = dp[i][k];

				if (t > i * 2)
					continue;
				ans = max(ans, k);
				if (k < n * 2) {
					if ((j = ll[i]) >= 0)
						dq[j][k + 1] = min(dq[j][k + 1], t + i - j);
					if ((j = rr[i]) < n)
						dq[j][k + 1] = min(dq[j][k + 1], t + j - i);
				}
			}
		} else
			for (k = 0; k <= n * 2; k++) {
				long long t = dq[i][k];

				if (t == INF)
					continue;
				if ((j = rr[i]) < n)
					dq[j][k] = min(dq[j][k], t + j - i);
			}
	ans = -ans;
	for (i = 0; i < n; i++)
		if (aa[i] > 0)
			ans += aa[i];
	printf("%lld\n", ans);
	return 0;
}

Compilation message (stderr)

tortoise.c: In function 'main':
tortoise.c:18:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
tortoise.c:20:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...