이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <utility>
#include <algorithm>
#include <list>
#include <limits>
#include <cassert>
typedef long long ll;
static std::vector<int> A;
static std::vector<long long> psum;
static int n, k;
static std::vector<std::vector<int>> offset; // (k+1) x (n+1) -> offset
static std::list<int> answer;
// assume a1 <= a2. Want smallest x(>=0) s.t. a2.x + b2 >= a1.x + b1
ll intersect(ll a1, ll b1, ll a2, ll b2) {
if (a1 != a2) {
ll x = (b1 - b2) / (a2 - a1);
if ((a2 * x + b2) >= (a1 * x + b1))
return x;
else
return x + 1;
}
if (b1 > b2)
return std::numeric_limits<ll>::max();
return 0LL;
}
static long long dp(void) {
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);
for (int i=2; i<=k1; i++)
row[i] = 0LL;
row[k1+1] = prevrow[k1] + psum[k1] * A[k1];
offset[k1][k1+1] = k1;
std::vector<ll> slopes;
std::vector<ll> intercepts;
std::vector<int> indices;
std::vector<ll> xmins;
slopes.push_back(psum[k1]);
intercepts.push_back(- psum[k1] * psum[k1] + prevrow[k1]);
indices.push_back(k1);
xmins.push_back(0);
int lastidx = 0; // remember the convex hull location for previous i
for (int i=(k1+2); i<=n; i++) {
// find j to maximize: prevrow[j] + psum[j] * (psum[i] - psum[j]);
// j goes from k1 to i-1
ll slope = psum[i-1];
ll intercept = -psum[i-1] * psum[i-1] + prevrow[i-1];
while (true) {
ll x = intersect(slopes.back(), intercepts.back(), slope, intercept);
if (x <= xmins.back()) {
slopes.pop_back();
intercepts.pop_back();
indices.pop_back();
xmins.pop_back();
if (!slopes.empty())
continue;
}
slopes.push_back(slope);
intercepts.push_back(intercept);
indices.push_back(i-1);
xmins.push_back(x);
break;
}
if (lastidx > (xmins.size() - 1))
lastidx = xmins.size() - 1;
int idx = lastidx;
for (idx=lastidx; (idx+1) < xmins.size(); idx++) {
if (psum[i] >= xmins[idx] and psum[i] < xmins[idx + 1])
break;
}
int j = indices[idx];
row[i] = prevrow[j] + psum[j] * (psum[i] - psum[j]);
offset[k1][i] = j;
lastidx = idx;
}
/*
std::cout << "k " << k1 << " val ";
for (long long x: row)
std::cout << x << " ";
std::cout << std::endl;
std::cout << "k " << k1 << " off ";
for (long x: offset[k1])
std::cout << x << " ";
std::cout << std::endl;
*/
}
int lastoffset = n;
k1 = k;
while (k1 > 0) {
lastoffset = offset[k1][lastoffset];
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;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'long long int dp()':
sequence.cpp:90:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (lastidx > (xmins.size() - 1))
~~~~~~~~^~~~~~~~~~~~~~~~~~~~
sequence.cpp:95:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (idx=lastidx; (idx+1) < xmins.size(); idx++) {
~~~~~~~~^~~~~~~~~~~~~~
# | 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... |