제출 #319931

#제출 시각아이디문제언어결과실행 시간메모리
319931tushar_2658수열 (APIO14_sequence)C++14
100 / 100
1829 ms82408 KiB
#include "bits/stdc++.h" using namespace std; const int maxn = 100005; typedef long long ll; ll dp[maxn][2]; int n, k; ll pref[maxn]; int best[maxn][202]; int cur, cnt; void compute(int l, int r, int optl, int optr){ if(l > r)return; int mid = (l + r) >> 1; ll BEST = -1; int opt = optl; for(int i = optl; i <= min(mid - 1, optr); ++i){ ll cost = dp[i][cur ^ 1] + pref[i] * (pref[mid] - pref[i]); if(BEST < cost){ BEST = cost; opt = i; } } dp[mid][cur] = BEST; best[mid][cnt] = opt; compute(l, mid - 1, optl, opt); compute(mid + 1, r, opt, optr); } int main(int argc, char const *argv[]) { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for(int i = 1; i <= n; ++i){ cin >> pref[i]; pref[i] = pref[i - 1] + pref[i]; } for(int i = 1; i <= k; ++i){ cur = i & 1; cnt = i; for(int j = 0; j <= n; ++j){ dp[j][cur] = -1; } compute(1, n, 1, n); } cout << dp[n][k & 1] << '\n'; cur = n; int cur_k = k; while(cur_k > 0){ cout << best[cur][cur_k] << ' '; cur = best[cur][cur_k]; --cur_k; } 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...