Submission #319924

#TimeUsernameProblemLanguageResultExecution timeMemory
319924tushar_2658Split the sequence (APIO14_sequence)C++14
0 / 100
69 ms82660 KiB
#include "bits/stdc++.h" using namespace std; const int maxn = 100005; using ll = long long; ll dp[maxn][2]; int n, k; ll a[maxn], pref[maxn]; int best[maxn][202]; int cur, cnt; void compute(int l, int r, int optl, int optr){ if(l > r)return; int mid = (l + r) >> 1; if(optl == optr){ for(int i = l; i <= r; ++i){ dp[i][cur] = dp[optl][cur ^ 1] + pref[optl] * (pref[mid] - pref[optl]); best[mid][cnt] = optl; } return; } int opt = optl; for(int i = optl; i <= min(mid - 1, optr); ++i){ ll cost = dp[i][cur ^ 1] + pref[i] * (pref[mid] - pref[i]); if(dp[mid][cur] < cost){ dp[mid][cur] = cost; opt = i; } } best[mid][cnt] = opt; compute(l, mid - 1, optl, opt); compute(mid + 1, r, opt, optr); } int main(int argc, char const *argv[]) { scanf("%d %d", &n, &k); for(int i = 1; i <= n; ++i){ scanf("%lld", &a[i]); pref[i] = pref[i - 1] + a[i]; } for(int i = 1; i <= k; ++i){ cur = i & 1; cnt = i; for(int j = 0; j <= n; ++j){ dp[j][cur] = -1; } compute(1, n, 1, n); } printf("%lld\n", dp[n][k & 1]); cur = n; int cur_k = k; while(cur_k > 0){ printf("%d ", best[cur][cur_k]); cur = best[cur][cur_k]; --cur_k; } return 0; }

Compilation message (stderr)

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