zoukankan      html  css  js  c++  java
  • CF B. Planning The Expedition

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

    分析:因为题目给出的天数范围比较小所以我们可以从1到100天开始枚举,我们判断如果是I天了,这些食物够不够N个人吃,够的话继续,不够的话可以跳出循环了,因为这是一种单调关系,如果天数比较大,那可以二分天数;

    枚举

    #include<stdio.h>
    int mp[101],a[101];
    bool book[101];
    int main( )
    {
        int n,m,cnt=0,x;
        scanf("%d%d",&n,&m);
        for(int i=0 ; i<m ; i++)
        {
            scanf("%d",&x);
            mp[x]++;
            if(book[x]==0)
            {
                book[x]=1;
                a[cnt++]=x;
            }
        }
    
       int ans=0;
        for(int i=1 ; i<=m ; i++)
        {
            int num=0;
            for(int j=0 ; j<cnt ; j++)
            {
                num+=(mp[a[j]]/i);
            }
    
            if(num>=n)
            ans=i;
            else
            break;
        }
        printf("%d
    ",ans);
        return 0;
    }
    View Code

    二分

    #include<stdio.h>
    int mp[101],a[101];
    bool book[101];
    int cnt=0,n;
    bool bl(int mid)
    {
        int num=0;
          for(int j=0 ; j<cnt ; j++)
            {
                num+=(mp[a[j]]/mid);///每种食物平均I天之和
                if(num>=n)
                return 1;
            }
           return 0;
    
    }
    int main( )
    {
        int m,x;
        scanf("%d%d",&n,&m);
        for(int i=0 ; i<m ; i++)
        {
            scanf("%d",&x);
            mp[x]++;
            if(book[x]==0)
            {
                book[x]=1;
                a[cnt++]=x;
            }
        }
       int st=0,en=0x3f3f3f3f;
       int ans=0;
       while(en-st>1)
       {
           int mid=(st+en)/2;
           if(bl(mid))
            st=mid;
           else
            en=mid;
       }
    
        printf("%d
    ",(st+en)/2);
        return 0;
    }
    View Code
  • 相关阅读:
    IfcBuildingStorey
    IfcBuilding
    IfcSpatialStructureElement (空间结构元素)
    IfcSpatialElement
    IfcProduct
    IfcPropertyDefinition
    IfcObject
    IfcObjectDefinition
    IfcRoot
    IfcTaskTime
  • 原文地址:https://www.cnblogs.com/shuaihui520/p/9395819.html
Copyright © 2011-2022 走看看