이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC target("avx2")
//#pragma GCC optimization("O3")
//#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAXK = 200+10;
const int MAXN = 2e5+100;
int N, K, a[MAXN], opt[MAXN][MAXK];
ll pr[MAXN], dp[MAXN], dp2[MAXN];
struct line{
ll m, q, i;
};
ll eval(line a, ll x){
return (a.m * x + a.q);
}
bool pback(line a, line b, line c){
return (a.q - b.q) * (c.m - b.m) <= (b.q - c.q) * (b.m - a.m);
}
void solve(){
cin >> N >> K;
rep(i, 1, N + 1){
cin >> a[i];
pr[i] = pr[i - 1] + a[i];
}
rep(k, 1, K + 1){
deque<line> q;
q.push_back({0, 0, 0});
rep(i, 1, N + 1){
while(SZ(q) > 1 && eval(q[0], pr[i]) <= eval(q[1], pr[i])){
q.pop_front();
}
int j = q[0].i;
dp2[i] = dp[j] + (pr[i] - pr[j]) * (pr[N] - pr[i]);
opt[i][k] = j;
line cur = {pr[i], dp[i] - pr[i] * pr[N], i};
while(SZ(q) > 1 && pback(cur, q.back(), q[SZ(q) - 2])){
q.pop_back();
}
q.push_back(cur);
}
swap(dp, dp2);
}
ll ans = -1; int ind = 0;
per(i, N - 1, 1){
if(dp[i] > ans){
ans = dp[i], ind = i;
}
}
cout << ans << "\n";
per(k, K, 1){
cout << ind << " ";
ind = opt[ind][k];
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
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... |