# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1048531 | manhlinh1501 | Split the sequence (APIO14_sequence) | C++17 | 29 ms | 131072 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>
using namespace std;
using pii = pair<int, int>;
using i64 = long long;
const int MAXN = 1e5 + 5;
const int MAXK = 205;
#define ALL(a) (a).begin(), (a).end()
int N, K;
int a[MAXN];
i64 prefix[MAXN];
namespace subtask {
i64 dp[MAXN][MAXK];
pii trace[MAXN][MAXK];
void solution() {
K++;
memset(dp, -0x3f, sizeof dp);
dp[0][0] = 0;
for(int i = 1; i <= N; i++) {
for(int j = 0; j <= i; j++) {
for(int z = 1; z <= K; z++)
// dp[i][z] = max(dp[i][z], dp[j - 1][z - 1] + (prefix[i] - prefix[j - 1]) * (prefix[N] - prefix[i]));
if(dp[i][z] < dp[j][z - 1] + (prefix[i] - prefix[j]) * (prefix[N] - prefix[i])) {
dp[i][z] = dp[j][z - 1] + (prefix[i] - prefix[j]) * (prefix[N] - prefix[i]);
trace[i][z] = {j, z - 1};
}
}
}
// for(int i = 1; i <= N; i++) {
// for(int j = 1; j <= K; j++)
// printf("%d %d %d %d\n", i, j, trace[i][j].first, trace[i][j].second);
// }
printf("%lld\n", dp[N][K]);
pii cur = {N, K};
vector<int> ans;
while(trace[cur.first][cur.second].second) {
cur = trace[cur.first][cur.second];
// printf("%d ", cur.first);
ans.emplace_back(cur.first);
}
reverse(ALL(ans));
for(int x : ans) printf("%d ", x);
}
}
signed main() {
#define TASK "code"
if (fopen(TASK ".inp", "r")) {
freopen(TASK ".inp", "r", stdin);
freopen(TASK ".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> K;
for(int i = 1; i <= N; i++) cin >> a[i];
for(int i = 1; i <= N; i++) prefix[i] = prefix[i - 1] + a[i];
subtask::solution();
return (0 ^ 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... |