Submission #861204

# Submission time Handle Problem Language Result Execution time Memory
861204 2023-10-15T16:16:29 Z sleepntsheep Feast (NOI19_feast) C++17
100 / 100
490 ms 25796 KB
#include <cstdio>
#include <cmath>
#include <climits>
#include <functional>
#include <numeric>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using ll=long long;

#define N 300005
#define ALL(x) x.begin(), x.end()

int n, k, a[N];
long long p[N];
pair<long double, int> dp[N][2];
const long double EPS = 1e-3;

int check(long double lambda)
{
    dp[0][1] = make_pair(-1e18, 0);
    for (int i = 1; i <= n; ++i)
    {
        dp[i][0] = max({dp[i-1][0], dp[i-1][1]});
        dp[i][1] = max(make_pair(dp[i-1][0].first + a[i] - lambda, dp[i-1][0].second + 1),
                make_pair(dp[i-1][1].first + a[i], dp[i-1][1].second));
    }
    return max(dp[n][0], dp[n][1]).second >= k;
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) cin >> a[i], p[i] = p[i-1] + a[i];

    long double l = 0, r = 1e18;
    while (l + EPS < r)
    {
        long double y = (l+r)/2;
        if (check(y)) l = y;
        else r = y;
    }

    check(l);
    cout << (long long)round(k * l + max(dp[n][0], dp[n][1]).first);

    return 0;

    if (count_if(a+1, a+1+n,[](int x){ return x<0; }) == 0) return cout << accumulate(a+1,a+1+n, 0ll), 0;

    if (count_if(a+1, a+1+n,[](int x){ return x<0; }) == 1)
    {
        int x=0;
        for (int i = 1; i <= n; ++i) if (a[i] < 0) x = i;
        if (k == 1) return cout << max({p[x-1], p[n] - p[x], p[n]}), 0;
        else return cout << p[n] - a[x], 0;
    }

    if (k == 1)
    {
        long long ans = 0, mn = 0;
        for (int i = 1; i <= n; ++i) ans = max(ans, p[i] - mn), mn = min(mn, p[i]);
        return cout << ans, 0;
    }

    if (n <= 300)
    {
        long long dp[305][305]{0};
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= k; ++j)
            {
                dp[i][j] = max({dp[i-1][j], dp[i][j], dp[i][j-1]});
                for (int l = 0; l < i; ++l) dp[i][j] = max(dp[i][j], dp[l][j-1] + p[i] - p[l]);
            }
        return cout << dp[n][k], 0;
    }

    if (n <= 3000)
    {
        vector<vector<vector<long long>>> dp(2, vector<vector<long long>>(3005, vector<long long>(2)));
        long long ans = 0;
        for (int i = 1; i <= n; ++i)
        {
            for (int j = 1; j <= k; ++j)
            {
                dp[i&1][j][0] = max(dp[!(i&1)][j][0], dp[!(i&1)][j][1]);
                dp[i&1][j][1] = max({a[i] + dp[!(i&1)][j-1][0], a[i] + dp[!(i&1)][j][1]});
                ans = max({ans, dp[i&1][j][0], dp[i&1][j][1]});
            }
        }
        return cout << ans, 0;
    }

    if (1)
    {

    }

#if 0
    {
        function<void(int, int, int, int, int)> compute = [&](int i, int l, int r, int optl, int optr)
        {
            if (l > r) return;
            int m = (l+r)/2;
            pair<long long, int> best = {LLONG_MIN, -1};
            for (int k = optl; k <= min(m, optr); ++k)
                best = max(best, {(dp[!(i&1)][k]) + p[m] - p[k], k});
            dp[i&1][m] = best.first;
            compute(i, l, m-1, optl, best.second), compute(i, m+1, r, best.second, optr);
        };

        long long ans = 0;
        for (int i = 1; i <= k; ++i)
        {
            compute(i, 1, n, 0, n-1);
            for (int i = 1; i <= n; ++i) dp[i&1][i] = max(dp[i&1][i], dp[i&1][i-1]);
            ans = max(ans, *max_element(dp[i&1], dp[i&1]+n+1));
        }
        return cout << ans,0;
    }
#endif

    return 0;
}


# Verdict Execution time Memory Grader output
1 Correct 374 ms 22356 KB Output is correct
2 Correct 377 ms 22608 KB Output is correct
3 Correct 381 ms 22612 KB Output is correct
4 Correct 381 ms 22608 KB Output is correct
5 Correct 377 ms 22544 KB Output is correct
6 Correct 378 ms 22512 KB Output is correct
7 Correct 371 ms 22356 KB Output is correct
8 Correct 380 ms 22580 KB Output is correct
9 Correct 373 ms 22608 KB Output is correct
10 Correct 376 ms 22560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 373 ms 22364 KB Output is correct
2 Correct 383 ms 22704 KB Output is correct
3 Correct 371 ms 22528 KB Output is correct
4 Correct 374 ms 22868 KB Output is correct
5 Correct 379 ms 22520 KB Output is correct
6 Correct 375 ms 22648 KB Output is correct
7 Correct 378 ms 22616 KB Output is correct
8 Correct 388 ms 22604 KB Output is correct
9 Correct 378 ms 22512 KB Output is correct
10 Correct 374 ms 22592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 398 ms 22584 KB Output is correct
2 Correct 402 ms 22340 KB Output is correct
3 Correct 396 ms 22572 KB Output is correct
4 Correct 390 ms 22612 KB Output is correct
5 Correct 395 ms 22568 KB Output is correct
6 Correct 396 ms 22352 KB Output is correct
7 Correct 398 ms 22864 KB Output is correct
8 Correct 398 ms 22360 KB Output is correct
9 Correct 400 ms 22824 KB Output is correct
10 Correct 402 ms 22656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 3 ms 604 KB Output is correct
22 Correct 3 ms 616 KB Output is correct
23 Correct 3 ms 604 KB Output is correct
24 Correct 3 ms 604 KB Output is correct
25 Correct 3 ms 604 KB Output is correct
26 Correct 3 ms 604 KB Output is correct
27 Correct 3 ms 604 KB Output is correct
28 Correct 3 ms 624 KB Output is correct
29 Correct 4 ms 604 KB Output is correct
30 Correct 3 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 374 ms 22356 KB Output is correct
2 Correct 377 ms 22608 KB Output is correct
3 Correct 381 ms 22612 KB Output is correct
4 Correct 381 ms 22608 KB Output is correct
5 Correct 377 ms 22544 KB Output is correct
6 Correct 378 ms 22512 KB Output is correct
7 Correct 371 ms 22356 KB Output is correct
8 Correct 380 ms 22580 KB Output is correct
9 Correct 373 ms 22608 KB Output is correct
10 Correct 376 ms 22560 KB Output is correct
11 Correct 373 ms 22364 KB Output is correct
12 Correct 383 ms 22704 KB Output is correct
13 Correct 371 ms 22528 KB Output is correct
14 Correct 374 ms 22868 KB Output is correct
15 Correct 379 ms 22520 KB Output is correct
16 Correct 375 ms 22648 KB Output is correct
17 Correct 378 ms 22616 KB Output is correct
18 Correct 388 ms 22604 KB Output is correct
19 Correct 378 ms 22512 KB Output is correct
20 Correct 374 ms 22592 KB Output is correct
21 Correct 398 ms 22584 KB Output is correct
22 Correct 402 ms 22340 KB Output is correct
23 Correct 396 ms 22572 KB Output is correct
24 Correct 390 ms 22612 KB Output is correct
25 Correct 395 ms 22568 KB Output is correct
26 Correct 396 ms 22352 KB Output is correct
27 Correct 398 ms 22864 KB Output is correct
28 Correct 398 ms 22360 KB Output is correct
29 Correct 400 ms 22824 KB Output is correct
30 Correct 402 ms 22656 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 3 ms 604 KB Output is correct
52 Correct 3 ms 616 KB Output is correct
53 Correct 3 ms 604 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
55 Correct 3 ms 604 KB Output is correct
56 Correct 3 ms 604 KB Output is correct
57 Correct 3 ms 604 KB Output is correct
58 Correct 3 ms 624 KB Output is correct
59 Correct 4 ms 604 KB Output is correct
60 Correct 3 ms 604 KB Output is correct
61 Correct 485 ms 22528 KB Output is correct
62 Correct 490 ms 25796 KB Output is correct
63 Correct 413 ms 25428 KB Output is correct
64 Correct 487 ms 25632 KB Output is correct
65 Correct 474 ms 25612 KB Output is correct
66 Correct 473 ms 25496 KB Output is correct
67 Correct 467 ms 25680 KB Output is correct
68 Correct 403 ms 25632 KB Output is correct
69 Correct 393 ms 25684 KB Output is correct
70 Correct 398 ms 25468 KB Output is correct