Worker (二分)

Worker

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

这道题一开始想的二分没写出来。。后来又用最小公倍数emmmm又卡了(今天就是一直wa wa wa wa。。。自闭)

最后还是二分,不过思路变了,直接求mid值组成的sum,当sum==m时取出mid值,依次输出。

#include<iostream>
#include<string.h>
using namespace std;



int main(){
	int flag;
	long long n,m;

	while(cin>>n>>m){
	long long a[10010];
		
		long long maxx=0;
		
		for(int i=0;i<n;i++){
			cin>>a[i];
			
			maxx=max(maxx,a[i]);
		}
		long long l=0,r=m*maxx,mid;
		flag=0;
		while(l<r){
			mid=(r+l+1)/2;
			long long sum=0;
		for(int i=0;i<n;i++){
			sum+=(mid/a[i]);
		}
			if(sum<m){
				l=mid;
			}
			else if(sum>m){
				r=mid-1;
			}
			else{
				flag=1;
				break;
			}
		}
		if(flag){
			cout<<"Yes"<<endl;
			cout<<mid/a[0];
			for(int i=1;i<n;i++){
				cout<<" "<<mid/a[i];
			}
			cout<<endl;
		}
		else{
			cout<<"No"<<endl;
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值