Submission #48728

#TimeUsernameProblemLanguageResultExecution timeMemory
48728zadrgaSplit the sequence (APIO14_sequence)C++14
0 / 100
100 ms86196 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; }; vector<line> env; int best_ind; bool bad(int l1, int l2, int l3){ return (env[l3].n - env[l1].n) * (env[l1].k - env[l2].k) <= (env[l2].n - env[l1].n) * (env[l1].k - env[l3].k); } void add(line a){ if(env.size() > 0){ if(env.back().k == a.k){ if(a.n > env.back().n) env.pop_back(); else return; } } env.pb(a); while(env.size() >= 3 && bad(env.size() - 3, env.size() - 2, env.size() - 1)) env.erase(env.end() - 2); } ll val(int ind, ll x){ return env[ind].k * x + env[ind].n; } pair<ll, int> query(ll x){ if(best_ind >= env.size()) best_ind = env.size() - 1; while(best_ind + 1 < env.size() && val(best_ind, x) >= val(best_ind + 1, x)) best_ind++; return mp(val(best_ind, x), env[best_ind].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++){ env.clear(); 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]), i - 1}); pair<ll, int> cur = query( sum(i + 1, n) ); 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 'std::pair<long long int, int> query(ll)':
sequence.cpp:50:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if(best_ind >= env.size())
     ~~~~~~~~~^~~~~~~~~~~~~
sequence.cpp:53:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  while(best_ind + 1 < env.size() && val(best_ind, x) >= val(best_ind + 1, x))
        ~~~~~~~~~~~~~^~~~~~~~~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:72: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:74: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...