이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <utility>
#include <algorithm>
#include <list>
static std::vector<int> A;
static std::vector<int> psum;
static int n, k;
static std::vector<std::vector<int>> offset; // (k+1) x (n+1) -> offset
static std::list<int> answer;
static long long dp(void) {
if (n <= 1)
return 0LL;
int k1;
for (k1=0; k1<=k; k1++)
offset.push_back(std::vector<int> (n+1, 0));
std::vector<long long> prevrow(n+1, 0);
std::vector<long long> row(n+1, 0);
// k = 0 => all offsets are zero
for (k1=1; k1<=k; k1++) {
std::swap(row, prevrow);
row[2] = A[0] * A[1];
offset[k1][2] = 1;
for (int i=3; i<=n; i++) {
int last = offset[k1][i-1];
int next = last + 1;
long long val1 = prevrow[last] + psum[last] * (psum[i] - psum[last]);
long long val2 = 0LL;
while (next < i) {
val2 = prevrow[next] + psum[next] * (psum[i] - psum[next]);
if (val1 > val2)
break;
val1 = val2;
next ++;
}
row[i] = val1;
offset[k1][i] = next - 1;
}
}
int lastoffset = n;
k1 = k;
while (k1 > 0) {
lastoffset = offset[k1][lastoffset];
if (lastoffset < 1)
break;
answer.push_front(lastoffset);
k1 --;
}
return row[n];
}
int main(int argc, char const *argv[]) {
std::cin >> n >> k;
for (int i=0; i<n; i++) {
int a;
std::cin >> a;
A.push_back(a);
}
psum.push_back(0);
for (int i=0; i<n; i++)
psum.push_back(A[i] + psum[i]);
std::cout << dp() << std::endl;
for (int a: answer)
std::cout << a << " ";
std::cout << std::endl;
return 0;
}
# | 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... |