# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
884865 | vjudge1 | 수열 (APIO14_sequence) | C++17 | 1058 ms | 83500 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 fi first
#define se second
const int N = 1e5 + 5;
const int K = 205;
const int mod = 1e9 + 7;
const int inf = 1e18 + 7;
#define all(v) (v).begin(), (v).end()
#define pii pair<long long, int>
using namespace std;
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
int n, k, trace[K][N], a[N];
long long dp[2][N], f[N];
long long cost(int i, int j){
return (f[n] - f[j]) * (f[j] - f[i - 1]);
}
void solve(int l, int r, int u, int v, int j){
if(l > r) return;
int mid = l + r >> 1;
pii best = {-inf, -1};
for(int pos = u; pos <= min(mid, v); pos++){
best = max(best, {dp[j & 1 ^ 1][pos - 1] + cost(pos, mid), pos});
}
dp[j & 1][mid] = best.fi;
trace[j][mid] = best.se;
if(best.se == -1) return;
solve(l, mid - 1, u, best.se, j);
solve(mid + 1, r, best.se, v, j);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
if(ifstream("sequence.in")){
freopen("sequence.in", "r", stdin);
freopen("sequence.out", "w", stdout);
}
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i];
f[i] = f[i - 1] + a[i];
dp[0][i] = -inf;
}
for(int j = 1; j <= k; j++){
for(int i = 0; i <= n; i++) dp[j & 1][i] = -inf;
solve(1, n - 1, 1, n - 1, j);
}
long long res = -1;
int pos = -1;
for(int i = 1; i < n; i++){
if(dp[k & 1][i] > res){
res = dp[k & 1][i];
pos = i;
}
}
cout << res << '\n';
stack<int> split;
for(; k; k--){
split.push(pos);
pos = trace[k][pos] - 1;
}
while(split.size()){
cout << split.top() << ' ';
split.pop();
}
return 0;
}
// tuntun
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... |