제출 #723868

#제출 시각아이디문제언어결과실행 시간메모리
723868Urvuk3수열 (APIO14_sequence)C++17
100 / 100
1022 ms86568 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long const int INF=1e9,MOD=998244353; const ll LINF=1e18; #define fi first #define se second #define pii pair<int,int> #define pll pair<ll,ll> #define mid ((l+r)/2) #define sz(a) (int((a).size())) #define all(a) a.begin(),a.end() #define endl "\n" #define pb push_back void PRINT(int x) {cerr << x;} void PRINT(ll x) {cerr << x;} void PRINT(double x) {cerr << x;} void PRINT(char x) {cerr << '\'' << x << '\'';} void PRINT(string x) {cerr << '\"' << x << '\"';} void PRINT(bool x) {cerr << (x ? "true" : "false");} template<typename T,typename V> void PRINT(pair<T,V>& x){ cerr<<"{"; PRINT(x.fi); cerr<<","; PRINT(x.se); cerr<<"}"; } template<typename T> void PRINT(T &x){ int id=0; cerr<<"{"; for(auto _i:x){ cerr<<(id++ ? "," : ""); PRINT(_i); } cerr<<"}"; } void _PRINT(){ cerr<<"]\n"; } template<typename Head,typename... Tail> void _PRINT(Head h,Tail... t){ PRINT(h); if(sizeof...(t)) cerr<<", "; _PRINT(t...); } #define Debug(x...) cerr<<"["<<#x<<"]=["; _PRINT(x) void Md(ll& x){ if(x>=MOD) x%=MOD; } struct Line{ ll k,m,l,r,id; ll operator()(ll x){ return k*x+m; } }; ll Intersect(Line l1, Line l2){ if(((l2.m-l1.m)>0) ^ ((l1.k-l2.k)>0)){ return ((l2.m-l1.m)/(l1.k-l2.k))-1; } else{ return (l2.m-l1.m)/(l1.k-l2.k); } } deque<Line> Hull; int it; void AddLine(Line f){ if(!Hull.empty() && Hull[0].k==f.k){ if(Hull[0].m>=f.m) return; Hull.pop_front(); if(!Hull.empty()) Hull[0].l=-LINF; if(it) it--; } if(sz(Hull)==0){ f.l=-LINF,f.r=LINF; Hull.push_front(f); it++; } else if(sz(Hull)==1){ ll sec=Intersect(f,Hull[0]); f.l=-LINF,f.r=sec; Hull.push_front(f); Hull[1].l=sec+1; it++; } else{ while(Hull[0].r<=Intersect(f,Hull[0])){ Hull.pop_front(); Hull[0].l=-LINF; if(it) it--; } ll sec=Intersect(f,Hull[0]); f.l=-LINF,f.r=sec; Hull.push_front(f); Hull[1].l=sec+1; it++; } } void Solve(){ int N,K; cin>>N>>K; vector<ll> a(N+1); for(int i=1;i<=N;i++) cin>>a[i]; vector<ll> p(N+1,0); for(int i=1;i<=N;i++) p[i]=p[i-1]+a[i]; vector<vector<int>> where(K+1,vector<int>(N+1,0)); vector<vector<ll>> dp(2,vector<ll>(N+1,0)); for(int j=1;j<=K;j++){ it=sz(Hull)-1; ///dp[i][k]=min(dp[j][k-1]+(p[i]-p[j])*(p[n]-p[i])) ///dp[i][k]=min(dp[j][k-1]-p[j]*(p[n]-p[i]))+p[i]*(p[n]-p[i]) for(int i=j;i<=N-1;i++){ ll x=p[N]-p[i]; Line f={-p[i-1],dp[0][i-1],0,0,i-1}; AddLine(f); while(!(Hull[it].l<=x && x<=Hull[it].r)){ it--; } ll dp_new=Hull[it](p[N]-p[i])+p[i]*(p[N]-p[i]); if(dp[1][i]<dp_new){ dp[1][i]=dp_new; where[j][i]=Hull[it].id; } } dp[0]=dp[1]; fill(all(dp[1]),0); Hull.clear(); } ll res=0; int idx; int c=K; for(int i=1;i<=N-1;i++){ if(dp[0][i]>=res){ idx=i; res=dp[0][i]; } } cout<<res<<endl; while(c!=0){ cout<<idx<<" "; idx=where[c--][idx]; } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; t=1; //cin>>t; while(t--){ Solve(); } return 0; }

컴파일 시 표준 에러 (stderr) 메시지

sequence.cpp: In function 'void Solve()':
sequence.cpp:152:27: warning: 'idx' may be used uninitialized in this function [-Wmaybe-uninitialized]
  152 |         idx=where[c--][idx];
      |                           ^
#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...