Submission #48734

#TimeUsernameProblemLanguageResultExecution timeMemory
48734zadrgaSplit the sequence (APIO14_sequence)C++14
0 / 100
84 ms86320 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define fi first #define se second #define INF (1LL << 55) #define MOD (1000 * 1000 * 1000 + 7) #define maxn 100111 #define maxk 211 typedef long long ll; typedef long double ld; typedef pair<int, int> pii; struct line{ ll k, n; int ind; }; ll k[maxn], n[maxn], ind[maxn]; int best_ind, top, bot; bool bad(int l1, int l2, int l3){ return (n[l3] - n[l1]) * (k[l1] - k[l2]) <= (n[l2] - k[l1]) * (k[l1] - k[l3]); } void add(ll cur_k, ll cur_n, int cur_ind){ if(top - bot > 0 && k[top - 1] == cur_k){ if(cur_n > n[top - 1]) top--; else return; } k[top] = cur_k; n[top] = cur_n; ind[top] = cur_ind; while(top - bot >= 3 && bad(top - 2, top - 1, top)){ k[top - 1] = k[top]; n[top - 1] = n[top]; ind[top - 1] = ind[top]; top--; } top++; } inline ll val(int ind, ll x){ return k[ind] * x + n[ind]; } pair<ll, int> query(ll x){ if(best_ind >= top) best_ind = top - 1; while(best_ind + 1 < top && val(best_ind, x) <= val(best_ind + 1, x)) best_ind++; return mp(val(best_ind, x), ind[best_ind]); } ll dp[maxn][2], pre[maxn], arr[maxn]; int from[maxn][maxk]; ll sum(int i, int j){ if(i > j) return 0LL; return pre[j] - pre[i - 1]; } int main(){ int n, k; scanf("%d%d", &n, &k); for(int i = 1; i <= n; i++){ scanf("%lld", arr + i); pre[i] = pre[i - 1] + arr[i]; } for(int i = 0; i <= n; i++){ for(int j = 0; j < 2; j++) dp[i][j] = -INF; } for(int i = 1; i <= n - 1; i++) dp[i][1] = sum(1, i) * sum(i + 1, n); for(int j = 2; j <= k; j++){ top = bot = 0; best_ind = 0; int jj = j % 2; int prev = 1 - jj; for(int i = j; i <= n - 1; i++){ add(sum(1, i - 1), dp[i - 1][prev] - sum(1, i - 1) * sum(1, n), i - 1); pair<ll, int> cur = query(sum(1, i)); dp[i][jj] = cur.fi + sum(i + 1, n) * sum(1, i); from[i][j] = cur.se; } } int ans = 0; for(int i = 1; i <= n - 1; i++){ if(dp[i][k % 2] > dp[ans][k % 2]) ans = i; } printf("%lld\n", dp[ans][k % 2]); for(int i = k; i >= 1; i--){ printf("%d ", ans); ans = from[ans][i]; } return 0; }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:78: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:80:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", 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...