제출 #122549

#제출 시각아이디문제언어결과실행 시간메모리
122549jakob_nogler수열 (APIO14_sequence)C++14
0 / 100
168 ms131072 KiB
#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]; 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++){ int idx = n - 1; //q[n - 1] = dp[i - 1][n - 1] - m[n - 1] * (tot - sum[n - 2]); for(int j = n - 1; j >= 0; j--){ ll curr = m[j]; while(idx - 1 > j && m[idx - 1] * curr + q[idx - 1] >= m[idx] * curr + q[idx]) idx--; nxt[i][j] = idx; dp[i][j] = m[idx] * curr + q[idx]; q[j] = dp[i - 1][j] - m[j] * m[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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...