Submission #624739

#TimeUsernameProblemLanguageResultExecution timeMemory
624739rainliofficialFeast (NOI19_feast)Java
Compilation error
0 ms0 KiB
#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? */

Compilation message (stderr)

feast.java:1: error: illegal character: '#'
#include <bits/stdc++.h>
^
feast.java:1: error: class, interface, or enum expected
#include <bits/stdc++.h>
         ^
feast.java:3: error: class, interface, or enum expected
typedef long long ll;
^
feast.java:4: error: class, interface, or enum expected
typedef pair<int, int> pii;
^
feast.java:5: error: class, interface, or enum expected
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
^
feast.java:5: error: '{' expected
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
                ^
feast.java:5: error: illegal start of type
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
                                                 ^
feast.java:5: error: > expected
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
                                                              ^
feast.java:5: error: <identifier> expected
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
                                                                 ^
feast.java:5: error: <identifier> expected
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
                                                                      ^
feast.java:6: error: class, interface, or enum expected
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
^
feast.java:6: error: '{' expected
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
                ^
feast.java:6: error: illegal start of type
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
                                                 ^
feast.java:6: error: > expected
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
                                                              ^
feast.java:6: error: <identifier> expected
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
                                                                 ^
feast.java:6: error: <identifier> expected
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
                                                                      ^
feast.java:7: error: illegal character: '#'
#define sz(x) (int)x.size()
^
feast.java:7: error: class, interface, or enum expected
#define sz(x) (int)x.size()
        ^
feast.java:39: error: class, interface, or enum expected
int n, k, arr[MAXN], ps[MAXN];
^
feast.java:41: error: class, interface, or enum expected
pair<ll, ll> check(ll x){
^
feast.java:43: error: class, interface, or enum expected
    pair<ll, ll> mx;
    ^
feast.java:44: error: class, interface, or enum expected
    for (int i=1; i<=n; i++){
    ^
feast.java:44: error: class, interface, or enum expected
    for (int i=1; i<=n; i++){
                  ^
feast.java:44: error: class, interface, or enum expected
    for (int i=1; i<=n; i++){
                        ^
feast.java:46: error: class, interface, or enum expected
        if (ps[i] + mx.first - x > dp[i].first){
        ^
feast.java:48: error: class, interface, or enum expected
        }
        ^
feast.java:51: error: class, interface, or enum expected
        }
        ^
feast.java:54: error: class, interface, or enum expected
}
^
feast.java:56: error: class, interface, or enum expected
    cin.tie(0); ios_base::sync_with_stdio(0);
                ^
feast.java:59: error: class, interface, or enum expected
    cin >> n >> k;
    ^
feast.java:60: error: class, interface, or enum expected
    for (int i=0; i<n; i++){
    ^
feast.java:60: error: class, interface, or enum expected
    for (int i=0; i<n; i++){
                  ^
feast.java:60: error: class, interface, or enum expected
    for (int i=0; i<n; i++){
                       ^
feast.java:62: error: class, interface, or enum expected
    }
    ^
feast.java:63: error: class, interface, or enum expected
    for (int i=0; i<n; i++){
                  ^
feast.java:63: error: class, interface, or enum expected
    for (int i=0; i<n; i++){
                       ^
feast.java:65: error: class, interface, or enum expected
    }
    ^
feast.java:68: error: class, interface, or enum expected
    while (low < high){
    ^
feast.java:70: error: class, interface, or enum expected
        int seg = check(mid).second;
        ^
feast.java:71: error: class, interface, or enum expected
        if (seg > k){
        ^
feast.java:73: error: class, interface, or enum expected
        }else{
        ^
feast.java:75: error: class, interface, or enum expected
        }
        ^
feast.java:78: error: class, interface, or enum expected
    cout << ans.first + k*high << "\n";
    ^
feast.java:79: error: class, interface, or enum expected
}
^
44 errors