이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sz(x) (int)size(x)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define rep(i, a, b) for(int i = a; i < (b); ++i)
typedef pair<int, int> pii;
typedef vector<int> vi;
// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html
template<class Fun>
class y_combinator_result {
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
mt19937 rng((unsigned)chrono::steady_clock::now().time_since_epoch().count());
struct Line {
mutable ll k, m, p;
int idx;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
static const ll inf = LLONG_MAX;
ll div(ll a, ll b) {
return a / b - ((a ^ b) < 0 && a % b);
}
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = inf, 0;
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(ll k, ll m, int idx) {
auto z = insert({k, m, 0, idx}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
pair<ll, int> query(ll x) {
assert(!empty());
auto l = *lower_bound(x);
return {l.k * x + l.m, l.idx};
}
};
void solve() {
int n, k;
cin >> n >> k;
vector<ll> p(n+1);
for (int i = 1; i <= n; i++) {
cin >> p[i];
p[i] += p[i-1];
}
vector<vector<int>> parent(n+1, vector<int>(k+1));
vector<array<ll, 2>> dp(n+1);
for (int i = 0; i <= n; i++) {
dp[i][0] = 0;
}
for (int j = 1; j <= k; j++) {
LineContainer g;
for (int i = j + 1; i <= n; i++) {
g.add(p[i-1], -p[i-1]*p[i-1] + dp[i-1][(j&1)^1], i-1);
tie(dp[i][j&1], parent[i][j]) = g.query(p[i]);
}
}
cout << dp[n][k&1] << '\n';
vector<int> result;
int idx = n;
for (int i = k; i >= 1; i--) {
result.push_back(parent[idx][i]);
idx = parent[idx][i];
}
for (int i = k-1; i >= 0; i--) {
cout << result[i] << ' ';
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int t = 1;
while (t--) {
solve();
}
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... |