이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//Dost SEFEROĞLU
#pragma GCC optimize("O3,unroll-loops,Ofast")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define vi vector<int>
const int N = 1e5+1, K = 2e2+1,inf = 1e18;
int dp2[N],dp[N];
int32_t stat[N][K];
struct Line {
int a,b;
int eval(int x) {
return a*x+b;
}
int div(int a,int b) {
return a/b - ((a^b) < 0 && a%b);
}
int isect(const Line& l) {
if (l.a == a) return b <= l.b ? -inf : inf;
return div(b-l.b,l.a-a);
}
};
struct CHT {
deque<pair<Line,int>> cht;
pii query(int x) {
while (cht.size() > 1 && cht[0].ff.eval(x) < cht[1].ff.eval(x)) cht.pop_front();
return {cht.front().ff.eval(x),cht.front().ss};
}
void add(Line l,int idx) {
while (cht.size() > 1 && cht[cht.size()-2].ff.isect(cht.back().ff) <= cht.back().ff.isect(l)) {
cht.pop_back();
}
cht.push_back({l,idx});
}
};
void solve() {
int n,k;
cin >> n >> k;
vi a(n+1),p(n+1,0);
for (int i=1;i<=n;i++) cin >> a[i];
for (int i=1;i<=n;i++) p[i] = p[i-1]+a[i];
CHT cht;
for (int j=1;j<=k;j++) {
cht.add(Line{0,0},0);
for (int i=1;i<=n;i++) {
auto fff = cht.query(p[i]);
dp[i] = fff.ff,stat[i][j] = fff.ss;
cht.add({p[i],dp2[i]-(p[i]*p[i])},i);
}
cht.cht.clear();
for (int i=1;i<=n;i++) dp2[i] = dp[i];
}
cout << dp2[n] << endl;
vi cuts;
int ptr = n,ptr2 = k;
while (ptr2) {
cuts.push_back(stat[ptr][ptr2]);
ptr = stat[ptr][ptr2];
ptr2--;
}
reverse(cuts.begin(),cuts.end());
for (auto it : cuts) cout << it << " ";
cout << endl;
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Dodi
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
# | 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... |