求助,80pts
查看原帖
求助,80pts
769006
crzcqh楼主2024/1/12 21:58
#include<bits/stdc++.h>
using namespace std;
int n,m,f,l,r,mid,ans,tmp;
int a[1024],b[1024],sum[1024];
bool check(int now,int l){
	if(f-tmp<sum[mid]) return false;
	if(now==0) return true;
	bool s; 
	for(int i=1;i<=m;i++){
		if(a[i]>=b[now]){
			a[i]-=b[now];
			if(a[i]<b[1]) tmp+=a[i];
			if(b[now-1]==b[now]) s=check(now-1,i);
			else s=check(now-1,1);
			if(a[i]<b[1]) tmp-=a[i];
			a[i]+=b[now];
			if(s) return true;
		}
	}
}
int main(){
	cin>>m;
	for(int i=1;i<=m;i++) cin>>a[i],f+=a[i];
	cin>>n;
	for(int i=1;i<=n;i++) cin>>b[i];
	sort(b+1,b+n+1);
	for(int i=1;i<=n;i++) sum[i]=sum[i-1]+b[i];
	while(sum[n]>f&&n) n--;
	l=1,r=n;
	while(l<=r){
		mid=(l+r)>>1;
		if(check(mid,1)) ans=mid,l=mid+1;
		else r=mid-1;
	}
	cout<<ans;
	return 0;
}


2024/1/12 21:58
加载中...