제출 #732632

#제출 시각아이디문제언어결과실행 시간메모리
732632ac2hu수열 (APIO14_sequence)C++14
71 / 100
70 ms131072 KiB
#include<bits/stdc++.h> using namespace std; using ll = long long; const int N = 1e5 + 10; const int K = 200 + 10; int n, k, b[N][K]; ll dp[N][K], a[N]; struct line { ll m, c;int id; ll eval(ll x) { return (ll)(m * x) + c; } long double intersectX(line l) { return (long double) (c - l.c) / (l.m - m); } }; signed main(){ iostream::sync_with_stdio(false);cin.tie(nullptr); cin >> n >> k; for(int i = 1;i<=n;i++)cin >> a[i]; for(int i = 1;i<=n;i++)a[i] += a[i - 1]; for(int l = 1;l<=k;l++){ deque<line> dq;dq.push_front({0, 0, 0}); for(int i = 1;i<=n;i++){ if(i > l){ while(dq.size() >= 2 && dq.back().eval(a[i]) <= dq[dq.size() -2].eval(a[i]))dq.pop_back(); dp[i][l] = dq.back().eval(a[i]); b[i][l] = dq.back().id; } if(i > l - 1){ line cur = {a[i], -a[i]*a[i] + dp[i][l - 1], i}; while(dq.size() >= 2 && cur.intersectX(dq[0]) <= cur.intersectX(dq[1])) dq.pop_front(); dq.push_front(cur); } } } cout << dp[n][k] << "\n"; vector<int> v = {n}; for(int l = k;l>=1;l--){ v.push_back(b[v.back()][l]); } reverse(v.begin(), v.end());v.pop_back();reverse(v.begin(), v.end()); for(int e : v)cout << e << " "; cout << "\n"; }
#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...