直接判断分成x组能否满足,从大到小平均分是最合理的方案
#include<bits/stdc++.h> using namespace std; #define N 200006 int suf[N],n,k,m[N],c[N]; vector<int>v[N]; long long t[N]; int judge(int x){ //分成x组 for(int i=1;i<=k;i++)t[i]=1ll*c[i]*x; for(int i=1;i<=k;i++) if(suf[i]>t[i])return 0; return 1; } int main(){ cin>>n>>k; for(int i=1;i<=n;i++)scanf("%d",&m[i]); sort(m+1,m+1+n); for(int i=1;i<=n;i++)suf[m[i]]++; for(int i=k;i>=1;i--)suf[i]+=suf[i+1]; for(int i=1;i<=k;i++)scanf("%d",&c[i]); int L=1,R=n,mid,ans; while(L<=R){ mid=L+R>>1; if(judge(mid)) R=mid-1,ans=mid; else L=mid+1; } int p=1; for(int i=1;i<=n;i++){ v[p].push_back(m[i]); p++;if(p>ans)p-=ans; } cout<<ans<<" "; for(int i=1;i<=ans;i++){ cout<<v[i].size()<<' '; for(auto x:v[i])cout<<x<<" "; puts(""); } }