Submission #394492

#TimeUsernameProblemLanguageResultExecution timeMemory
394492rocks03Feast (NOI19_feast)C++14
100 / 100
267 ms9432 KiB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << " "
#define nl cout << "\n"
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll INF = 1e16;
const int MAXN = 3e5+100;
int N, K, a[MAXN];
ll dp[MAXN];

pll get(ll penalty){
    vector<ll> dp(N + 1, 0), cnt(N + 1, 0);
    ll s = 0, curcnt = 0, mx = 0, mxcnt = 0;
    rep(i, 1, N + 1){
        dp[i] = dp[i - 1], cnt[i] = cnt[i - 1];
        s += a[i];
        if(s < 0){
            s = 0, curcnt = 0;
        }
        if(mx < s){
            mx = s, mxcnt = curcnt;
        }
        if(mx - penalty > dp[i]){
            dp[i] = mx - penalty;
            cnt[i] = mxcnt + 1;
        }
        if(dp[i] > s){
            s = dp[i], curcnt = cnt[i];
        }
        if(s > mx){
            mx = s, mxcnt = cnt[i];
        }
    }
    return {dp[N], cnt[N]};
}

void solve(){
    cin >> N >> K;
    int cnt = 0;
    rep(i, 1, N + 1){
        cin >> a[i];
    }
    ll l = -1, r = INF;
    while(r - l > 1){
        ll m = (l + r) / 2;
        pll ans = get(m);
        if(ans.ss <= K){
            r = m;
        } else{
            l = m;
        }
    }
    pll ans = get(r);
    ll solution = ans.ff + ans.ss * r;
    cout << solution;
}
 
int main(){
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    solve();
    return 0;
}

Compilation message (stderr)

feast.cpp: In function 'void solve()':
feast.cpp:50:9: warning: unused variable 'cnt' [-Wunused-variable]
   50 |     int cnt = 0;
      |         ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...