NYOJ -804 Gift (二分)
来源:互联网 发布:网络管理吧 编辑:程序博客网 时间:2024/06/07 19:25
Gift
描述
HEIHEI was planning to send his friends some necklaces as gifts. To show sincerity, he decided to make the necklaces all by himself. He bought some kinds of pearls and each kind of pearls has a different color from others. He wanted to make each necklace consisted of M pearls from different kinds, that’s what he called M-perfect necklace.HEIHEI wanted to know the maximum number of necklaces he could send out to his friends. The number of pearls of each kind and M are given,and now you are asked tell HEIHEI how many M-perfect necklaces he could make at most。
The second line contains n positive numbers represent for the number of pearls of each kind which will not exceed 2000.
The third line contains a positive number(1<=m<=100).
The end of input is indicated by a line containing n=0.
53 3 3 3 3561 2 3 4 5 650
3 3题意:给定n种不同颜色的珠子,每种珠子的数目不超过2000,问最多可以组成多少条M完美项链。一条M完美项链是由M颗不同颜色的珠子组成的。
解题思路:如果直接求解最优性是比较困难的。但是,如果考虑本题更弱一点的形式,则会比较简单:给定n种珠子及其数目,问是否能够组成x条M完美链。显然每种珠子最多只能用x次,而只要每种珠子可用的数目的总和大于或等于xXM,一定可以构造出x条M完美链。因此,本题可以结合二分上界和判断可行性来进行求解。二分范围的下界可以设为0,上界则可设为所有珠子数目总和除以M的商。
#include <stdio.h>int main(){int n, i, a[1002], m;while(~scanf("%d", &n) && n){for(i = 0; i < n; i++)scanf("%d", &a[i]);scanf("%d", &m);if(n<m){printf("0\n");continue;}int low = 0, high = 2000000 /* n*max(a[i])/m */, mid, ans;while(low <= high) {mid = (low + high) / 2; //二分项链条数int sum = 0; //记录可用珠子的数目for(i = 0; i < n; i++){if(a[i] > mid) //珠子用不完sum += mid;elsesum += a[i];}if(sum >= mid * m) //可用珠子数可以组成m条项链{low = mid + 1;ans = mid; //记录条数}elsehigh = mid - 1;}printf("%d\n",ans);}return 0;}
- NYOJ -804 Gift (二分)
- LightOJ - 1222 Gift Packing(带权二分图)
- NYOJ - 找球号(一)(二分)
- nyoj 疯牛(二分+贪心)
- NYOJ练习题 删除元素(二分查找)
- NYOJ 776 删除元素(二分查找)
- nyoj 疯牛(二分搜索+贪心)
- nyoj-680(摘枇杷) 贪心 + 二分
- nyoj 586 疯牛(二分+贪心)
- nyoj 86 找球号(一)【二分查找】
- nyoj--586--疯牛(二分&&枚举)
- nyoj 86 找球号(一)<水,二分>
- NYOJ 还是01背包(枚举+二分)
- NYOJ 586 疯牛(贪心+二分)
- NYOJ 914 Yougth最大化(贪心+二分)
- NYOJ 586 疯牛 (二分搜索答案)
- NYOJ 2354-分班级(二分)
- nyoj 2354 分班级(经典二分)
- OPNET的Process Model中statistic 如何与变量相关联
- 20131002组队赛-Regionals 2011, North America - Rocky Mountain
- 在unity3d里,简易又快速的让物体进入Screen
- hibernate配置复合主键
- 【leetcode】Validate Binary Search Tree
- NYOJ -804 Gift (二分)
- 最短路(SPFA+负权回路的判断)-poj3268
- vmware下ubuntu利用虚拟光驱安装vmware tools需注意
- Python 入门教程 14 ---- Practice Makes Perfect
- iOS之同步请求、异步请求、GET请求、POST请求
- android手机速度慢
- 获得数据库的元数据与参数的元数据以及应用
- cf 163e e-Government
- 启动盘启动键