Submission #768160

#TimeUsernameProblemLanguageResultExecution timeMemory
768160hafoSplit the sequence (APIO14_sequence)C++14
0 / 100
15 ms7680 KiB
#include <bits/stdc++.h> #define ll long long #define ull unsigned ll #define pb push_back #define pa pair<int, int> #define pall pair<ll, int> #define fi first #define se second #define TASK "test" #define Size(x) (int) x.size() #define all(x) x.begin(), x.end() using namespace std; template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;} template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;} const int MOD = 1e9 + 7; const int LOG = 20; const int maxn = 1e5 + 7; const ll oo = 1e18 + 69; int n, k, a[maxn], trace[207][maxn]; ll f[maxn], dp[207][maxn]; struct cht{ struct line { ll m, b; int id; }; vector<line> lines; int pointer; void init() { lines.clear(); pointer = 0; } double inter(line &x, line &y) { return (double) (y.b - x.b) / (double) (x.m - y.m); } bool bad(int i, int j, int k) { return inter(lines[i], lines[k]) < inter(lines[i], lines[j]); } ll calc(line &a, ll &x) { return a.m * x + a.b; } void add(ll m, ll b, int id) { lines.pb({m, b, id}); while(Size(lines) >= 3 && bad(Size(lines) - 3, Size(lines) - 2, Size(lines) - 1)) lines.erase(lines.end() - 2); } pair<ll, int> query(ll x) { if(pointer >= Size(lines)) pointer = Size(lines) - 1; while(pointer < Size(lines) - 1 && calc(lines[pointer], x) < calc(lines[pointer + 1], x)) pointer++; return {calc(lines[pointer], x), lines[pointer].id}; } } cht; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); //freopen(TASK".inp", "r", stdin); //freopen(TASK".out", "w", stdout); cin>>n>>k; for(int i = 1; i <= n; i++) { cin>>a[i]; f[i] = f[i - 1] + a[i]; } for(int c = 1; c <= k; c++) { cht.init(); cht.add(0, 0, 0); for(int i = 1; i <= n; i++) { // for(int j = 1; j <= i; j++) { // if(maxi(dp[c][i], f[i] * f[n] - f[i] * f[i] + f[j - 1] * f[i] - f[j - 1] * f[n] + dp[c - 1][j - 1])) trace[c][i] = j; // } auto tmp = cht.query(f[i]); dp[c][i] = f[i] * f[n] - f[i] * f[i] + tmp.fi; trace[c][i] = tmp.se + 1; cht.add(f[i], -f[i] * f[n] + dp[c - 1][i], i); } } ll ans = 0; int pos; vector<int> res; for(int i = 1; i <= n; i++) { if(maxi(ans, dp[k][i])) pos = i; } for(int i = k; i >= 1; i--) { res.pb(pos); pos = trace[i][pos - 1]; } reverse(all(res)); cout<<ans<<"\n"; for(auto i:res) cout<<i<<" "; return 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...