제출 #382277

#제출 시각아이디문제언어결과실행 시간메모리
382277ijxjdjd수열 (APIO14_sequence)C++14
71 / 100
148 ms131076 KiB
#include <bits/stdc++.h> #define FR(i, N) for (int i = 0; i < int(N); i++) #define all(x) begin(x), end(x) using namespace std; using ll = long long; const int MAXN = (int)(1e5) + 5; const int MAXK = 200+5; int opt[MAXN][MAXK]; ll dp[MAXN][MAXK]; ll arr[MAXN]; ll prefSum[MAXN]; ll calc(int l, int r) { if (l == 0) { return dp[r][0]; } else { return dp[r][0] - (prefSum[r]-prefSum[l-1])*prefSum[l-1] - dp[l-1][0]; } } void recur(int k, int tl, int tr, int l, int r) { if (tl > tr) { return; } int tm = (tl + tr)/2; ll best = (ll)(1e18); int id = -1; for (int i = min(r, tm-1); i >= l; i--) { if (dp[i][k-1] + calc(i+1, tm) < best) { best = dp[i][k-1] + calc(i+1, tm); id = i; } } opt[tm][k] = id; dp[tm][k] = best; recur(k, tl, tm-1, l, id); recur(k, tm+1, tr, id, r); } int main() { cin.tie(0); cin.sync_with_stdio(0); int N, K; cin >> N >> K; ll sm = 0; FR(i, N) { cin >> arr[i]; dp[i][0] = (i > 0 ? dp[i-1][0] : 0) + arr[i]*sm; opt[i][0] = -1; sm += arr[i]; prefSum[i] = sm; } for (int i = 1; i <= K; i++) { recur(i, 0, N-1, 0, N-1); } cout << dp[N-1][0] - dp[N-1][K] << '\n'; int u = N-1; for (int iter = K; iter >= 1; iter--) { cout << opt[u][iter]+1 << '\n'; u = opt[u][iter]; } return 0; }
#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...