hdu 4190 Distributing Ballot Boxes(贪心+二分查找)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4190

Distributing Ballot Boxes

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1065    Accepted Submission(s):
536

Problem Description

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个投票箱,接下去的是每个城市的人口数,最后输出每个投票箱的容量是多少。

题目思路:对投票数进行二分,然后对箱子数进行匹配,人口数除以箱子的容量就是城市分配的箱子数。

详见代码。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4
 5 using namespace std;
 6
 7 int main ()
 8 {
 9     int a[500001];
10     int n,b,ans,Max;
11     while (~scanf("%d%d",&n,&b))
12     {
13         if (n==-1&&b==-1)
14             break;
15         Max=0;
16         for (int i=0;i<n;i++)
17         {
18             scanf("%d",&a[i]);
19             if (Max<a[i])
20                 Max=a[i];
21         }
22         int l=0;
23         int r=Max;
24         int sum,mid;
25         while (r>=l)
26         {
27             sum=0;
28             mid=(r+l)/2;
29             for (int i=0;i<n;i++)
30             {
31                 sum+=ceil(a[i]*1.0/mid);
32             }
33             if (sum>b)
34                 l=mid+1;
35             else
36                 r=mid-1,ans=mid;
37         }
38         printf ("%d\n",ans);
39     }
40     return 0;
41 }
时间: 2024-08-07 19:37:22

hdu 4190 Distributing Ballot Boxes(贪心+二分查找)的相关文章

HDU 4190 Distributing Ballot Boxes【二分答案】

题意:给出n个城市,n个城市分别的居民,m个盒子,为了让每个人都投上票,问每个盒子应该装多少张票 二分盒子装的票数, 如果mid<=m,说明偏大了,r应该向下逼近 ,r=mid 如果mid>m,说明偏小了,l应该向上逼近,l=mid+1 和上午那个切割木块那个一样----------- 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5

hdu 1969 Pie(贪心+二分查找)(简单)

Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5628    Accepted Submission(s): 2184 Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I

贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

题目传送门 1 /* 2 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 3 当然有可能两个数和超过p,那么an的值最优,每次还要和an比较 4 注意:不能选取两个相同的数 5 反思:比赛时想到了%p和sort,lower_bound,但是还是没有想到这个贪心方法保证得出最大值,还是题目做的少啊:( 6 */ 7 #include <cstdio> 8 #include <algorithm>

hdu 4938 Seeing People 排序+二分查找

Seeing People Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 241    Accepted Submission(s): 61 Problem Description There are two kinds of people. If person i is the first kind of people, it

UVA 11368 &amp; POJ 3636 &amp; HDU 1677 Nested Dolls(贪心 + 二分LIS)

A - Nested Dolls Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Dilworth is the world's most prominent collector of Russian nested dolls: he literally has thousands of them! You know, the wooden hollow dolls

hdu 4715 Difference Between Primes (二分查找)

Problem Description All you know Goldbach conjecture.That is to say, Every even integer greater than 2 can be expressed as the sum of two primes. Today, skywind present a new conjecture: every even integer can be expressed as the difference of two pr

HDU 2141 Can you find it? 二分查找

Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 21485    Accepted Submission(s): 5446 Problem Description Give you three sequences of numbers A, B, C, then we give you a number

HDU 3280 Equal Sum Partitions(二分查找)

Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 551    Accepted Submission(s): 409 Problem Description An equal sum partition of a sequence of numbers is a grouping of the

HDU 5265 pog loves szh II (二分查找)

[题目链接]click here~~ [题目大意]在给定 的数组里选两个数取模p的情况下和最大 [解题思路]: 思路见官方题解吧~~ 弱弱献上代码: Problem : 5265 ( pog loves szh II ) Judge Status : Accepted RunId : 13961817 Language : G++ Author : javaherongwei Code Render Status : Rendered By HDOJ G++ Code Render Versio