이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100005, K = 205;
ll dp[K][N], sum[N], val[N], m[N], q[N], tot = 0;
int nxt[K][N];
ll get_val(int x, int l, int idx){
return m[x] * m[l] - m[l] * m[l] + dp[idx - 1][l];
}
double get_in(int a, int b, int idx){
return (double) 1.0 * (m[a] * m[a] - dp[idx - 1][a] - m[b] * m[b] + dp[idx - 1][b]) / (m[b] - m[a]);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
for(int i = 0; i < n; i++){
cin >> val[i];
tot += val[i];
sum[i] += val[i];
sum[i + 1] += sum[i];
}
for(int i = 0; i < n; i++) m[i] = tot - (i == 0 ? 0 : sum[i - 1]);
for(int i = 1; i <= k; i++){
val[n - 1] = n - 1;
dp[i][n - 1] = 0;
int idx = n - 1, nn = n - 1;
for(int j = n - 2; j >= 0; j--){
while(idx > nn && get_val(j, val[idx], i) <= get_val(j, val[idx - 1], i)) idx--;
nxt[i][j] = val[idx];
dp[i][j] = get_val(j, val[idx], i);
while(idx > nn && get_in(val[nn], val[nn + 1], i) <= get_in(j, val[nn], i)) nn++;
val[--nn] = j;
}
}
cout << dp[k][0] << endl;
int curr = nxt[k][0];
for(int i = k - 1; i >= 0; i--){
cout << curr << " ";
curr = nxt[i][curr];
}
cout << endl;
}
# | 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... |