B. Planning The Expedition

http://codeforces.com/contest/1011/problem/B

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

Note

In the first example, Natasha can assign type 11 food to the first participant, the same type 11 to the second, type 55 to the third and type 22to the fourth. In this case, the expedition can last for 22 days, since each participant can get two food packages of his food type (there will be used 44 packages of type 11, two packages of type 22 and two packages of type 55).

In the second example, there are 100100 participants and only 11 food package. In this case, the expedition can't last even 11 day.

my idea:

将不同的食物类型存入数组a中,然后将这个 array进行排序,然后算出假如m种食物n个人吃最多能吃几天(不考虑每人只能吃一种类型),最大天数为x,接下来就是将x带入看看是否满足条件,不满足x--,然后循环。

这里的条件就是假如天数为x,那么第一个人的食物种类在数组中应该有x次,数组减去第一个人x天所吃的食物, 第二个人的食物种类在数组(排除第一个人x天的食物之外)也应该有x次,直到n个人都满足。

首先让数组初始化,以免受到影响,这里找数组中某个数出现了几次我是判断a[ 1 ]==a[ n + 1-1 ];来进行的

我的代码(AC):

#include <iostream>
#include <stdio.h>
#include <algorithm>

using namespace std;

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int a[200]={0};
    for(int i=0;i<m;i++)
       scanf("%d",&a[i]);
    sort(a,a+m);
//    for(int i=0;i<m;i++)
//       printf("%d ",a[i]);
//       cout<<endl;

    int x=m/n;
    if(x==0)  printf("0\n");
    else if(x==1) printf("1\n");
    else
    {
        int cp=x;
        int st=0,i=0;
        while(1)
        {
            int c=0;
            st=0;
            while(1)
            {
                if(a[st]==a[st+cp-1]) st=st+cp,c++;
                else st++;
                if(c==n) break;
                if(st>=m) break;
            }
            if(c==n) {printf("%d\n",cp);break;}
            else cp--;
        }
    }

    return 0;
}

大神的idea:

Let cici be the number of food packages that equal to ii. Calculate the array cc.

For any dd we can calculate the maximum number of people kk, who can participate in the expedition for dd days. To do this, we'll go over all the elements of the array cc. Let now be considered cici. If ci≥d, we can decrease cici by dd and increase kby 1, that is, take dd daily food packages for one person. If still ci≥d, repeat the algorithm, and so on. That is for ii-th iteration number k increases by⌊cid⌋. After all the iterations the number kk will be the required number of people.

It is clear that the answer does not exceed mm (every day at least one food package is used).

Let's iterate dd from mm to 11, each time checking whether the answer can be equal dd. To do this, we calculate the maximum number of people kk, who can participate in the expedition for dd days. If k≥n, then the answer is dd. If no answer was received on any iteration, then the answer is 0.

Complexity: O(m2)

Bonus. Try to improve the complexity to O(mlogm).http://codeforces.com/blog/entry/60851

 vector<int> c(N + 1)是创造一个N+1长度的动态数组c,vector<int> c(40,1) 创造一个长度为40,每个元素都为1的动态数组

想要知道一个序列中某个数出现了几次,可以将输入的数据当成数组的下标,让数组元素++,数组元素存的就是该数出现的次数

下列代码是有关vector的演示

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
   vector<int> c(40);
   for(int i=0;i<10;i++)
          c[i]=i;//必须规定c的长度才能用该种赋值方式
//       c.push_back(i);//否则只能用push_back()函数
//    for(int i=0;i<10;i++)
//        c[i]=i+1;
    for(int i=0;i<10;i++)
       cout<<c[i]<<' ';
       cout<<endl;
    vector<int> cc(c); //直接复制了一个c数组
    for(int i=0;i<10;i++)
       cc[i]=i+1;
    for(int i=0;i<10;i++)
       cout<<cc[i]<<' ';
    return 0;
}

 大神代码:

#include <bits/stdc++.h>

using namespace std;

const int N = 100;

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> c(N + 1);
    for (int i = 0; i < m; i++) {
        int a;
        cin >> a;
        c[a]++;
    }
    for (int d = N; d >= 1; d--) {
        vector<int> cc(c);
        int k = 0;
        for (int i = 1; i <= N; i++)
            while (cc[i] >= d) {
                k++;
                cc[i] -= d;
            }
        if (k >= n) {
            cout << d << endl;
            return 0;
        }
    }
    cout << 0 << endl;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值