Submission #750567

#TimeUsernameProblemLanguageResultExecution timeMemory
750567M_WSplit the sequence (APIO14_sequence)C++17
33 / 100
270 ms131072 KiB
#include <bits/stdc++.h> #define ar array<long long, 3> using namespace std; long long a[100001], dp[200][100001][2], qs[100001]; deque<ar> cht; int idx = 0; bool should_pop(ar cur, ar line1, ar line2){ if(cur[0] == line1[0] && cur[1] > line1[1]) return true; return (line2[1] - cur[1]) * (cur[0] - line1[0]) >= (line1[1] - cur[1]) * (cur[0] - line2[0]); } long long f(ar line, long long x){ return line[0] * x + line[1]; } pair<long long, int> query(long long x){ if(cht.empty()) return {0, 0}; while(idx < cht.size() - 1 && f(cht[idx + 1], x) >= f(cht[idx], x)) cht.pop_front(); return {f(cht[0], x), cht[0][2]}; } int main(int argc, char* argv[]){ int N, K; scanf("%d %d", &N, &K); for(int i = 1; i <= N; i++){ scanf("%lld", &a[i]); qs[i] = qs[i - 1] + a[i]; } long long ans = 0; int maxj = 0; cht.push_back({0, 0, 0}); for(int i = 1; i <= K; i++){ for(int j = 1; j < N; j++){ if(j >= i - 1){ long long const_val = qs[j] * (qs[N] - qs[j]); pair<long long, int> res = query(qs[j]); dp[i][j][0] = const_val + res.first; dp[i][j][1] = res.second; if(dp[i][j][0] >= ans){ ans = dp[i][j][0]; maxj = j; } } ar cur = {qs[j], dp[i - 1][j][0] - (qs[N] * qs[j]), j}; while(cht.size() > 1){ int sz = cht.size(); if(should_pop(cur, cht[sz - 1], cht[sz - 2])) cht.pop_back(); else break; } if(cht.empty() || cur[0] > cht.back()[0] || (cur[0] == cht.back()[0] && cur[1] >= cht.back()[1])) cht.push_back(cur); } cht.clear(); } printf("%lld\n", ans); for(int i = K; i > 0; i--){ printf("%d ", maxj); maxj = dp[i][maxj][1]; } return 0; }

Compilation message (stderr)

sequence.cpp: In function 'std::pair<long long int, int> query(long long int)':
sequence.cpp:19:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |   while(idx < cht.size() - 1 && f(cht[idx + 1], x) >= f(cht[idx], x)) cht.pop_front();
      |         ~~~~^~~~~~~~~~~~~~~~
sequence.cpp: In function 'int main(int, char**)':
sequence.cpp:24:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |   int N, K; scanf("%d %d", &N, &K);
      |             ~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:26:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |     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...