CodeForces 1011B Planning The Expedition

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.

扫描二维码关注公众号,回复: 3327393 查看本文章

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.

Examples

input

4 10
1 5 2 1 1 1 2 5 7 2

output

2

input

100 1
1

output

0

input

2 5
5 4 3 2 1

output

1

input

3 9
42 42 42 42 42 42 42 42 42

output

3

有每个数,每个数代表一种食物,该食物每出现一次,代表有一单位的该类食物,

现有n个人,每人选一种食物,之后不可更改。每人消耗所选种类食物一个单位,问这些食物最多能让n个人吃几天

看数据量1~100,可以对天数暴力,

也可以用二分查找,找到满足条件的最大值

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define PI acos(-1)
#define INF 0x3f3f3f3f
#define N 200000 + 10
using namespace std;
const int intN = 3e5 + 10 ;
typedef long long int LL;
const int dir[4][2]= { {1,0},{0,1},{-1,0},{0,-1} };

int GCD(int a,int b)
{
    return b ? GCD(b,a%b) : a;
}

int n,m,flag,g;
int food[N];
int OK(int day)
{
    int i,cnt=0;
    for(i=0;i<n;i++)
    {
        cnt+=food[i]/day;
    }
    if(cnt>=m)
    {
        flag=1;
        return 1;
    }
    return 0;
}

int main()
{
    map<int,int>mp;
    map<int,int>::iterator it;
    int i,t;
    scanf("%d%d",&m,&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&t);
        mp[t]++;
    }
    for(it=mp.begin();it!=mp.end();it++)
    {
        food[g++]=it->second;
    }
    int left=1,right=100,mid;
    while(left <= right)
    {
        mid=(right+left)/2;
        if(OK(mid))
        {
            left=mid+1;
        }
        else
        {
            right=mid-1;
        }
    }
    if(!flag)
        printf("0\n");
    else
        printf("%d\n",right);
    return 0;
}

整理:

二分查找的模板:
 

// 这里必须是 <=
while (left <= right) {
    int mid = (left + right) / 2;
    if (满足条件) {
        //... right = mid - 1;
    }
    else {
        // ... left = mid + 1;
    }
}
return xxx;

猜你喜欢

转载自blog.csdn.net/codertcm/article/details/81435654
今日推荐