CodeForces 1011B Planning The Expedition

本文介绍了一个关于为火星探险队员分配食物的问题,通过二分查找算法确定探险队伍可以在给定食物条件下持续的最长时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

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;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值