Submission #43707

#TimeUsernameProblemLanguageResultExecution timeMemory
43707baactreeSplit the sequence (APIO14_sequence)C++14
100 / 100
847 ms84376 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n, k; ll dp[2][100005]; int par[205][100005]; ll sum[100005]; int arr[100005]; pair<ll, ll> dd[100005]; long double calc(pair<ll, ll> a, pair<ll, ll> b) { return ((long double)a.second - b.second) / (b.first - a.first); } int main() { scanf("%d%d", &n, &k); set<int> s; for (int i = 1; i <= n; i++) { scanf("%d", &arr[i]); sum[i] = sum[i - 1] + arr[i]; if (!arr[i])s.insert(i); } for (int i = 1; i <= k; i++) { deque<int> dq; int cur = i & 1; int pre = (i - 1) & 1; for (int j = 1; j <= n; j++) { if (!arr[j]) { dp[cur][j] = dp[cur][j - 1]; par[i][j] = par[i][j - 1]; continue; } while (dq.size() > 1 && calc(dd[dq[0]], dd[dq[1]]) <= sum[j]) dq.pop_front(); if (!dq.empty()) { dp[cur][j] = dd[dq[0]].first * sum[j] + dd[dq[0]].second; par[i][j] = dq[0]; } ll a = sum[j]; ll b = dp[pre][j] - sum[j] * sum[j]; dd[j] = make_pair(a, b); while (dq.size() > 1 && calc(dd[dq.back()], dd[j]) <= calc(dd[dq[dq.size() - 2]], dd[j])) dq.pop_back(); dq.push_back(j); } } printf("%lld\n", dp[k & 1][n]); vector<int> ans; while (k) { ans.push_back(par[k][n]); n = par[k][n]; k--; } for (int i = 0; i < ans.size(); i++) { s.erase(ans[i]); if (!ans[i]) { printf("%d ", *s.begin()); s.erase(s.begin()); } else printf("%d ", ans[i]); } printf("\n"); return 0; }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:52:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < ans.size(); i++) {
                    ^
sequence.cpp:14:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &k);
                       ^
sequence.cpp:17:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &arr[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...