이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define int long long
struct Line {
mutable int k, m, p, tr;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : vector<Line> {
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
static const ll inf = LLONG_MAX;
ll div(ll a, ll b) { // floored division
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 tr) {
auto z = insert(end(), {k, m, 0, tr}), 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));
}
int id=0;
pair<int, int> query(ll x) {
assert(!empty());
while ((begin()+id)->p<x) ++id;
return {(begin()+id)->k * x + (begin()+id)->m, (begin()+id)->tr};
}
};
const int N=1e5+10;
int n, k, a[N], pf[N], f[2][N];
int32_t tr[N][210];
LineContainer cht;
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (int i=1; i<=n; ++i) cin >> a[i];
partial_sum(a, a+n+1, pf);
int res=0;
for (int j=1; j<=k; ++j){
cht.add(0, 0, 0);
cht.id=0;
for (int i=1; i<=n; ++i){
auto tmp=cht.query(pf[i]);
tr[i][j]=tmp.second;
f[1][i]=tmp.first;
cht.add(pf[i], f[0][i]-pf[i]*pf[i], i);
}
cht.clear();
memcpy(f[0], f[1], sizeof f[1]);
}
cout << f[0][n] << '\n';
vector<int> ans;
while (n){
if (tr[n][k]) ans.push_back(tr[n][k]);
n=tr[n][k];
--k;
}
reverse(ans.begin(), ans.end());
for (int i:ans) cout << i << ' ';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int32_t main()':
sequence.cpp:57:8: warning: unused variable 'res' [-Wunused-variable]
57 | int res=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... |