제출 #987830

#제출 시각아이디문제언어결과실행 시간메모리
987830Gray수열 (APIO14_sequence)C++17
33 / 100
88 ms131072 KiB
#include <algorithm> #include <bits/stdc++.h> #define ll long long #define ld long double #define ff first #define ss second #define ln "\n" using namespace std; const ll MOD = 1e9+7; const unsigned long long INF = 1e19; struct line{ ll m, c; pair<ll, ll> id; line(ll m, ll c, pair<ll, ll> id):m(m),c(c),id(id){} ll eval(ll x){ return m*x+c; } long double inter(const line &alt){ return (ld)(c-alt.c)/(ld)(alt.m-m); } }; struct CHT{ deque<line> lc; void add(ll m, ll c, ll i, ll j){ while (lc.size()>1 and lc.front().inter(line(m, c, {i, j}))>=lc[1].inter(lc.front())) lc.pop_front(); lc.push_front(line(m, c, {i, j})); } pair<ll, pair<ll, ll>> query(ll x){ while (lc.size()>1 and lc.back().eval(x)<=lc[lc.size()-2].eval(x)) lc.pop_back(); return {lc.back().eval(x), lc.back().id}; } }; // dp[i] = dp[j]+(suff[j]-suff[i])*suff[i]; // dp[i] = dp[j]+suff[j]*suff[i]-suff[i]*suff[i]; void solve(){ ll n, k; cin >> n >> k; vector<ll> a(n); vector<ll> suff(n+1, 0); for (ll i=0; i<n; i++) cin >> a[i]; for (ll i=n-1; i>=0; i--){ suff[i]=suff[i+1]+a[i]; } vector<vector<ll>> dp(n, vector<ll>(k+1)); vector<vector<pair<ll, ll>>> con(n, vector<pair<ll, ll>>(k+1)); vector<CHT> chts(k+1); chts[0].add(suff[0], 0, -1, -1); ll mx = -1; pair<ll, ll> mxi = {-1, -1}; for (ll i=0; i<n; i++){ for (ll j=min(i, k); j>=1; j--){ auto ret = chts[j-1].query(suff[i]); dp[i][j] = ret.ff-suff[i]*suff[i]; con[i][j] = ret.ss; chts[j].add(suff[i], dp[i][j], i, j); if (j==k and mx<dp[i][j]){ mx=dp[i][j]; mxi = {i, j}; } } } cout << mx << ln; vector<ll> ans; while (make_pair(-1LL, -1LL)!=mxi){ ans.push_back(mxi.ff); mxi = con[mxi.ff][mxi.ss]; } for (ll i=k-1; i>=0; i--){ cout << ans[i] << " "; } cout << ln; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ll t=1; // cin >> t; while (t--) solve(); }
#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...