이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int mxN = 1e5+10;
const int mxK = 215;
ll m[mxN], dp[mxN][mxK];
int last[mxN][mxK];
ld intercept(int x, int y, int k) {
return (ld)(dp[y][k]-dp[x][k])/(ld)(m[x]-m[y]);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<ll> v(n+1), pref(n+1);
for(int i = 1; i <= n; i++) {
cin >> v[i];
pref[i] = pref[i-1]+v[i];
}
vector<deque<int>> q(k+5);
q[0].push_back(0);
for(int i = 1; i < n; i++) {
for(int j = min(k, i); j >= 1; j--) {
ll x = pref[n]-pref[i];
while(q[j-1].size() >= 2 && intercept(q[j-1][0], q[j-1][1], j-1) >= x)
q[j-1].pop_front();
int curr = q[j-1].front();
dp[i][j] = m[curr]*x + dp[curr][j-1] + pref[i]*x;
m[i] = -pref[i];
last[i][j] = curr;
bool ok = true;
while(q[j].size() && m[q[j].back()] == m[i]) {
if(dp[q[j].back()][j] <= dp[i][j]) q[j].pop_back();
else {
ok = false;
break;
}
}
if(!ok) continue;
while(q[j].size() >= 2 && intercept(q[j].back(), q[j][q[j].size()-2], j) <=
intercept(i, q[j][q[j].size()-2], j))
q[j].pop_back();
q[j].push_back(i);
}
}
int idx = -1; ll mx = -1;
for(int i = k; i < n; i++) {
if(dp[i][k] >= mx) {
mx = dp[i][k];
idx = i;
}
}
cout << mx << '\n';
int prev = idx;
vector<int> ans;
while(k) {
ans.push_back(prev);
prev = last[prev][k--];
}
reverse(ans.begin(), ans.end());
for(auto it : ans) {
cout << it << ' ';
}
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... |