Submission #140865

#TimeUsernameProblemLanguageResultExecution timeMemory
140865silxikysSplit the sequence (APIO14_sequence)C++14
50 / 100
2048 ms49972 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; typedef long double ld; const int maxn = 1e5+5, maxk = 205; int N, K, a[maxn]; ll pre[maxn]; ll dp[maxn][maxk]; int previous[maxn][maxk]; struct FN { int id; ll m, b; //in this case, function is a line FN(ll _m, ll _b, int _id) : m(_m), b(_b), id(_id) {} ll eval(ll x) const { return m*x + b; } ld intersect(const FN& r) const { //returns x coordinate of intersection //assert(m != r.m); return (ld)(r.b-b)/(ld)(m-r.m); } friend bool comp(const FN& l1, const FN& l2, const FN& l) { //order in deque: l1, l2, l //true --> l2 is unnecessary ld x1 = l1.intersect(l); ld x2 = l1.intersect(l2); return x1 <= x2; } }; struct Hull: public deque<FN> //convex hull for maximum { void add(const FN& l) { //slopes coming in -1,-2,-3,...so add to front of deque rather than back (querying for maximum) //if adding to back, need to change the indices while (!empty() && back().m == l.m) { assert(back().b <= l.b); pop_back(); } while (size() >= 2 && comp(*prev(rbegin()),*rbegin(),l)) { pop_back(); } while (!empty() && back().m == l.m) { assert(back().b <= l.b); pop_back(); } if (!empty() && back().m == l.m) { pop_back(); } if (!empty()) assert(back().m != l.m); push_front(l); /* if (size() >= 2) { assert(prev(rbegin())->m != (*rbegin()).m); } */ } pair<ll,int> query(ll x) { /* while (size() >= 2 && begin()->eval(x) <= next(begin())->eval(x)) { pop_front(); } return {begin()->eval(x),begin()->id}; */ pair<ll,int> best = {0,0}; for (auto it = begin(); it != end(); ++it) { best = max(best,{it->eval(x),it->id}); } return best; } } hulls[maxk]; int main() { //ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> N >> K; for (int i = 1; i <= N; i++) { cin >> a[i]; pre[i] = a[i] + pre[i-1]; } for (int i = 1; i <= N; i++) { for (int k = 1; k <= K; k++) { //if (i-1 == 0 || pre[i-1] > pre[i-2] || // (pre[i-1] == pre[i-2] && // dp[i-1][k-1] > dp[i-2][k-1])) { hulls[k-1].add(FN(pre[i-1],dp[i-1][k-1]-pre[i-1]*pre[i-1],i-1)); //} //else cout << "discounted " << (i-1) << ' ' << (k-1) << '\n'; auto p = hulls[k-1].query(pre[i]); dp[i][k] = p.first; previous[i][k] = p.second; //printf("[%d][%d]: %d\n",i,k,dp[i][k]); } } //output cout << dp[N][K] << '\n'; int curr = N; int k = K; vector<int> ans; while (k > 0) { curr = previous[curr][k]; k--; ans.push_back(curr); } reverse(ans.begin(),ans.end()); for (int i: ans) { cout << i << ' '; } cout << '\n'; }

Compilation message (stderr)

sequence.cpp: In constructor 'FN::FN(ll, ll, int)':
sequence.cpp:15:11: warning: 'FN::b' will be initialized after [-Wreorder]
     ll m, b; //in this case, function is a line
           ^
sequence.cpp:14:9: warning:   'int FN::id' [-Wreorder]
     int id;
         ^~
sequence.cpp:16:5: warning:   when initialized here [-Wreorder]
     FN(ll _m, ll _b, int _id) : m(_m), b(_b), id(_id) {}
     ^~
#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...