问题·:

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
5000004 6
120
2680
3400
200-1 -1

Sample Output

100000
1700

大意:

给你城市数目和箱子数,然后是每个城市的投票数,开始进行投票,每个城市最少有一个箱子,问你如何分配才能使投票数目最多的箱子里的票数降到最少(每各城市里的箱子均分票数)。

思路:

采用二分法,逐步求出箱子里最多票数的数目,然后判断箱子数目是否满足条件。

代码:

#define N 5500000
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int k[N];
int n,m;
int main()
{while(scanf("%d%d",&n,&m)&&(n!=-1||m!=-1)){int i,ma=-0x3f3f3f3f;for(i=0; i<n; i++){scanf("%d",&k[i]);if(ma<k[i])ma=k[i];//先找出最大值,然后从最大值开始二分法}if(n==m){printf("%d\n",ma);continue;}int a=1,b=ma;while(a<b)//二分法找最大票数{int mid=(a+b)/2,c,sum=0;for(int i=0; i<n; i++)//计算以mid为为最大票时,箱子数是否符合要求{if(k[i]%mid)sum+=k[i]/mid+1;elsesum+=k[i]/mid;}if(sum>m)//如果需要的箱子是比总箱子数多,说明最大数偏小了a=mid+1;else     //不比总箱子数多,可能最大数目还能减小b=mid;}printf("%d\n",b);}return 0;
}

Distributing Ballot Boxes HDU - 4190相关推荐

  1. HDU 4190 Distributing Ballot Boxes【二分答案】

    题意:给出n个城市,n个城市分别的居民,m个盒子,为了让每个人都投上票,问每个盒子应该装多少张票 二分盒子装的票数, 如果mid<=m,说明偏大了,r应该向下逼近 ,r=mid 如果mid> ...

  2. hdu 4190(二分)

    思路:二分答案每次验证一下是否可行. 代码如下: 1 /************************************************** 2 * Author : xiaohao ...

  3. 二分查找(折半查找)详解

    二分查找详解 1. 二分查找的引入 2. 二分的一些基本知识 1) 定义 2) 特点 3. 二分查找的边界问题 1) 常用模板 2)综合练习 4.二分的应用 1) Flyer 2) Distribut ...

  4. 2017 ACM ICPC Asia Shenyang Regional Contest 题解(10 / 13)【每日亿题2 / 16】

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A.(2017 ICPC shenyang I)Little Boxes B.(2017 ICP ...

  5. [kuangbin]各种各样的题单

    [kuangbin]各种各样的题单 专题1 简单搜索 POJ 1321 POJ 2251 POJ 3278 POJ 3279 POJ 1426 POJ 3126 POJ 3087 POJ 3414 F ...

  6. 马丁 路德 金的演讲词 I have a dream

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 马丁·路 ...

  7. 暑期ACM模拟赛--2017ACM/ICPC亚洲区沈阳站-重现赛 【待补】

    A - BBP Formula HDU - 6217 (数学问题+公式推导+快速幂) F - Heron and His Triangle HDU - 6222  (数学问题+海伦公式+大数) I - ...

  8. 马丁路德金博士的三段演讲录音

    1.Our God Is Marching On! - 25 March 1965 在线收听 Let us therefore continue our triumphant march (Uh hu ...

  9. Tripwire 配置 使用

    关于Tripwire的使用,在网上找到如下两个比较全的文章, 一开始找到这个,按照其所述去做,但是始终不太清楚,最终能建立database,但是在执行 tripwire --check时找不到tw.c ...

最新文章

  1. mysql useradd_useradd失败
  2. 人工智能、大数据的广泛应用,算法推荐如何守好边界
  3. 使用links方式安装eclipse插件
  4. python网络编程(苦肝一夜,近万字)
  5. word2vec介绍
  6. Unity3D学习笔记之九为场景添加细节(二)
  7. 给孩子一束安全的光 明基WiT MindDuo亲子共读灯首发评测
  8. 高等组合学笔记(六): 第二类Stirling数,第一类Stirling数以及生成函数
  9. android修改代码后运行不生效,Android Studio使用Kotlin时,修改代码后运行不生效问题...
  10. es6 babel编译
  11. json格式转换工具包
  12. 记录一次linux信号量sem_t使用bug
  13. 32.768khz晶振应该接多大的电容
  14. CSS3-3D变换 transform-style:preserve-3d
  15. [渝粤教育] 南京师范大学 中国古代文学(一) 参考 资料
  16. 新产品开发中TR1,TR2,TR3..具体指什么?
  17. 申报高新技术企业需要注意的问题
  18. OpenCV 学习(四)之 将图片插入另一张图片中的实现(ROI)
  19. Poly-YOLO keras代码记录
  20. iZotope RX 10 (专业的音频降噪修复软件) win/mac版

热门文章

  1. 20172321 20172333 2017-2018 暑假作业APP
  2. Find命令的7种用法
  3. python零基础到实现UI自动化 (1)
  4. js垃圾回收机制的优化
  5. 【邀请函】相约2022中国(南京)软博会,现场见!
  6. Struts2【开发Action】
  7. Redis的应用详解
  8. 泛型通配符之PECS
  9. html点击a标签弹窗QQ聊天界面
  10. php 分布存储,分布式存储原理是什么?