제출 #1217315

#제출 시각아이디문제언어결과실행 시간메모리
121731512345678Tricks of the Trade (CEOI23_trade)C++20
45 / 100
5777 ms407384 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long const ll nx=250005, kx=201, inf=1e18; ll n, k, c[nx], s[nx], qs[nx], dp[kx][nx], ans=-inf, res[nx], open[nx], tmp[nx]; void solve(int l, int r, int optl, int optr, int layer) { if (r<l) return; int md=(l+r)/2; pair<ll, ll> mx={-inf, 0}; for (int i=optl; i<=min(optr, md-1); i++) mx=max(mx, {dp[layer-1][i]+qs[i]-qs[md]+s[md], i}); dp[layer][md]=mx.first; solve(l, md-1, optl, mx.second, layer); solve(md+1, r, mx.second, optr, layer); } int main() { cin.tie(NULL)->sync_with_stdio(false); cin>>n>>k; for (int i=1; i<=n; i++) cin>>c[i], qs[i]=qs[i-1]+c[i]; for (int i=1; i<=n; i++) cin>>s[i]; for (int i=1; i<=k; i++) for (int j=0; j<=n; j++) dp[i][j]=-inf; for (int i=1; i<=k; i++) solve(i, n, 0, n, i); for (int i=k; i<=n; i++) ans=max(ans, dp[k][i]); for (int i=1; i<=n; i++) open[i]=dp[k][i]==ans, res[i]=res[i]||open[i]; for (int i=k-1; i>=1; i--) { set<ll> sv; for (int j=n; j>=1; j--) { tmp[j]=sv.find(dp[i][j]+qs[j])!=sv.end(); if (open[j]) sv.insert(dp[i+1][j]+qs[j]-s[j]); } for (int j=1; j<=n; j++) open[j]=tmp[j], res[j]=res[j]||open[j]; } cout<<ans<<'\n'; for (int i=1; i<=n; i++) cout<<res[i]; }
#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...