답안 #861149

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
861149 2023-10-15T13:32:43 Z sleepntsheep Feast (NOI19_feast) C++17
51 / 100
29 ms 4704 KB
#include <cstdio>
#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];

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];

    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)
    {
        long long dp[2][3005]{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-1]) + p[m] - p[k-1], k});
            dp[i&1][m] = best.first;
            int opt = best.second;
            compute(i, l, m-1, optl, opt);
            compute(i, m+1, r, opt, optr);
        };

        for (int i = 1; i <= k; ++i)
        {
            compute(i, 1, n, 1, n);
            //for (int j = 1; j <= n; ++j) dp[!(i&1)][j] = 0;
        }
        return cout << *max_element(dp[k&1], dp[k&1]+n+1), 0;
    }

    return 0;
}


# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 4444 KB Output is correct
2 Correct 27 ms 4536 KB Output is correct
3 Correct 23 ms 4700 KB Output is correct
4 Correct 23 ms 4444 KB Output is correct
5 Correct 23 ms 4416 KB Output is correct
6 Correct 22 ms 4564 KB Output is correct
7 Correct 27 ms 4444 KB Output is correct
8 Correct 24 ms 4632 KB Output is correct
9 Correct 22 ms 4432 KB Output is correct
10 Correct 23 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 4444 KB Output is correct
2 Correct 15 ms 4700 KB Output is correct
3 Correct 15 ms 4444 KB Output is correct
4 Correct 15 ms 4444 KB Output is correct
5 Correct 22 ms 4432 KB Output is correct
6 Correct 17 ms 4444 KB Output is correct
7 Correct 16 ms 4704 KB Output is correct
8 Correct 23 ms 4664 KB Output is correct
9 Correct 22 ms 4600 KB Output is correct
10 Correct 15 ms 4556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 4432 KB Output is correct
2 Correct 26 ms 4380 KB Output is correct
3 Correct 29 ms 4440 KB Output is correct
4 Correct 25 ms 4488 KB Output is correct
5 Correct 29 ms 4692 KB Output is correct
6 Correct 25 ms 4552 KB Output is correct
7 Correct 25 ms 4600 KB Output is correct
8 Correct 24 ms 4592 KB Output is correct
9 Correct 26 ms 4700 KB Output is correct
10 Correct 26 ms 4440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1116 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 1 ms 1112 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 1 ms 1116 KB Output is correct
9 Correct 1 ms 1112 KB Output is correct
10 Correct 1 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1116 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 1 ms 1112 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 1 ms 1116 KB Output is correct
9 Correct 1 ms 1112 KB Output is correct
10 Correct 1 ms 1116 KB Output is correct
11 Correct 3 ms 1112 KB Output is correct
12 Correct 3 ms 1116 KB Output is correct
13 Correct 1 ms 1116 KB Output is correct
14 Correct 3 ms 1368 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 2 ms 1116 KB Output is correct
17 Correct 3 ms 1116 KB Output is correct
18 Correct 1 ms 1116 KB Output is correct
19 Correct 1 ms 1116 KB Output is correct
20 Correct 2 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1116 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 1 ms 1112 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 1 ms 1116 KB Output is correct
9 Correct 1 ms 1112 KB Output is correct
10 Correct 1 ms 1116 KB Output is correct
11 Correct 3 ms 1112 KB Output is correct
12 Correct 3 ms 1116 KB Output is correct
13 Correct 1 ms 1116 KB Output is correct
14 Correct 3 ms 1368 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 2 ms 1116 KB Output is correct
17 Correct 3 ms 1116 KB Output is correct
18 Correct 1 ms 1116 KB Output is correct
19 Correct 1 ms 1116 KB Output is correct
20 Correct 2 ms 1116 KB Output is correct
21 Incorrect 11 ms 1116 KB Output isn't correct
22 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 4444 KB Output is correct
2 Correct 27 ms 4536 KB Output is correct
3 Correct 23 ms 4700 KB Output is correct
4 Correct 23 ms 4444 KB Output is correct
5 Correct 23 ms 4416 KB Output is correct
6 Correct 22 ms 4564 KB Output is correct
7 Correct 27 ms 4444 KB Output is correct
8 Correct 24 ms 4632 KB Output is correct
9 Correct 22 ms 4432 KB Output is correct
10 Correct 23 ms 4444 KB Output is correct
11 Correct 15 ms 4444 KB Output is correct
12 Correct 15 ms 4700 KB Output is correct
13 Correct 15 ms 4444 KB Output is correct
14 Correct 15 ms 4444 KB Output is correct
15 Correct 22 ms 4432 KB Output is correct
16 Correct 17 ms 4444 KB Output is correct
17 Correct 16 ms 4704 KB Output is correct
18 Correct 23 ms 4664 KB Output is correct
19 Correct 22 ms 4600 KB Output is correct
20 Correct 15 ms 4556 KB Output is correct
21 Correct 25 ms 4432 KB Output is correct
22 Correct 26 ms 4380 KB Output is correct
23 Correct 29 ms 4440 KB Output is correct
24 Correct 25 ms 4488 KB Output is correct
25 Correct 29 ms 4692 KB Output is correct
26 Correct 25 ms 4552 KB Output is correct
27 Correct 25 ms 4600 KB Output is correct
28 Correct 24 ms 4592 KB Output is correct
29 Correct 26 ms 4700 KB Output is correct
30 Correct 26 ms 4440 KB Output is correct
31 Correct 1 ms 1116 KB Output is correct
32 Correct 1 ms 1116 KB Output is correct
33 Correct 1 ms 1116 KB Output is correct
34 Correct 1 ms 1116 KB Output is correct
35 Correct 1 ms 1116 KB Output is correct
36 Correct 1 ms 1112 KB Output is correct
37 Correct 1 ms 1116 KB Output is correct
38 Correct 1 ms 1116 KB Output is correct
39 Correct 1 ms 1112 KB Output is correct
40 Correct 1 ms 1116 KB Output is correct
41 Correct 3 ms 1112 KB Output is correct
42 Correct 3 ms 1116 KB Output is correct
43 Correct 1 ms 1116 KB Output is correct
44 Correct 3 ms 1368 KB Output is correct
45 Correct 2 ms 1116 KB Output is correct
46 Correct 2 ms 1116 KB Output is correct
47 Correct 3 ms 1116 KB Output is correct
48 Correct 1 ms 1116 KB Output is correct
49 Correct 1 ms 1116 KB Output is correct
50 Correct 2 ms 1116 KB Output is correct
51 Incorrect 11 ms 1116 KB Output isn't correct
52 Halted 0 ms 0 KB -