이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << ' ' <<
#define nl << '\n'
deque<array<int, 3>> hull[200+1];
int f(int x, int i, int j){
if(j<0) j += hull[i].size();
return hull[i][j][0]*x + hull[i][j][1] - x*x;
}
bool bad(int m, int c, int i){
int x1 = c-hull[i][0][1];
int d1 = hull[i][0][0]-m;
if(d1<0) d1 *= -1LL, x1 *= -1LL;
c = hull[i][1][1], m = hull[i][1][0];
int x2 = c-hull[i][0][1];
int d2 = hull[i][0][0]-m;
if(d2<0) d2 *= -1LL, x2 *= -1LL;
return x1*d2 >= x2*d1;
}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int n, k; cin >> n >> k;
int a[n], ans = 0, ind = 0;
for(int &i : a) cin >> i;
for(int i=n-2; i>=0; --i) a[i] += a[i+1];
hull[0].push_back({a[0], 0});
int32_t p[n][k];
for(int i=1; i<n; ++i){
for(int j=k-1; j>=0; --j){
if(hull[j].empty()) continue;
while(hull[j].size()>1 and f(a[i], j, -2) >= f(a[i], j, -1)) hull[j].pop_back();
int dp = f(a[i], j, -1);
p[i][j] = hull[j].back()[2];
int m = a[i];
while(!hull[j+1].empty()){
if(hull[j+1][0][0]==m){
dp = max(dp, hull[j+1][0][1]);
hull[j+1].pop_front();
}else if(hull[j+1].size() > 1 and bad(m, dp, j+1)){
hull[j+1].pop_front();
}else break;
}
hull[j+1].push_front({m, dp, i});
if(dp >= ans){
ans = dp;
ind = i;
}
}
}
// for(auto i : hull[1]) cout << i[2] nl;
vector<int> res = {ind};
for(int j=k-1; j>=0; --j){
res.push_back(p[ind][j]);
ind = res.back();
}
cout << ans nl;
for(int i=res.size()-2; i>=0; --i) cout << res[i] << ' ';
}
# | 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... |