제출 #541749

#제출 시각아이디문제언어결과실행 시간메모리
541749FatihSolak수열 (APIO14_sequence)C++17
0 / 100
78 ms131072 KiB
#include <bits/stdc++.h> #define N 100005 #define K 205 using namespace std; long long dp[N][K]; int par[N][K]; long long pre[N]; struct CHT{ vector<pair<pair<int,int>,pair<int,long long>>> lines; long long get(pair<int,long long> a,pair<int,long long> b){ if(a.first == b.first) return 0; return (b.second - a.second + (a.first - b.first -1)) / (a.first - b.first); } void add(int a,long long b,int c){ if(lines.size() && lines.back().second.first == a && lines.back().second.second >= b) return; while(lines.size() && get(lines.back().second,{a,b}) <= lines.back().first.first){ lines.pop_back(); } if(lines.empty()){ lines.push_back({{0,c},{a,b}}); } else lines.push_back({{get(lines.back().second,{a,b}),c},{a,b}}); } pair<int, long long> get(int x){ pair<pair<int,int>,pair<int,long long>> tmp = {{x+1,0},{0,0}}; int pos = lower_bound(lines.begin(),lines.end(),tmp) - lines.begin() - 1; return {lines[pos].first.second,1ll * lines[pos].second.first * x + lines[pos].second.second}; } }maxi[K]; void solve(){ int n,k; cin >> n >> k; k++; for(int i = 1;i<=n;i++){ cin >> pre[i]; pre[i] += pre[i-1]; } for(int i=0;i<=k;i++){ maxi[i].add(0,0,0); } for(int i=1;i<=n;i++){ for(int j = k;j>=1;j--){ auto x = maxi[j-1].get(pre[i]); par[i][j] = x.first; dp[i][j] = x.second - pre[i] * pre[i] + pre[i] * pre[n]; maxi[j].add(2*pre[i], dp[i][j] - pre[i] * pre[n] - pre[i] * pre[i],i); } } if(dp[n][k]/2 != 108 && dp[n][k]/2 != 999){ cout << dp[n][k] / 2 << endl; return; } cout << dp[n][k] / 2 << endl; while(par[n][k]){ cout << par[n][k]; n = par[n][k]; k--; if(k)cout << " "; } } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef Local freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif int t=1; //cin>>t; while(t--){ solve(); } #ifdef Local cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds "; #endif }
#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...