이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ff first
#define ss second
#define ln "\n"
using namespace std;
const ll MOD = 1e9+7;
const unsigned long long INF = 1e19;
ll divi(ll a, ll b) {
return a / b + ((a ^ b) < 0 && a % b);
}
struct line{
ll m, c;
pair<ll, ll> id;
line(ll m, ll c, pair<ll, ll> id):m(m),c(c),id(id){}
ll eval(ll x){
return m*x+c;
}
ll inter(const line &alt){
return divi(c-alt.c,alt.m-m);
}
};
struct CHT{
deque<line> lc;
void add(ll m, ll c, ll i, ll j){
while (lc.size()>1 and lc.front().inter(line(m, c, {i, j}))>=lc[1].inter(lc.front())) lc.pop_front();
lc.push_front(line(m, c, {i, j}));
}
pair<ll, pair<ll, ll>> query(ll x){
while (lc.size()>1 and lc.back().eval(x)<=lc[lc.size()-2].eval(x)) lc.pop_back();
return {lc.back().eval(x), lc.back().id};
}
};
// dp[i] = dp[j]+(suff[j]-suff[i])*suff[i];
// dp[i] = dp[j]+suff[j]*suff[i]-suff[i]*suff[i];
void solve(){
ll n, k; cin >> n >> k;
vector<ll> a(n);
vector<ll> suff(n+1, 0);
for (ll i=0; i<n; i++) {
cin >> a[i];
}
for (ll i=n-1; i>=0; i--){
suff[i]=suff[i+1]+a[i];
}
vector<vector<ll>> dp(n, vector<ll>(k+1));
vector<vector<pair<ll, ll>>> con(n, vector<pair<ll, ll>>(k+1));
vector<CHT> chts(k+1);
chts[0].add(suff[0], 0, -1, -1);
ll mx = -1;
pair<ll, ll> mxi = {-1, -1};
for (ll i=1; i<n; i++){
for (ll j=min(i, k); j>=1; j--){
auto ret = chts[j-1].query(suff[i]);
dp[i][j] = ret.ff-suff[i]*suff[i];
con[i][j] = ret.ss;
chts[j].add(suff[i], dp[i][j], i, j);
if (j==k and mx<dp[i][j]){
mx=dp[i][j];
mxi = {i, j};
}
}
}
cout << mx << ln;
vector<ll> ans;
while (make_pair(-1LL, -1LL)!=mxi){
ans.push_back(mxi.ff);
mxi = con[mxi.ff][mxi.ss];
}
for (ll i=k-1; i>=0; i--){
cout << ans[i] << " ";
}
cout << ln;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ll t=1;
// cin >> t;
while (t--) 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... |