제출 #670081

#제출 시각아이디문제언어결과실행 시간메모리
670081JooDdae수열 (APIO14_sequence)C++17
71 / 100
113 ms131072 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; int n, k, bt[100100][202]; ll s[100100], dp[100100][202]; deque<array<ll, 3>> t[202]; bool check(int k, int a, int b, int c) { return (t[k][b][0]-t[k][a][0]) * (t[k][c][1]-t[k][a][1]) >= (t[k][c][0]-t[k][a][0]) * (t[k][b][1]-t[k][a][1]); } void f(int u, int k) { if(k == 0) return; f(bt[u][k], k-1); cout << u << " "; } int main(){ cin.tie(0)->sync_with_stdio(0); cin >> n >> k; for(int i=1;i<=n;i++) cin >> s[i]; for(int i=1;i<=n;i++) s[i] += s[i-1]; t[0].push_back({s[n], 0, 0}); int mx = 0; for(int i=1;i<n;i++) { for(int j=min(i, k);j>=1;j--) { int b = int(t[j-1].size())-1; while(t[j-1].size() > 2 && check(j-1, b-2, b-1, b)) t[j-1][b-1] = t[j-1][b], b--, t[j-1].pop_back(); while(t[j-1].size() > 1 && t[j-1][0][0]*s[i]+t[j-1][0][1] <= t[j-1][1][0]*s[i]+t[j-1][1][1]) t[j-1].pop_front(); auto [X, Y, id] = t[j-1][0]; dp[i][j] = X*s[i]+Y - s[i]*s[i], bt[i][j] = id; t[j].push_back({s[n]+s[i], dp[i][j] - s[n]*s[i], i}); } if(dp[i][k] >= dp[mx][k]) mx = i; } cout << dp[mx][k] << "\n"; f(mx, k); }
#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...