Submission #1214232

#TimeUsernameProblemLanguageResultExecution timeMemory
1214232emptypringlescanCookies (JOI23_cookies)C++17
25 / 100
508 ms1114112 KiB
#include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(0); cin.tie(0); int n,m; cin >> n; int arr[n]; bool st1=true; for(int i=0; i<n; i++){ cin >> arr[i]; if(arr[i]!=1) st1=false; } cin >> m; vector<int> brr(m); for(int i=0; i<m; i++) cin >> brr[i]; if(st1){ bitset<505> bs[n+1]; bs[0][0]=1; int num=-1; for(int i=1; i<=n; i++){ for(int j=0; j<m; j++) bs[i]|=bs[i-1]<<brr[j]; if(bs[i][n]){ num=i; break; } } if(num==-1){ cout << -1; return 0; } else{ vector<int> ans; int cur=n; for(int i=num; i>0; i--){ for(int j=0; j<m; j++){ if(bs[i-1][cur-brr[j]]){ ans.push_back(brr[j]); cur-=brr[j]; break; } } } cout << ans.size() << '\n'; cur=1; for(int i:ans){ cout << i << ' '; for(int j=0; j<i; j++){ cout << cur << ' '; cur++; } cout << '\n'; } } } else if(m==1){ int tot=0; for(int i=0; i<n; i++) tot+=arr[i]; if(tot%brr[0]){ cout << -1; return 0; } set<pair<int,int> > got; for(int i=0; i<n; i++) got.insert({arr[i],i+1}); vector<vector<int> > ans; for(int i=0; i<tot/brr[0]; i++){ if((int)got.size()<brr[0]){ cout << -1; return 0; } ans.push_back({}); vector<pair<int,int> > nxt; for(int j=0; j<brr[0]; j++){ pair<int,int> yay=*(--got.end()); ans.back().push_back(yay.second); yay.first--; if(yay.first) nxt.push_back(yay); got.erase(--got.end()); } for(auto i:nxt) got.insert(i); } cout << tot/brr[0] << '\n'; for(auto i:ans){ cout << brr[0] << ' '; for(int j:i) cout << j << ' '; cout << '\n'; } } else{ int tot=0; for(int i=0; i<n; i++) tot+=arr[i]; vector<int> tp; for(int i=0; i<n; i++){ for(int j=0; j<arr[i]; j++) tp.push_back(i); } int dp[tot+5][1<<tot]; int prv[tot+5][1<<tot]; memset(dp,-1,sizeof(dp)); dp[0][0]=0; int num=-1; for(int i=0; i<=tot; i++){ if(dp[i][(1<<tot)-1]!=-1){ num=i; break; } for(int j=0; j<(1<<tot); j++){ if(dp[i][j]==-1) continue; pair<int,int> cum[n]; for(int k=0; k<n; k++) cum[k]={0,-1}; for(int k=0; k<tot; k++){ if(!(j&(1<<k))){ cum[tp[k]].first++; cum[tp[k]].second=k; } } vector<pair<int,int> > srt; for(int i=0; i<n; i++) if(cum[i].first) srt.push_back(cum[i]); sort(srt.begin(),srt.end()); reverse(srt.begin(),srt.end()); for(int k=0; k<m; k++){ if(brr[k]>(int)srt.size()) continue; int nxt=j; for(int l=0; l<brr[k]; l++) nxt^=(1<<srt[l].second); if(dp[i+1][nxt]==-1){ dp[i+1][nxt]=k; prv[i+1][nxt]=j; } } } } if(num==-1){ cout << -1; return 0; } vector<int> yey; cout << num << '\n'; int cur=(1<<tot)-1; for(int i=num; i>0; i--){ yey.push_back(brr[dp[i][cur]]); cur=prv[i][cur]; } sort(yey.begin(),yey.end(),greater<int>()); set<pair<int,int> > got; for(int i=0; i<n; i++) got.insert({arr[i],i+1}); vector<vector<int> > ans; for(int i=0; i<num; i++){ if((int)got.size()<yey[i]){ assert(false); } ans.push_back({}); vector<pair<int,int> > nxt; for(int j=0; j<yey[i]; j++){ pair<int,int> yay=*(--got.end()); ans.back().push_back(yay.second); yay.first--; if(yay.first) nxt.push_back(yay); got.erase(--got.end()); } for(auto i:nxt) got.insert(i); } for(auto i:ans){ cout << i.size() << ' '; for(int j:i) cout << j << ' '; cout << '\n'; } } }
#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...