Submission #156819

#TimeUsernameProblemLanguageResultExecution timeMemory
156819jhnah917Split the sequence (APIO14_sequence)C++14
0 / 100
18 ms4840 KiB
#include <bits/stdc++.h> #define x first #define y second #define all(v) v.begin(), v.end() using namespace std; typedef long long ll; typedef pair<ll, ll> p; struct Line{ ll a, b, c; ll get(ll x){ return a*x+b; } }; struct CHT{ vector<Line> stk; int idx = 1; void insert(Line v){ while(stk.size() >= 2){ if(v.a == stk.back().a && v.b >= stk.back().b) stk.pop_back(); else if((stk.back().b - v.b)*(stk.back().a - stk[stk.size()-2].a) < (v.a - stk.back().a)*(stk[stk.size()-2].b - v.b)){ stk.pop_back(); }else break; } if(stk.back().a == v.a && stk.back().b <= v.b && v.c) return; stk.push_back(v); } p query(ll x){ while(idx+1 < stk.size() && (stk[idx].b - stk[idx+1].b) < (stk[idx+1].a - stk[idx].a) * x) idx++; return {stk[idx].get(x), stk[idx].c}; } }; ll arr[101010]; ll sum[101010]; ll dp[2][101010]; int track[222][101010]; vector<int> ans; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; for(int i=1; i<=n; i++) cin >> arr[i], sum[i] = sum[i-1] + arr[i]; for(int i=1; i<=k; i++){ CHT cht; cht.stk.reserve(101010); cht.insert({0, 0, 0}); for(int j=1; j<=n; j++){ auto now = cht.query(sum[j]); dp[i&1][j] = now.x; track[i][j] = now.y; cht.insert({sum[j], dp[(i-1)&1][j] - sum[j]*sum[j], j}); } } cout << dp[k&1][n] << "\n"; int now = n; ans.push_back(-1); for(int i=1; i<=k; i++){ ans.push_back(track[k-i+1][now]); now = track[k-i+1][now]; } sort(all(ans)); for(int i=1; i<=k; i++){ if(!ans[i]) ans[i] = 1; if(ans[i] <= ans[i-1]) ans[i] = ans[i-1] + 1; } for(int i=1; i<=k; i++) cout << ans[i] << " "; }

Compilation message (stderr)

sequence.cpp: In member function 'p CHT::query(ll)':
sequence.cpp:33:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(idx+1 < stk.size() && (stk[idx].b - stk[idx+1].b) < (stk[idx+1].a - stk[idx].a) * x) idx++;
         ~~~~~~^~~~~~~~~~~~
#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...