Submission #43703

#TimeUsernameProblemLanguageResultExecution timeMemory
43703baactreeSplit the sequence (APIO14_sequence)C++14
0 / 100
861 ms83448 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]; double 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<int> dq; int cur = i & 1; int pre = (i - 1) & 1; for (int j = 2; j <= n; j++) { if (sum[j - 1] == sum[j - 2]) { dp[cur][j] = dp[pre][j - 1]; par[i][j] = j - 1; continue; } ll a = sum[j - 1]; ll b = dp[pre][j - 1] - sum[j - 1] * sum[j - 1]; dd[j - 1] = make_pair(a, b); while (dq.size() > 1 && calc(dd[dq.back()], dd[j-1]) <= calc(dd[dq[dq.size() - 2]], dd[j-1])) dq.pop_back(); dq.push_back(j-1); while (dq.size() > 1 && calc(dd[dq[0]], dd[dq[1]]) <= sum[j]) dq.pop_front(); dp[cur][j] = dd[dq[0]].first * sum[j] + dd[dq[0]].second; par[i][j] = dq[0]; } } 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: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:16: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...