Submission #43701

#TimeUsernameProblemLanguageResultExecution timeMemory
43701baactreeSplit the sequence (APIO14_sequence)C++14
0 / 100
25 ms4828 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]; ll calc(pair<ll, ll> a, pair<ll, ll> b) { return ((double)a.second - b.second) / (b.first - a.first); } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &arr[i]); sum[i] = sum[i - 1] + arr[i]; } for (int i = 1; i <= k; i++) { deque<pair<ll, ll> > dq; int idx = 1; int cur = i & 1; int pre = (i - 1) & 1; for (int j = 2; j <= n; j++) { ll a = sum[j - 1]; ll b = dp[pre][j - 1] - sum[j - 1] * sum[j - 1]; pair<ll, ll> now = make_pair(a, b); while (dq.size() > 1 && calc(dq.back(), now) <= calc(dq[dq.size() - 2], now)) dq.pop_back(); dq.push_back(now); while (dq.size() > 1 && calc(dq[0], dq[1]) <= sum[j]) { dq.pop_front(); idx++; } dp[cur][j] = dq[0].first * sum[j] + dq[0].second; par[i][j] = idx; } } printf("%lld\n", dp[k&1][n]); while (k) { printf("%d ", par[k][n]); n = par[k][n]; k--; } printf("\n"); return 0; }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:13: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:15: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...