이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <assert.h>
#define MAXN 100005
#define MAXK 205
using namespace std;
typedef long long int ll;
vector<ll> dpprev, dpnow;
int n, k, arr[MAXN], dprev[MAXN][MAXK], layer;
int s[MAXN];
void DCT(int a, int b, int op1, int op2) {
if (a > b) return;
int mid = (a + b) / 2;
int theop = op1;
for (int op = max(layer, op1); op <= min(mid-1, op2); op++) {
ll rev = dpprev[op-1] + 1LL * s[op] * (1LL * (s[mid] - s[op]));
if (dpnow[mid-1] < rev)
theop = op,
dpnow[mid-1] = rev,
dprev[mid][layer] = theop;
}
DCT(a, mid-1, op1, theop), DCT(mid+1, b, theop, op2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> arr[i];
s[0] = 0;
for (int i = 1; i <= n; i++)
s[i] = s[i-1] + arr[i];
for (int i = 1; i <= n; i++)
dpprev.push_back(0),
dpnow.push_back(-1);
for (layer = 1; layer <= k; layer++) {
for (int i = 1; i <= n; i++)
dpnow[i-1] = -1;
DCT(layer, n, 1, n);
dpprev = dpnow;
}
cout << dpnow.back() << "\n";
bool ap = 0;
int cnt = 0;
int x = n;
layer--;
while (dprev[x][layer]) {
if (ap) cout << " ";
cout << dprev[x][layer];
ap = 1;
x = dprev[x][layer];
layer--;
cnt++;
}
cout << "\n";
assert(cnt == k);
}
# | 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... |