Worker hdu-6576

Avin meets a rich customer today. He will earn 1 million dollars if he can solve a hard problem. There are n warehouses and m workers. Any worker in the i-th warehouse can handle ai orders per day. The customer wonders whether there exists one worker assignment method satisfying that every warehouse handles the same number of orders every day. Note that each worker should be assigned to exactly one warehouse and no worker is lazy when working.
Input
The first line contains two integers n (1 ≤ n ≤ 1, 000), m (1 ≤ m ≤ 1018). The second line contains n integers. The i-th integer ai (1 ≤ ai ≤ 10) represents one worker in the i-th warehouse can handle ai orders per day.
Output
If there is a feasible assignment method, print “Yes” in the first line. Then, in the second line, print n integers with the i-th integer representing the number of workers assigned to the i-th warehouse.
Otherwise, print “No” in one line. If there are multiple solutions, any solution is accepted.
Sample Input
2 6
1 2
2 5
1 2
Sample Output
Yes
4 2
No

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	long long n;
	long long m;
	while(cin>>n>>m)
	{
		long long aa[1234];long long maxx=0;
		for(long long i=0;i<n;i++)
		{
			cin>>aa[i];
			maxx=max(aa[i],maxx);
		}
		long long l=0;long long r=maxx*m;int flag=0;long long sum;long long mid;
		while(l<r)
		{
			sum=0;
			mid=(l+r+1)/2;
			for(long long i=0;i<n;i++)
				{
					long long cc=mid/aa[i];
					//if(cc*aa[i]!=mid) cc++;
					sum+=cc;
				}
			if(sum>m) r=mid-1;
			else if(sum<m) l=mid;
			else if(sum==m) 
			{
				flag=1;
				break;
			}
		}
		if(flag)
		{
			cout<<"Yes"<<endl;
			cout<<mid/aa[0];
			for(int i=1;i<n;i++)
				cout<<" "<<mid/aa[i];
				cout<<endl;
		}
		else cout<<"No"<<endl;
	}
	return 0;
}
发布了31 篇原创文章 · 获赞 0 · 访问量 340

猜你喜欢

转载自blog.csdn.net/weixin_44828107/article/details/103000469