Submission #101677

#TimeUsernameProblemLanguageResultExecution timeMemory
101677arman_ferdousSplit the sequence (APIO14_sequence)C++17
71 / 100
2045 ms83784 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 100001; const int K = 201; const ll oo = 1e18; int n, k; ll S[N], dp[N][2]; int opt[N][K]; struct CHT{ vector<ll> m, c, id; int last; void init() { last = 0; m.clear(); c.clear(); id.clear(); } bool bad(int i, int j, int k) { return 1.0 * (c[j] - c[i]) * (m[i] - m[k]) >= 1.0 * (c[k] - c[i]) * (m[i] - m[j]); } void insert(ll M, ll C, int id_) { m.push_back(M); c.push_back(C); id.push_back(id_); int sz = m.size(); while(sz >= 3 && bad(sz-3, sz-2, sz-1)) { m.erase(m.end()-2); c.erase(c.end()-2); id.erase(id.end()-2); sz--; } } ll f(int i, ll X) { return m[i] * X + c[i]; } pair<ll,int> get(ll X) { int sz = m.size(); last = min(last, sz-1); while(last + 1 < sz && f(last,X) <= f(last+1,X)) last++; return {f(last,X), id[last]}; } }ds; int main() { scanf("%d %d", &n, &k); for(int i = 1; i <= n; i++) { scanf("%lld", &S[i]); S[i] += S[i-1]; } for(int kk = 1; kk <= k; kk++) { int row = (kk&1); ds.init(); ds.insert(0,0,0); for(int i = kk; i <= n; i++) { auto got = ds.get(S[i]); dp[i][row] = got.first; opt[i][kk] = got.second; ds.insert(S[i], dp[i][(row^1)] - S[i]*S[i], i); } } printf("%lld\n", dp[n][(k&1)]); int pos = n; for(int cnt = k; cnt > 0; cnt--) { printf("%d ", opt[pos][cnt]); pos = opt[pos][cnt]; } return 0; }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:39:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:41:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", &S[i]);
   ~~~~~^~~~~~~~~~~~~~~
#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...