Distributing Ballot Boxes(二分,思维)

B - Distributing Ballot Boxes HDU - 4190
Today, besides SWERC’11, another important event is taking place in Spain which rivals it in importance: General Elections. Every single resident of the country aged 18 or over is asked to vote in order to choose representatives for the Congress of Deputies and the Senate. You do not need to worry that all judges will suddenly run away from their supervising duties, as voting is not compulsory.
The administration has a number of ballot boxes, those used in past elections. Unfortunately, the person in charge of the distribution of boxes among cities was dismissed a few months ago due to nancial restraints. As a consequence, the assignment of boxes to cities and the lists of people that must vote in each of them is arguably not the best. Your task is to show how efficiently this task could have been done.
The only rule in the assignment of ballot boxes to cities is that every city must be assigned at least one box. Each person must vote in the box to which he/she has been previously assigned. Your goal is to obtain a distribution which minimizes the maximum number of people assigned to vote in one box.
In the first case of the sample input, two boxes go to the fi rst city and the rest to the second, and exactly 100,000 people are assigned to vote in each of the (huge!) boxes in the most efficient distribution. In the second case, 1,2,2 and 1 ballot boxes are assigned to the cities and 1,700 people from the third city will be called to vote in each of the two boxes of their village, making these boxes the most crowded of all in the optimal assignment.
Input
The fi rst line of each test case contains the integers N (1<=N<=500,000), the number of cities, and B(N<=B<=2,000,000), the number of ballot boxes. Each of the following N lines contains an integer ai,(1<=ai<=5,000,000), indicating the population of the ith city.
A single blank line will be included after each case. The last line of the input will contain -1 -1 and should not be processed.
Output
For each case, your program should output a single integer, the maximum number of people assigned to one box in the most efficient assignment.
Sample Input
2 7
200000
500000

4 6
120
2680
3400
200

-1 -1
Sample Output
100000
1700
题目大意:(n个城市,b个信箱,输入n行为每个城市的信封数。请均衡分配信箱,使得信箱里的信封最少。求信箱中最多装多少封信使最大值最小
今天,除了SWERC’11之外,另一个重要的事件正在西班牙发生,它的重要性与之抗衡:大选。每个年满18岁或18岁以上的居民都被要求投票,以便选出代表大会和参议院的代表。你不必担心所有的法官会突然逃避他们的监督职责,因为投票不是强制性的。

政府有许多投票箱,这些都是在过去的选举中使用的。不幸的是,几个月前,由于经济拮据,负责在城市间分发箱子的人被解雇了。因此,分配给城市的盒子和每个城市必须投票的人的名单可以说不是最好的。你的任务是展示这项任务的效率。

向城市分配投票箱的唯一规则是,每个城市必须至少分配一个投票箱。每个人都必须在他/她以前分配到的框中投票。您的目标是获得一个分配,使最大人数的人分配给投票在一个框中。

在样本输入的第一种情况下,两个框分别指向第一个城市,其余的框指向第二个城市,每一个城市都有10万人参与投票(巨大!)最有效分配的盒子。在第二种情况下,将1、2、2和1个投票箱分配给城市,第三个城市的1700人将被召集到各自村庄的两个投票箱中投票,使这些投票箱成为最优分配中最拥挤的。

输入

每个测试用例的第一行包含整数N(1<=N<=500000),即城市数,以及B(N<=B<=2000000),即投票箱数。以下N行中的每一行都包含一个整数ai,(1<=ai<=5000000),表示第i个城市的人口。

每个案例后面都有一个空行。输入的最后一行将包含-1-1,不应进行处理。

输出

对于每种情况,您的程序都应该输出一个整数,即在最有效的分配中分配给一个框的最大人数。

#include <bits/stdc++.h>
using namespace std;
int a[500010],n,b;
bool ch(int x){
	int sum=0;
	for(int i=1;i<=n;i++){
		sum+=(a[i]-1)/x+1;//注意向上取整,因为题目说只要有信就需要信箱
		if(sum>b) return 1;
	}
	return 0;
}
int main(){
	while(scanf("%d%d",&n,&b)&&n!=-1){
		int l=1,r=0;
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			r=max(r,a[i]);
		}
		while(l<r){
			int mid=l+r>>1;
			if(ch(mid)) l=mid+1;
			else r=mid;
		}
		printf("%d\n",r);
	}
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值