이 제출은 이전 버전의 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_container {
struct line {
ll a, b, idx;
line(ll a_, ll b_, int idx_) : a(a_), b(b_), idx(idx_) {}
line() : a(0LL), b(0LL), idx(0) {}
ll operator()(ll x) const {
return a * x + b;
}
};
ll floor_div(ll a, ll b) {
return a / b - ((a ^ b) < 0 && a % b);
}
ll intersect(const line& l1, const line& l2) {
return floor_div(l2.b - l1.b, l1.a - l2.a);
}
deque<line> dq;
void add(ll a, ll b, int idx) {
line cur(a, b, idx);
if (!dq.empty() && dq.back().a == a) {
if (dq.back().b > b) {
return;
} else {
dq.pop_back();
}
}
while (sz(dq) > 1 && intersect(cur, dq[sz(dq)-1]) <= intersect(dq[sz(dq)-1], dq[sz(dq)-2])) {
dq.pop_back();
}
dq.push_back(cur);
}
pair<ll, int> query(ll x) {
assert((!dq.empty()));
while (sz(dq) > 1 && dq[0](x) < dq[1](x)) {
dq.pop_front();
}
return {dq[0](x), dq[0].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<line_container> g(k);
vector<vector<int>> parent(n+1, vector<int>(k+1));
vector<ll> dp(k+1);
for (int i = 1; i <= n; i++) {
dp[0] = 0;
for (int j = 1; j < i && j <= k; j++) {
tie(dp[j], parent[i][j]) = g[j-1].query(p[i]);
}
for (int j = 0; j < i && j < k; j++) {
g[j].add(p[i], -p[i]*p[i] + dp[j], i);
}
}
cout << dp[k] << '\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... |