A New Year Gift

1109: A New Year Gift

描述

题目描述:

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.

输入:

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.

输出:

For each test case, output a line containing the Maximum number of M-perfect necklaces.

样例输入
5
3 3 3 3 3
5
6
1 2 3 4 5 6
5
0
样例输出
3
3
 
        
1

题意:先输入有几种珠子 再输入 每种珠子有多少个 再收入 制作项链需要的颜色数量 让你找出来能够制作的最多条数
先想的模拟+排序   显然超时了   
理论上所有珠子的总和 除以 需要的颜色数 是理论上能够制作最多的项链  但是 有的颜色多  有的颜色 少   所有可以想到二分枚举可能制作的
长度 
 
        
#include<bits/stdc++.h>
using namespace std;
int a[1005];
int main()
{
	int n, m;
	while(cin >> n && n)
	{
		memset(a,0,sizeof(a));
		
		int sum = 0;
		
		for(int i = 0;i < n;i ++)
		{
			cin >> a[i];
			sum += a[i];
		}
		
		cin >> m;   //m种颜色 
		
		int maxn = sum / m;  //理论上最多的条数
		int minn = 0;
		while(minn < maxn)
		{
			int mid = (maxn + minn) / 2;
			sum = 0;
			for(int i = 0;i < n;i ++)
			{
				sum += (a[i] >= mid ? mid : a[i]);
			}
			
			if(sum >= mid * m) 
			minn = mid + 1;
			else
			maxn = mid - 1;
		} 
		sum = 0;
		for(int i = 0;i < n;i ++)
		{
			sum += (a[i] >= minn ? minn : a[i]);
		}
		if(sum >= minn * m) cout << minn << endl;
		else cout << minn - 1 << endl;
	}
	return 0;
} 


猜你喜欢

转载自blog.csdn.net/soul_97/article/details/80210432