Submission #624743

# Submission time Handle Problem Language Result Execution time Memory
624743 2022-08-08T16:27:12 Z rainliofficial Feast (NOI19_feast) C++17
100 / 100
145 ms 12760 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
#define sz(x) (int)x.size()


/* 
Date: 2022/08/08 11:13
Problem Link: link
Topic(s):
Time Spent:
Solution Notes:
There is a simply N^2 dp:
dp[i][j] = max reward in [0, i] after using j segments
dp[i][j] = ps[i] + max(-ps[i'] + dp[i'][j-1]), where ps[i] is the prefix sums of the first i elements OR dp[i-1][j]

Using Alien's trick to optimize
Consider the "value" of each segment, which is the max reward we can get if we place this segment optimally. As we add more segments, the answer increases, 
but the answer increases by less as we have more segments. In other words, the value of a segment always decreases. We can think about the value of a segment
as a concave function. The kth segment will correpsond to some value x. Our goal is to find the sum of the values of segments before k (i.e. segments with value
greater or equal to x). 

We can do this by binary searching on x, but how do we know if we have used more/less than k segments? First, think what does it mean for each segment to have value
at least x. That means the sum over all segments we takes >= k*x. Let's subtract our x every time we take a new segment, now we want to make sure we only
take segments that result in a positive value. Now, we can utilize our dp formulation. Instead of keeping track of how many segments we used as part of the state,
subtracting by x will automatically "deter" us from taking too many segments (if x is bigger, that means we will select less elements, and vice versa).
dp[i] = max reward in [0, i] (offseting by x everytime we take a new segment)
dp[i] = ps[i] + max(-ps[i'] + dp[i']) - x OR dp[i-1]

Track for each dp[i], how many x's have we subtracted by (how many segments used). In the end, we will adjust our binary search on x with it.
In the end, we will have to add back the k*x we offsetted by. There might be multiple segments all with value == x. However, we don't have to worry about this,
as we offset by x every time we take a segment (meaning in the dp, segments == x will contribute 0), and we ONLY add back k*x regardless of what lo (in the binary search) is.  
*/

const int MAXN = 3e5+5, INF = 1e9;
int n, k;
ll arr[MAXN], ps[MAXN];

pair<ll, ll> check(ll x){
    vector<pair<ll, ll>> dp(n+1);
    pair<ll, ll> mx;
    for (int i=1; i<=n; i++){
        dp[i] = dp[i-1];
        if (ps[i] + mx.first - x > dp[i].first){
            dp[i] = {ps[i] + mx.first - x, mx.second + 1};
        }
        if (dp[i].first - ps[i] > mx.first){
            mx = {dp[i].first - ps[i], dp[i].second};
        }
    }
    return dp[n];
}
int main(){
    cin.tie(0); ios_base::sync_with_stdio(0);
    // freopen("file.in", "r", stdin);
    // freopen("file.out", "w", stdout);
    cin >> n >> k;
    for (int i=0; i<n; i++){
        cin >> arr[i];
    }
    for (int i=0; i<n; i++){
        ps[i+1] = ps[i] + arr[i];
    }
    // binary search on x
    ll low = 0, high = 1e15;
    while (low < high){
        ll mid = (low + high)/2;
        int seg = check(mid).second;
        if (seg > k){
            low = mid+1;
        }else{
            high = mid;
        }
    }
    pair<ll, ll> ans = check(high);
    cout << ans.first + k*high << "\n";
}

/**
 * Debugging checklist:
 * - Reset everything after each TC
 * - Integer overflow, index overflow
 * - Special cases?
 */
# Verdict Execution time Memory Grader output
1 Correct 81 ms 9540 KB Output is correct
2 Correct 73 ms 12436 KB Output is correct
3 Correct 74 ms 12528 KB Output is correct
4 Correct 71 ms 12492 KB Output is correct
5 Correct 76 ms 12436 KB Output is correct
6 Correct 74 ms 12208 KB Output is correct
7 Correct 84 ms 12176 KB Output is correct
8 Correct 73 ms 12404 KB Output is correct
9 Correct 71 ms 12288 KB Output is correct
10 Correct 78 ms 12376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 9460 KB Output is correct
2 Correct 84 ms 10796 KB Output is correct
3 Correct 68 ms 10556 KB Output is correct
4 Correct 81 ms 10756 KB Output is correct
5 Correct 67 ms 12228 KB Output is correct
6 Correct 72 ms 10572 KB Output is correct
7 Correct 67 ms 10776 KB Output is correct
8 Correct 75 ms 12516 KB Output is correct
9 Correct 73 ms 12156 KB Output is correct
10 Correct 77 ms 10780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 9592 KB Output is correct
2 Correct 99 ms 12448 KB Output is correct
3 Correct 114 ms 12508 KB Output is correct
4 Correct 106 ms 12420 KB Output is correct
5 Correct 98 ms 12580 KB Output is correct
6 Correct 102 ms 12664 KB Output is correct
7 Correct 103 ms 12708 KB Output is correct
8 Correct 98 ms 12584 KB Output is correct
9 Correct 115 ms 12748 KB Output is correct
10 Correct 101 ms 12668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 2 ms 368 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 9540 KB Output is correct
2 Correct 73 ms 12436 KB Output is correct
3 Correct 74 ms 12528 KB Output is correct
4 Correct 71 ms 12492 KB Output is correct
5 Correct 76 ms 12436 KB Output is correct
6 Correct 74 ms 12208 KB Output is correct
7 Correct 84 ms 12176 KB Output is correct
8 Correct 73 ms 12404 KB Output is correct
9 Correct 71 ms 12288 KB Output is correct
10 Correct 78 ms 12376 KB Output is correct
11 Correct 68 ms 9460 KB Output is correct
12 Correct 84 ms 10796 KB Output is correct
13 Correct 68 ms 10556 KB Output is correct
14 Correct 81 ms 10756 KB Output is correct
15 Correct 67 ms 12228 KB Output is correct
16 Correct 72 ms 10572 KB Output is correct
17 Correct 67 ms 10776 KB Output is correct
18 Correct 75 ms 12516 KB Output is correct
19 Correct 73 ms 12156 KB Output is correct
20 Correct 77 ms 10780 KB Output is correct
21 Correct 106 ms 9592 KB Output is correct
22 Correct 99 ms 12448 KB Output is correct
23 Correct 114 ms 12508 KB Output is correct
24 Correct 106 ms 12420 KB Output is correct
25 Correct 98 ms 12580 KB Output is correct
26 Correct 102 ms 12664 KB Output is correct
27 Correct 103 ms 12708 KB Output is correct
28 Correct 98 ms 12584 KB Output is correct
29 Correct 115 ms 12748 KB Output is correct
30 Correct 101 ms 12668 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 2 ms 340 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 344 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 2 ms 368 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 135 ms 12448 KB Output is correct
62 Correct 135 ms 12760 KB Output is correct
63 Correct 123 ms 12416 KB Output is correct
64 Correct 134 ms 12660 KB Output is correct
65 Correct 138 ms 12652 KB Output is correct
66 Correct 145 ms 12620 KB Output is correct
67 Correct 136 ms 12492 KB Output is correct
68 Correct 118 ms 12676 KB Output is correct
69 Correct 121 ms 12436 KB Output is correct
70 Correct 118 ms 12368 KB Output is correct