Submission #640310

#TimeUsernameProblemLanguageResultExecution timeMemory
640310BackNoobSplit the sequence (APIO14_sequence)C++14
71 / 100
2069 ms36004 KiB
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define endl '\n' #define MASK(i) (1LL << (i)) #define ull unsigned long long #define ld long double #define pb push_back #define all(x) (x).begin() , (x).end() #define BIT(x , i) ((x >> (i)) & 1) #define TASK "task" #define sz(s) (int) (s).size() using namespace std; const int mxN = 1e5 + 227; const int inf = 1e9 + 277; const int mod = 1e9 + 7; const ll infll = 1e18 + 7; const int base = 4000; const int LOG = 50; const int block = 700; template <typename T1, typename T2> bool minimize(T1 &a, T2 b) { if (a > b) {a = b; return true;} return false; } template <typename T1, typename T2> bool maximize(T1 &a, T2 b) { if (a < b) {a = b; return true;} return false; } inline namespace _LineContainer { bool _Line_Comp_State; struct Line { // k is slope, m is intercept, p is intersection point mutable ll k, m, p, idx; bool operator<(const Line& o) const { return _Line_Comp_State ? p < o.p : k < o.k; } }; struct LineContainer : multiset<Line> { long long div(long long a, long long b) { return a / b - ((a ^ b) < 0 && a % b); } bool isect(iterator x, iterator y) { if (y == end()) { x->p = LLONG_MAX; return false; } if (x->k == y->k) x->p = x->m > y->m ? LLONG_MAX : -LLONG_MAX; else x->p = div(y->m - x->m, x->k - y->k); return x->p >= y->p; } void add(long long k, long long m, int idx) { auto z = insert({k, m, 0, idx}), y = z++, x = y; while (isect(y, z)) z = erase(z); if (x != begin() && isect(--x, y)) isect(x, y = erase(y)); while ((y = x) != begin() && (--x)->p >= y->p) isect(x, erase(y)); } pair<ll, int> query(long long x) { assert(!empty()); _Line_Comp_State = 1; auto l = *lower_bound({0,0,x,0}); _Line_Comp_State = 0; return make_pair(l.k * x + l.m, l.idx); } }; } int n; int k; int a[mxN]; int pre[mxN]; int trace[207][mxN]; ll dp[2][mxN]; LineContainer lc; vector<int> ans; void solve() { cin >> n >> k; for(int i = 1; i <= n; i++) cin >> a[i]; for(int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i]; memset(dp, -0x3f, sizeof(dp)); ll oo = dp[0][0]; for(int i = 1; i <= n; i++) dp[0][i] = 0; for(int i = 1; i <= k; i++) { lc.clear(); for(int j = 0; j <= n; j++) dp[i & 1][j] = oo; for(int j = 1; j <= n; j++) { lc.add(pre[j - 1], dp[(i - 1) & 1][j - 1] - 1LL * pre[j - 1] * pre[j - 1], j - 1); pair<ll, int> tmp = lc.query(pre[j]); if(maximize(dp[i & 1][j], tmp.fi) == true) { trace[i][j] = tmp.se; } } } cout << dp[k & 1][n] << endl; int x = k; int y = n; while(x > 0) { int tmp = trace[x][y]; if(y != n) ans.pb(y); --x; y = tmp; } ans.pb(y); reverse(all(ans)); for(auto it : ans) cout << it << ' '; } // dp[i][k] = dp[j][k - 1] + (pre[i] - pre[j]) * pre[j] // dp[i][k] = dp[j][k - 1] - pre[j] ^ 2 + pre[i] * pre[j] int main() { ios_base::sync_with_stdio(0); cin.tie(0); //freopen("task.inp", "r", stdin); //freopen("task.out", "w", stdout); int tc = 1; //cin >> tc; while(tc--) { solve(); } return 0; } //3 5 7 //4 1 3 4 0 2 3 //4 1 3 | 4 0 2 3 //4 1 3 | 4 0 | 2 3 //4 1 3 | 4 0 | 2 3 // // //3 * 14 = 42 //5 * 12 = 60 // 8 * 4 + 12 * 5
#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...