제출 #26499

#제출 시각아이디문제언어결과실행 시간메모리
26499oneshadab수열 (APIO14_sequence)C++14
71 / 100
2000 ms93704 KiB
#include <bits/stdc++.h> using namespace std; #define For(i,N) Forr(i, 0, N) #define Forr(i,a,b) Fotr(i, a, b, 1) #define Fotr(i,a,b,c) for(int i=(a);i<(b);i+=(c)) #define FOREACH(it, x) for(__typeof__((x).begin()) it=(x).begin();it!=(x).end();it++) #define MEM(a,x) memset(a,x,sizeof(a)) #define BCHK(a,x) (((a)>>(x))&1) #define BSET(a,x) ((a)|(1<<(x))) #define BCLR(a,x) ((a)&(~(1<<(x)))) #define BTGL(a,x) ((a)^(1<<(x))) #define FMT(...) (sprintf(CRTBUFF, __VA_ARGS__)?CRTBUFF:0) #define read() freopen("input.txt","r",stdin) #define write() freopen("output.txt","w",stdout) #define cpp_io() {ios_base::sync_with_stdio(false);cin.tie(NULL);} #define BUFFSIZE 30000 #define INF 1000000000 #define MOD 1000000007 #define MAX 100100 #define pb push_back #define mkpr make_pair #define pii pair<int, int> #define fi first #define si second typedef long long ll; char CRTBUFF[BUFFSIZE]; #define dbg(args...) {cout<<"-->";debugger::call(#args,args);cout<<"\n";} struct debugger{ static void call(const char* it){} template<typename T, typename ... aT> static void call(const char* it, T a, aT... rest){ string b; for(;*it&&*it!=',';++it) if(*it!=' ')b+=*it; cout<<b<<"="<<a<<" "; call(++it, rest...); } }; /* Template Ends */ const ll is_query = -(1LL<<62); struct Line { ll m, b, idx; mutable function<const Line*()> succ; bool operator<(const Line& rhs) const { if (rhs.b != is_query) return m < rhs.m; const Line* s = succ(); if (!s) return 0; ll x = rhs.m; return b - s->b < (s->m - m) * x; } }; struct HullDynamic : public multiset<Line> { // will maintain upper hull for maximum bool bad(iterator y) { auto z = next(y); if (y == begin()) { if (z == end()) return 0; return y->m == z->m && y->b <= z->b; } auto x = prev(y); if (z == end()) return y->m == x->m && y->b <= x->b; return (x->b - y->b)*(z->m - y->m) >= (y->b - z->b)*(y->m - x->m); } void insert_line(ll m, ll b, ll idx) { auto y = insert({ m, b, idx}); y->succ = [=] { return next(y) == end() ? 0 : &*next(y); }; if (bad(y)) { erase(y); return; } while (next(y) != end() && bad(next(y))) erase(next(y)); while (y != begin() && bad(prev(y))) erase(prev(y)); } pair<ll, ll> eval(ll x) { auto it=lower_bound((Line) { x, is_query }); auto l = *it; return {l.m * x + l.b, it->idx}; } }; ll A[MAX+10], S[MAX+10]; ll dp[2][MAX+10]; int fd[202][MAX+10]; int main() { //read(); cpp_io(); //begin int n, k; while(cin >> n >> k){ For(i, n) cin >> A[i]; For(i, n) S[i]=A[i]+(i?S[i-1]:0); For(j, n) dp[0][j]=fd[0][j]=0; Forr(i, 1, k+1){ int x=i&1; HullDynamic h; For(j, n){ if(j==0){ dp[x][j]=-INF; fd[i][j]=0; } else{ tie(dp[x][j], fd[i][j])=h.eval(S[j]); } h.insert_line(S[j], dp[x^1][j]-S[j]*S[j], j); } } ll ans=dp[k&1][n-1]; cout << ans << "\n"; int j=fd[k][n-1]; for(int i=k-1; i>=0; i--){ cout << j+1; if(i) cout << " "; j=fd[i][j]; } cout << "\n"; } return 0; }
#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...