이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 10;
const int K = 200 + 10;
int n, k, b[N][K];
ll dp[N][K], a[N];
struct line {
ll m, c;int id;
ll eval(ll x) { return (ll)(m * x) + c; }
long double intersectX(line l) { return (long double) (c - l.c) / (l.m - m); }
};
signed main(){
iostream::sync_with_stdio(false);cin.tie(nullptr);
cin >> n >> k;
for(int i = 1;i<=n;i++)cin >> a[i];
for(int i = 1;i<=n;i++)a[i] += a[i - 1];
for(int l = 1;l<=k;l++){
deque<line> dq;dq.push_front({0, 0, 0});
for(int i = 1;i<=n;i++){
if(i > l){
while(dq.size() >= 2 && dq.back().eval(a[i]) <= dq[dq.size() -2].eval(a[i]))dq.pop_back();
dp[i][l] = dq.back().eval(a[i]); b[i][l] = dq.back().id;
}
if(i > l - 1){
line cur = {a[i], -a[i]*a[i] + dp[i][l - 1], i};
while(dq.size() >= 2 && cur.intersectX(dq[0]) <= cur.intersectX(dq[1]))
dq.pop_front();
dq.push_front(cur);
}
}
}
cout << dp[n][k] << "\n";
vector<int> v = {n};
for(int l = k;l>=1;l--){
v.push_back(b[v.back()][l]);
}
reverse(v.begin(), v.end());v.pop_back();reverse(v.begin(), v.end());
for(int e : v)cout << e << " ";
cout << "\n";
}
# | 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... |