Submission #1066449

#TimeUsernameProblemLanguageResultExecution timeMemory
1066449beaconmcSplit the sequence (APIO14_sequence)C++14
0 / 100
35 ms39904 KiB
#include <vector> #include <bits/stdc++.h> typedef long long ll; #define FOR(i,x,y) for(ll i=x; i<y; i++) #define FORNEG(i,x,y) for(ll i=x; i>y; i--) using namespace std; ll dp[10001][201]; ll prevs[1001][201]; int main(){ ll n,k; cin >> n >> k; vector<ll> arr(n); vector<ll> pref(n); FOR(i,0,n) cin >> arr[i]; pref[0] = arr[0]; FOR(i,1,n) pref[i] = pref[i-1]+arr[i]; FOR(i,0,10001)FOR(j,0,201) dp[i][j] = -1000000000; deque<vector<ll>> sus; dp[0][0] = 0; FOR(j,1,k+1){ sus.clear(); sus.push_back({-pref[0]+arr[0], dp[0][j-1], 0}); FOR(i,1,n+1){ ll temp = (pref[n-1] - pref[i-1]); while (sus.size() >= 2 && (temp * sus[1][0] + sus[1][1]) > (temp * sus[0][0] + sus[0][1])){ sus.pop_front(); } ll ans = temp * sus[0][0] + sus[0][1] + (pref[n-1] - pref[i-1]) * pref[i-1]; if (dp[i][j] < ans){ dp[i][j] = ans; prevs[i][j] = sus[0][2]; } while (sus.size()>=2 && (temp * sus[sus.size()-2][0] + sus[sus.size()-2][1]) < temp * (-pref[i]+arr[i]) + dp[i][j-1]) { sus.pop_back(); } sus.push_back({(-pref[i]+arr[i]), dp[i][j-1], i}); } } ll maxi = -1; ll pos = -1; FORNEG(i,n-1,-1) if (dp[i][k] > maxi) maxi = dp[i][k], pos = i; cout << dp[pos][k] << endl; FOR(i,0,k){ ll sus = prevs[pos][k-i]; cout << pos << " "; pos = sus; } }
#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...