Problem Description
Windbreaker was planning to send his friends some necklaces as New Year 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. Windbreaker 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 to tell Windbreaker how many M-perfect necklaces he could make at most.
Input
You will be given a number of cases in the input. Each case starts with a positive integer n(n<=1000),which is the number of different kinds;
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 M(1<=M<=100).
The end of input is indicated by a line containing n=0.
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 M(1<=M<=100).
The end of input is indicated by a line containing n=0.
Output
For each test case, output a line containing the Maximum number of M-perfect necklaces.
Sample Input
5 3 3 3 3 3 5 6 1 2 3 4 5 6 5 0
Sample Output
3 3
/*解题报告:所有的种类的珍珠总数为sum,项链的范围[1,sum];用二分查找,得到最大项链数。
把所有种类能够构成项链的珍珠加起来,如果比mid*m种大,则符合条件,在从符合条件中
得到最大值。(初始值low=1, high=sum, mid=(low+high)/2);
*/
//标程:
#include<iostream> #include<cstdio> using namespace std; int n,m; int a[1010]; int judge(int x) { int temp = 0; for(int i = 0; i < n; i ++) { if(x > a[i]) temp += a[i]; else temp += x; } if(temp >= x*m) return 1; else return 0; } int main() { // freopen("a.txt","r",stdin); while(cin >> n, n) { int i, sum = 0; for(i = 0; i < n; i ++) { cin >> a[i]; sum += a[i]; } cin >> m; int low = 1, ans = 0; while(low <= sum) { int mid = (low+sum)/2; if(judge(mid)) { ans = mid; low = mid + 1; } else sum = mid - 1; } cout << ans << endl; } return 0; }