Submission #968568

#TimeUsernameProblemLanguageResultExecution timeMemory
968568huutuanCookies (JOI23_cookies)C++14
70 / 100
1071 ms178316 KiB
#include<bits/stdc++.h> using namespace std; #ifdef sus const int N=100, M=20; #else const int N=15e3+10, M=3010; #endif int f[N][M]; int n, m, a[N], b[N], pf[N], _a[N], d[N]; int nxt[N], prv[N]; int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i=1; i<=n; ++i) cin >> _a[i]; cin >> m; for (int i=1; i<=m; ++i) cin >> b[i]; for (int i=1; i<=n; ++i) a[i]=_a[i]; int sum=accumulate(a+1, a+n+1, 0); sort(a+1, a+n+1, greater<int>()); int mx=b[m]; if (mx<n){ for (int i=mx+1; i<=n; ++i) a[mx]+=a[i]; } for (int i=1; i<=mx; ++i) pf[i]=pf[i-1]+a[mx-i+1]; for (int i=1; i<=m; ++i) d[i]=b[i]-b[i-1]; reverse(d+1, d+m+1); int cur=0; for (int i=1; i<=m; ++i) nxt[cur]=cur+d[i], prv[cur+d[i]]=cur, cur+=d[i]; memset(f, 0x3f, sizeof f); f[0][0]=0; for (int i=0; i<mx; i=nxt[i]){ for (int j=0; j<=pf[i]; ++j){ int l=0, r=sum; while (l<=r){ int mid=(l+r)>>1; bool check=1; for (int k=i; k<=nxt[i]; ++k) check&=j+mid*(k-i)<=pf[k]; if (check) l=mid+1; else r=mid-1; } for (int val=f[i][j]; val<=r; ++val){ f[nxt[i]][j+val*(nxt[i]-i)]=min(f[nxt[i]][j+val*(nxt[i]-i)], val); } } } if (f[mx][sum]>1e9){ cout << -1 << '\n'; return 0; } vector<int> tr; vector<int> sz; while (mx){ int val=f[mx][sum]; tr.push_back(val); sum-=val*(mx-prv[mx]); mx=prv[mx]; } reverse(tr.begin(), tr.end()); for (int i=0; i<m; ++i){ int cnt=tr[i]-(i?tr[i-1]:0); while (cnt--) sz.push_back(b[m-i]); } priority_queue<pair<int, int>> pq; vector<vector<int>> ans; for (int i=1; i<=n; ++i) a[i]=_a[i]; for (int i=1; i<=n; ++i) pq.emplace(a[i], i); for (int i:sz){ ans.emplace_back(); for (int j=0; j<i; ++j){ if (pq.empty()){ cout << -1 << '\n'; return 0; } ans.back().push_back(pq.top().second); pq.pop(); } for (int j:ans.back()){ --a[j]; if (a[j]) pq.emplace(a[j], j); } } cout << ans.size() << '\n'; for (auto &i:ans){ cout << i.size(); for (int j:i) cout << ' ' << 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...