Submission #791750

#TimeUsernameProblemLanguageResultExecution timeMemory
791750minhnhatnoeFeast (NOI19_feast)C++14
100 / 100
93 ms7392 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; vector<ll> a; pair<ll, int> choose(pair<ll, int> a, pair<ll, int> b){ if (a.first == b.first) return min(a, b); return max(a, b); } pair<ll, int> calc(ll bm){ int n = a.size(); pair<ll, int> pfmax(0, 0); pair<ll, int> prevdp(0, 0); for (int i=0; i<n; i++){ // dp[i] = max(dp[i-1], dp[k-1] + pf[i] - pf[k-1] - bm) // (dp[k-1] - pf[k-1]) + pf[i] - bm pair<ll, int> cdp = prevdp; cdp = choose(cdp, pair<ll, int> (pfmax.first + a[i] - bm, pfmax.second + 1)); pfmax = choose(pfmax, pair<ll, int> (cdp.first - a[i], cdp.second)); prevdp = cdp; } return prevdp; } signed main(){ cin.tie(0)->sync_with_stdio(0); int n, k; cin >> n >> k; a.resize(n); for (int i=0; i<n; i++) cin >> a[i]; partial_sum(a.begin(), a.end(), a.begin()); ll bl=0, br=1e18; vector<pair<ll, int>> dp(n); while (bl < br){ ll bm = (bl + br) >> 1; if (calc(bm).second > k) bl = bm+1; else br = bm; } pair<ll, int> s = calc(bl); cout << s.first + k * bl << "\n"; }
#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...