B - Distributing Ballot Boxes

题目:

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 a i,(1<=a i<=5,000,000), indicating the population of the i th 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个城市,M个投票箱。

然后是N行,表示每个城市的人口数。

现在每个城市所有的人要投票,投票箱的大小可以无限大(投票箱全部相同,大小相等),我们现在要求的是最小的投票箱容纳量。

思路:

如果N == M,则容量肯定为城市人口数最多的那个。

如果N<M,那么我们肯定要把所有的箱子都用上,这样最后箱子的最小容量才会是最小的,这样我们可以用二分法来判断箱子的最小容量。

代码如下:

#include<stdio.h>
#include<string.h>

int a[500000];
int main()
{
    int c,b;
    while(scanf("%d%d",&c,&b)!=EOF)
    {
        int i,max=0;
        if(c==-1||b==-1)
            break;
        for(i=0; i<c; i++)
        {
            scanf("%d",&a[i]);
            if(a[i]>max)
                max=a[i];//存储最大的城市人数;
        }
        if(c==b)//箱子和城市个数相同,那么箱子的容量就应该是最大的人数;
        {
            printf("%d\n",max);
            continue;
        }
        int l,r,mid;
        l=1;
        r=max;
        while(l<r)
        {
            int flag=0;
            int sum=0;
            mid=(l+r)/2;//这是箱子容量的中间值;
            for(i=0; i<c; i++)
            {   
                //假设箱子的容量是mid;
                int tt;
                tt=a[i]%mid;
                if(tt==0)
                    sum+=a[i]/mid;
                else
                    sum+=a[i]/mid+1;
                if(sum>b)   //如果箱子的容量是mid,但是所需要的箱子的个数小于原来的,这就意味着箱子的容量还可以增加;
                    flag=1;
            }
            if(flag==1)//如果所需要的箱子数大于原来的数量,那么,箱子的数量应该小于mid;
                l=mid+1;
            else//如果所需要的箱子数小于原来的数量,那么,箱子的数量应该大于mid;
                r=mid;
        }
        printf("%d\n",r);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/titi2018815/article/details/81157889
今日推荐