Submission #624740

# Submission time Handle Problem Language Result Execution time Memory
624740 2022-08-08T16:25:07 Z rainliofficial Feast (NOI19_feast) C++17
0 / 100
1000 ms 7268 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, 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 + 1)/2;
        int seg = check(mid).second;
        if (seg > k){
            low = mid;
        }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 Execution timed out 1084 ms 7116 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1086 ms 7152 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1087 ms 7268 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1094 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1094 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1094 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1084 ms 7116 KB Time limit exceeded
2 Halted 0 ms 0 KB -