Submission #987519

# Submission time Handle Problem Language Result Execution time Memory
987519 2024-05-22T23:24:23 Z tfgs Feast (NOI19_feast) C++17
100 / 100
194 ms 15200 KB
#include <bits/stdc++.h>
using namespace std;
#define int int64_t

#ifdef LOCAL
#include "algo/debug.h"
#endif

#define f first
#define s second
template<class T> using V = vector<T>; 
using vi = V<int>;
using vb = V<bool>;
using vs = V<string>;

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x) 
#define pb push_back
#define lb lower_bound
#define ub upper_bound
template<class T> int lwb(V<T>& a, const T& b) { return lb(all(a),b)-begin(a); }
template<class T> int upb(V<T>& a, const T& b) { return ub(all(a),b)-begin(a); }
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a=b, true : false; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a=b, true : false; }

const int max_n = 3e5;
array<int, 2> dp[max_n][2];
int n, k;
vi a;
// returns {score, number of segments} with penalty lmb for making a new subarray
array<int, 2> calc(int lmb) {
    memset(dp, 0, sizeof dp);
    // V<V<array<int, 2>>> dp(n, V<array<int, 2>>(2));
    dp[0][1] = {a[0]-lmb, 1};
    for (int i = 1; i < n; i++) {
        dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
        dp[i][1] = max(array<int, 2>{ dp[i-1][0][0]+a[i]-lmb, dp[i-1][0][1]+1 },
                       array<int, 2>{ dp[i-1][1][0]+a[i],     dp[i-1][1][1] });
    }
    return max(dp[n-1][0], dp[n-1][1]);
}

void solve() {
    cin >> n >> k;
    a.resize(n); for (int i = 0; i < n; i++) cin >> a[i];

    // int lo = 0;
    // int hi = 1e18;
    // while (lo < hi) {
        // int mid = (lo + hi + 1) / 2;
        // calc(mid)[1] >= k ? lo = mid : hi = mid - 1;
    // }
    // cout << calc(lo)[0]+lo*k << '\n';
    int lo = -1, hi = 1e18;
    while (hi-lo > 1) {
        int m = (lo+hi)/2;
        auto res = calc(m);
        res[1] >= k ? lo = m : hi = m;
    }
    if (lo == -1) {
        cout << calc(0)[0] << '\n';
        return;
    }
    auto ans = calc(lo);
    cout << ans[0]+lo*k << '\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 158 ms 12124 KB Output is correct
2 Correct 175 ms 14940 KB Output is correct
3 Correct 165 ms 14940 KB Output is correct
4 Correct 163 ms 14928 KB Output is correct
5 Correct 174 ms 14940 KB Output is correct
6 Correct 159 ms 14940 KB Output is correct
7 Correct 160 ms 14672 KB Output is correct
8 Correct 189 ms 14920 KB Output is correct
9 Correct 161 ms 14672 KB Output is correct
10 Correct 194 ms 14808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 12120 KB Output is correct
2 Correct 97 ms 13148 KB Output is correct
3 Correct 99 ms 13136 KB Output is correct
4 Correct 142 ms 13128 KB Output is correct
5 Correct 103 ms 14676 KB Output is correct
6 Correct 93 ms 13144 KB Output is correct
7 Correct 135 ms 13144 KB Output is correct
8 Correct 105 ms 15196 KB Output is correct
9 Correct 117 ms 14676 KB Output is correct
10 Correct 127 ms 13144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 125 ms 12124 KB Output is correct
2 Correct 130 ms 15036 KB Output is correct
3 Correct 127 ms 14924 KB Output is correct
4 Correct 124 ms 14912 KB Output is correct
5 Correct 154 ms 14916 KB Output is correct
6 Correct 126 ms 15180 KB Output is correct
7 Correct 144 ms 15080 KB Output is correct
8 Correct 129 ms 14932 KB Output is correct
9 Correct 130 ms 15196 KB Output is correct
10 Correct 151 ms 15184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 9816 KB Output is correct
2 Correct 13 ms 9816 KB Output is correct
3 Correct 15 ms 9820 KB Output is correct
4 Correct 14 ms 9820 KB Output is correct
5 Correct 17 ms 9820 KB Output is correct
6 Correct 13 ms 10072 KB Output is correct
7 Correct 15 ms 9820 KB Output is correct
8 Correct 14 ms 9816 KB Output is correct
9 Correct 14 ms 9820 KB Output is correct
10 Correct 16 ms 9844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 9816 KB Output is correct
2 Correct 13 ms 9816 KB Output is correct
3 Correct 15 ms 9820 KB Output is correct
4 Correct 14 ms 9820 KB Output is correct
5 Correct 17 ms 9820 KB Output is correct
6 Correct 13 ms 10072 KB Output is correct
7 Correct 15 ms 9820 KB Output is correct
8 Correct 14 ms 9816 KB Output is correct
9 Correct 14 ms 9820 KB Output is correct
10 Correct 16 ms 9844 KB Output is correct
11 Correct 13 ms 9820 KB Output is correct
12 Correct 14 ms 9816 KB Output is correct
13 Correct 16 ms 9816 KB Output is correct
14 Correct 16 ms 9816 KB Output is correct
15 Correct 15 ms 9820 KB Output is correct
16 Correct 13 ms 9820 KB Output is correct
17 Correct 15 ms 9816 KB Output is correct
18 Correct 14 ms 9852 KB Output is correct
19 Correct 19 ms 9856 KB Output is correct
20 Correct 14 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 9816 KB Output is correct
2 Correct 13 ms 9816 KB Output is correct
3 Correct 15 ms 9820 KB Output is correct
4 Correct 14 ms 9820 KB Output is correct
5 Correct 17 ms 9820 KB Output is correct
6 Correct 13 ms 10072 KB Output is correct
7 Correct 15 ms 9820 KB Output is correct
8 Correct 14 ms 9816 KB Output is correct
9 Correct 14 ms 9820 KB Output is correct
10 Correct 16 ms 9844 KB Output is correct
11 Correct 13 ms 9820 KB Output is correct
12 Correct 14 ms 9816 KB Output is correct
13 Correct 16 ms 9816 KB Output is correct
14 Correct 16 ms 9816 KB Output is correct
15 Correct 15 ms 9820 KB Output is correct
16 Correct 13 ms 9820 KB Output is correct
17 Correct 15 ms 9816 KB Output is correct
18 Correct 14 ms 9852 KB Output is correct
19 Correct 19 ms 9856 KB Output is correct
20 Correct 14 ms 9820 KB Output is correct
21 Correct 15 ms 9820 KB Output is correct
22 Correct 14 ms 9868 KB Output is correct
23 Correct 15 ms 9820 KB Output is correct
24 Correct 16 ms 9820 KB Output is correct
25 Correct 15 ms 9820 KB Output is correct
26 Correct 17 ms 9868 KB Output is correct
27 Correct 17 ms 9820 KB Output is correct
28 Correct 21 ms 9820 KB Output is correct
29 Correct 18 ms 9820 KB Output is correct
30 Correct 17 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 158 ms 12124 KB Output is correct
2 Correct 175 ms 14940 KB Output is correct
3 Correct 165 ms 14940 KB Output is correct
4 Correct 163 ms 14928 KB Output is correct
5 Correct 174 ms 14940 KB Output is correct
6 Correct 159 ms 14940 KB Output is correct
7 Correct 160 ms 14672 KB Output is correct
8 Correct 189 ms 14920 KB Output is correct
9 Correct 161 ms 14672 KB Output is correct
10 Correct 194 ms 14808 KB Output is correct
11 Correct 95 ms 12120 KB Output is correct
12 Correct 97 ms 13148 KB Output is correct
13 Correct 99 ms 13136 KB Output is correct
14 Correct 142 ms 13128 KB Output is correct
15 Correct 103 ms 14676 KB Output is correct
16 Correct 93 ms 13144 KB Output is correct
17 Correct 135 ms 13144 KB Output is correct
18 Correct 105 ms 15196 KB Output is correct
19 Correct 117 ms 14676 KB Output is correct
20 Correct 127 ms 13144 KB Output is correct
21 Correct 125 ms 12124 KB Output is correct
22 Correct 130 ms 15036 KB Output is correct
23 Correct 127 ms 14924 KB Output is correct
24 Correct 124 ms 14912 KB Output is correct
25 Correct 154 ms 14916 KB Output is correct
26 Correct 126 ms 15180 KB Output is correct
27 Correct 144 ms 15080 KB Output is correct
28 Correct 129 ms 14932 KB Output is correct
29 Correct 130 ms 15196 KB Output is correct
30 Correct 151 ms 15184 KB Output is correct
31 Correct 14 ms 9816 KB Output is correct
32 Correct 13 ms 9816 KB Output is correct
33 Correct 15 ms 9820 KB Output is correct
34 Correct 14 ms 9820 KB Output is correct
35 Correct 17 ms 9820 KB Output is correct
36 Correct 13 ms 10072 KB Output is correct
37 Correct 15 ms 9820 KB Output is correct
38 Correct 14 ms 9816 KB Output is correct
39 Correct 14 ms 9820 KB Output is correct
40 Correct 16 ms 9844 KB Output is correct
41 Correct 13 ms 9820 KB Output is correct
42 Correct 14 ms 9816 KB Output is correct
43 Correct 16 ms 9816 KB Output is correct
44 Correct 16 ms 9816 KB Output is correct
45 Correct 15 ms 9820 KB Output is correct
46 Correct 13 ms 9820 KB Output is correct
47 Correct 15 ms 9816 KB Output is correct
48 Correct 14 ms 9852 KB Output is correct
49 Correct 19 ms 9856 KB Output is correct
50 Correct 14 ms 9820 KB Output is correct
51 Correct 15 ms 9820 KB Output is correct
52 Correct 14 ms 9868 KB Output is correct
53 Correct 15 ms 9820 KB Output is correct
54 Correct 16 ms 9820 KB Output is correct
55 Correct 15 ms 9820 KB Output is correct
56 Correct 17 ms 9868 KB Output is correct
57 Correct 17 ms 9820 KB Output is correct
58 Correct 21 ms 9820 KB Output is correct
59 Correct 18 ms 9820 KB Output is correct
60 Correct 17 ms 9820 KB Output is correct
61 Correct 158 ms 15036 KB Output is correct
62 Correct 160 ms 15188 KB Output is correct
63 Correct 147 ms 14928 KB Output is correct
64 Correct 160 ms 15200 KB Output is correct
65 Correct 169 ms 15184 KB Output is correct
66 Correct 193 ms 15180 KB Output is correct
67 Correct 159 ms 14928 KB Output is correct
68 Correct 132 ms 15184 KB Output is correct
69 Correct 124 ms 14940 KB Output is correct
70 Correct 125 ms 14844 KB Output is correct