Submission #439946

# Submission time Handle Problem Language Result Execution time Memory
439946 2021-07-01T09:19:47 Z dongliu0426 Feast (NOI19_feast) C++14
71 / 100
439 ms 20304 KB
#include <bits/stdc++.h>
using namespace std;

#define N	300000
#define INF	0x3f3f3f3f3f3f3f3f

int n, k, a[N + 1];
pair<long double, int> dp[N + 1][2];

template<class T, class U> T lastTrue(T lo, T hi, U f) {
	assert(lo <= hi); --lo; /* assuming f is decreasing */
	while (lo < hi) { /* find last index such that f is true */
		T mid = lo + (hi - lo + 1) / 2;
		f(mid) ? lo = mid : hi = mid - 1;
	} return lo;
}

int main() {
	scanf("%d%d", &n, &k);
	for (int i = 1; i <= n; ++i)
		scanf("%d", a + i);
	auto check = [&](long double penalty) {
		dp[0][0] = {0, 0}, dp[0][1] = {-INF, 0};
		for (int i = 1; i <= n; ++i) {
			pair<long double, int> last = max(dp[i - 1][0], dp[i - 1][1]);
			dp[i][0] = max(make_pair(last.first - penalty, last.second + 1), last);
			dp[i][1] = max(make_pair(dp[i - 1][1].first + a[i], dp[i - 1][1].second),
				make_pair(dp[i - 1][0].first + a[i] - penalty, dp[i - 1][0].second + 1));
		}
		return max(dp[n][0], dp[n][1]).second >= k;
	};
	long double lower = 0, upper = INF;
	for (int it = 0; it < 50; ++it) {
		long double mid = (lower + upper) / 2;
		if (check(mid)) {
			lower = mid;
		} else {
			upper = mid;
		}
	}
	long double penalty = lower;
	check(penalty);
	printf("%lld\n", (long long) round(penalty * k + max(dp[n][0], dp[n][1]).first));
}

Compilation message

feast.cpp: In function 'int main()':
feast.cpp:19:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |  scanf("%d%d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~
feast.cpp:21:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |   scanf("%d", a + i);
      |   ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 390 ms 19616 KB Output is correct
2 Correct 439 ms 19924 KB Output is correct
3 Correct 400 ms 20208 KB Output is correct
4 Correct 419 ms 20072 KB Output is correct
5 Correct 402 ms 19968 KB Output is correct
6 Correct 382 ms 19656 KB Output is correct
7 Correct 408 ms 19572 KB Output is correct
8 Correct 396 ms 20036 KB Output is correct
9 Correct 383 ms 19680 KB Output is correct
10 Correct 384 ms 19912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 341 ms 19780 KB Output is correct
2 Correct 358 ms 20304 KB Output is correct
3 Correct 341 ms 19732 KB Output is correct
4 Correct 348 ms 19888 KB Output is correct
5 Correct 346 ms 19652 KB Output is correct
6 Correct 350 ms 19712 KB Output is correct
7 Correct 360 ms 20228 KB Output is correct
8 Correct 358 ms 20232 KB Output is correct
9 Correct 358 ms 19496 KB Output is correct
10 Correct 358 ms 20100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 392 ms 19916 KB Output is correct
2 Correct 390 ms 19756 KB Output is correct
3 Correct 374 ms 20036 KB Output is correct
4 Correct 387 ms 19652 KB Output is correct
5 Correct 368 ms 19952 KB Output is correct
6 Correct 374 ms 20076 KB Output is correct
7 Correct 397 ms 20160 KB Output is correct
8 Correct 377 ms 19952 KB Output is correct
9 Correct 376 ms 20196 KB Output is correct
10 Correct 369 ms 20132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 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 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 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 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 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 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 4 ms 332 KB Output is correct
22 Correct 3 ms 332 KB Output is correct
23 Correct 3 ms 332 KB Output is correct
24 Correct 3 ms 332 KB Output is correct
25 Correct 3 ms 332 KB Output is correct
26 Correct 3 ms 332 KB Output is correct
27 Correct 4 ms 436 KB Output is correct
28 Correct 4 ms 332 KB Output is correct
29 Correct 3 ms 332 KB Output is correct
30 Correct 3 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 390 ms 19616 KB Output is correct
2 Correct 439 ms 19924 KB Output is correct
3 Correct 400 ms 20208 KB Output is correct
4 Correct 419 ms 20072 KB Output is correct
5 Correct 402 ms 19968 KB Output is correct
6 Correct 382 ms 19656 KB Output is correct
7 Correct 408 ms 19572 KB Output is correct
8 Correct 396 ms 20036 KB Output is correct
9 Correct 383 ms 19680 KB Output is correct
10 Correct 384 ms 19912 KB Output is correct
11 Correct 341 ms 19780 KB Output is correct
12 Correct 358 ms 20304 KB Output is correct
13 Correct 341 ms 19732 KB Output is correct
14 Correct 348 ms 19888 KB Output is correct
15 Correct 346 ms 19652 KB Output is correct
16 Correct 350 ms 19712 KB Output is correct
17 Correct 360 ms 20228 KB Output is correct
18 Correct 358 ms 20232 KB Output is correct
19 Correct 358 ms 19496 KB Output is correct
20 Correct 358 ms 20100 KB Output is correct
21 Correct 392 ms 19916 KB Output is correct
22 Correct 390 ms 19756 KB Output is correct
23 Correct 374 ms 20036 KB Output is correct
24 Correct 387 ms 19652 KB Output is correct
25 Correct 368 ms 19952 KB Output is correct
26 Correct 374 ms 20076 KB Output is correct
27 Correct 397 ms 20160 KB Output is correct
28 Correct 377 ms 19952 KB Output is correct
29 Correct 376 ms 20196 KB Output is correct
30 Correct 369 ms 20132 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 4 ms 332 KB Output is correct
52 Correct 3 ms 332 KB Output is correct
53 Correct 3 ms 332 KB Output is correct
54 Correct 3 ms 332 KB Output is correct
55 Correct 3 ms 332 KB Output is correct
56 Correct 3 ms 332 KB Output is correct
57 Correct 4 ms 436 KB Output is correct
58 Correct 4 ms 332 KB Output is correct
59 Correct 3 ms 332 KB Output is correct
60 Correct 3 ms 332 KB Output is correct
61 Correct 404 ms 19736 KB Output is correct
62 Correct 399 ms 20164 KB Output is correct
63 Correct 376 ms 19780 KB Output is correct
64 Incorrect 401 ms 20040 KB Output isn't correct
65 Halted 0 ms 0 KB -