B. Planning The Expedition Codeforces Round #499 (Div. 2)

B. Planning The Expedition

题目链接

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Natasha is planning an expedition to Mars for nn people. One of the important tasks is to provide food for each participant.

The warehouse has mm daily food packages. Each package has some food type aiai.

Each participant must eat exactly one food package each day. Due to extreme loads, each participant must eat the same food type throughout the expedition. Different participants may eat different (or the same) types of food.

Formally, for each participant jj Natasha should select his food type bjbj and each day jj-th participant will eat one food package of type bjbj. The values bjbj for different participants may be different.

What is the maximum possible number of days the expedition can last, following the requirements above?

Input

The first line contains two integers nn and mm (1≤n≤1001≤n≤100, 1≤m≤1001≤m≤100) — the number of the expedition participants and the number of the daily food packages available.

The second line contains sequence of integers a1,a2,…,ama1,a2,…,am (1≤ai≤1001≤ai≤100), where aiai is the type of ii-th food package.

Output

Print the single integer — the number of days the expedition can last. If it is not possible to plan the expedition for even one day, print 0.

题意:有n个人和m个食物,给出每一个食物的种类,每个人只会吃一种食物,每个人一天吃一个食物,问这n个人可以撑多少天。

思路:因为最多只有100天,从100到0进行枚举每一种可能天数是否可以满足n个人每一天的食欲即可。如果ai非常大的话,可以用二分枚举的方法计算出最大的天数。

#include<bits/stdc++.h>
using namespace std;
const int maxn = int(1e5) + 100;
const int maxm=100;
int num[maxn];
int tmp[maxn];
bool solve(int x,int n,int m) {
	if(x==0)
		return true;
	for(int i=1; i<=maxm; i++)
		tmp[i]=num[i];
	int pos=1;
	int top=0;
	while(pos<=maxm) {
		if(tmp[pos]>=x) {
			tmp[pos]-=x;
			top++;
			if(top>=n)break;
		} else {
			pos++;
		}
	}
	if(top>=n)
		return true;
	else
		return false;
}
int main() {
	int n,m;
	int u;
	while(~scanf("%d %d",&n,&m)) {
		memset(num,0,sizeof(num));
		for(int i=1; i<=m; i++){
			scanf("%d",&u);
			num[u]++;
		}
		for(int i=100; i>=0; i--) {
			if(solve(i,n,m)) {
				printf("%d\n",i);
				break;
			}
		}
	}
	return 0;
}

二分枚举的方法

#include<bits/stdc++.h>
using namespace std;
const int maxn = int(1e5) + 100;
const int maxm=100;
int num[maxn];
int tmp[maxn];
bool solve(int x,int n,int m) {
	if(x==0)
		return true;
	for(int i=1; i<=maxm; i++)
		tmp[i]=num[i];
	int pos=1;
	int top=0;
	while(pos<=maxm) {
		if(tmp[pos]>=x) {
			tmp[pos]-=x;
			top++;
			if(top>=n)break;
		} else {
			pos++;
		}
	}
	if(top>=n)
		return true;
	else
		return false;
}
int main() {
	int n,m;
	int u;
	while(~scanf("%d %d",&n,&m)) {
		memset(num,0,sizeof(num));
		for(int i=1; i<=m; i++) {
			scanf("%d",&u);
			num[u]++;
		}
		int l=0,r=100;
		while(l<r){
			int mid=(l+r+1)>>1;
			if(solve(mid,n,m))
				l=mid;
			else
				r=mid-1;
		}
		printf("%d\n",l);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40160605/article/details/81255803