제출 #416230

#제출 시각아이디문제언어결과실행 시간메모리
416230DEQK수열 (APIO14_sequence)C++17
100 / 100
1613 ms81924 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxN = 1e5 + 5, maxK = 205; const ll INF = numeric_limits<ll>::max(); int n, k; ll a[maxN]; ll dp[maxN], _dp[maxN]; int opt[maxN]; int trace[maxK][maxN]; ll cost(int i, int j) { assert(j < i); return _dp[j] + a[j] * (a[i] - a[j]); } void solve(int l, int r, int lo, int hi) { if(l > r) return; int mid = (l + r) >> 1; opt[mid] = -1; for(int i = lo; i <= min(hi, mid - 1); i++) { if(opt[mid] == -1 || cost(mid, opt[mid]) < cost(mid, i)) { opt[mid] = i; } } if(opt[mid] == -1) dp[mid] = -INF; else dp[mid] = cost(mid, opt[mid]); solve(l, mid - 1, lo, opt[mid]); solve(mid + 1, r, opt[mid], hi); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for(int i = 1; i <= n; i++) { cin >> a[i]; a[i] += a[i - 1]; } for(int layer = 1; layer <= k; layer++) { _dp[0] = -INF; for(int i = 1; i <= n; i++) _dp[i] = dp[i]; solve(1, n, 1, n); for(int i = 1; i <= n; i++) trace[layer][i] = opt[i]; } cout << dp[n] << '\n'; vector<int> ans; for(int i = k, cur = n; i; i--) { ans.push_back(trace[i][cur]); cur = trace[i][cur]; } for(int i = (int)ans.size() - 1; i >= 0; i--) if(ans[i]) cout << ans[i] << ' '; }
#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...