# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
750560 | M_W | Split the sequence (APIO14_sequence) | C++17 | 18 ms | 8528 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ar array<long long, 3>
using namespace std;
long long a[100001], dp[200][100001][2], qs[100001];
vector<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};
idx = min(idx, int(cht.size() - 1));
while(idx < cht.size() - 1 && f(cht[idx + 1], x) >= f(cht[idx], x)) idx++;
return {f(cht[idx], x), cht[idx][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;
for(int i = 1; i <= K; i++){
for(int j = 1; j <= N; j++){
if(j >= i - 1){
long long const_val = qs[N] * qs[j] - (qs[j] * 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);
if(!ans) for(int i = 1; i <= K; i++) printf("%d ", i);
else{
for(int i = K; i > 0; i--){
printf("%d ", maxj);
maxj = dp[i][maxj][1];
}
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |