Submission #59075

#TimeUsernameProblemLanguageResultExecution timeMemory
59075zubecSplit the sequence (APIO14_sequence)C++14
71 / 100
2044 ms90460 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; ll n, m, a[100100], pref[100100]; int pr[100100][210]; struct line{ ll k, b; int pos; //set<line>::iterator it; line(ld _k, ld _b, int _pos){ k = _k; b = _b; pos = _pos; } ld eval(ld x){ return k*x+b; } }; inline ld intersect(line l1, line l2){ return (l1.b-l2.b)/(l2.k-l1.k); } inline bool to_erase(line l1, line l2, line l3){ //ld x = intersect(l1, l3); //return l2.eval(x) <= l3.eval(x); return ((l3.b-l1.b)/(l1.k-l3.k) > (l3.b-l2.b)/(l2.k-l3.k)); } vector <line> vec, vecCur; void insrt(line ln){ if (!vec.empty() && vec.back().k == ln.k){ if (vec.back().b < ln.b) vec.pop_back(); else return; } while((int)vec.size() > 2 && to_erase(vec[(int)vec.size()-2], vec[(int)vec.size()-1], ln)) vec.pop_back(); vec.push_back(ln); } /*void rec(int n, int m){ if (m == 0) return; for (int i = n-1; i >= 1; i--){ if (dp[i][m-1] + (pref[n]-pref[i])*pref[i] == dp[n][m]){ rec(i, m-1); cout << i << ' '; return; } } }*/ void swp(){ vec.swap(vecCur); vec.clear(); } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++){ cin >> a[i]; pref[i] = pref[i-1] + a[i]; } for (int i = 1; i <= n; i++) insrt(line(pref[i], -(pref[i]*pref[i]), i)); swp(); ll ans = 0; for (int ii = 1; ii <= m; ii++){ int pos = 0; for (int i = ii+1; i <= n; i++){ while(pos+1 < (int)vecCur.size() && vecCur[pos+1].pos < i && vecCur[pos].eval(pref[i]) <= vecCur[pos+1].eval(pref[i])) ++pos; pr[i][ii] = vecCur[pos].pos; ans = vecCur[pos].eval(pref[i]); insrt(line(pref[i], ans-(pref[i]*pref[i]), i)); } swp(); } cout << ans << endl; vector <int> vecAns; int x = n, y = m; while(x != 0){ x = pr[x][y]; y--; vecAns.push_back(x); } vecAns.pop_back(); for (int i = vecAns.size()-1; i >= 0; i--) cout << vecAns[i] << ' '; //cout << dp[n][m] << endl; //rec(n, m); } /** 7 3 4 1 3 4 0 2 3 */
#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...