Codeforces Round #499 (Div. 2)(1011)
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?
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.
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.
4 10
1 5 2 1 1 1 2 5 7 2
2
100 1
1
0
2 5
5 4 3 2 1
1
3 9
42 42 42 42 42 42 42 42 42
3
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 22 to 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.
题意:有n个人去火星,有m斤食物,每个人只能吃一种食物,而且每天必须吃一斤食物,问n个人最多能活几天。
题解:由于数据量比较小,完全可以用暴力解决,一年一年往上加,不满足条件则找到最优解。
1 #include<stdio.h> 2 #include<string.h> 3 #include<stack> 4 #include<string.h> 5 #include<queue> 6 #include<algorithm> 7 #include<iostream> 8 #include<map> 9 #include<vector> 10 #define PI acos(-1.0) 11 using namespace std; 12 typedef long long ll; 13 const int MAXN=3e5+10; 14 map<int,int>::iterator it; 15 bool cmp(int a,int b) 16 { 17 return a>b; 18 } 19 int main() 20 { 21 map<int,int >mp; 22 int i,siz,n,rt,m,g=0,flag=0,ck=0; 23 int ans[MAXN]= {0}; 24 int t[MAXN]= {0}; 25 int s[MAXN]= {0}; 26 scanf("%d%d",&n,&m); 27 for(i=0; i<m; i++) 28 { 29 scanf("%d",&rt); 30 mp[rt]++; 31 } 32 int r=n-mp.size(); 33 for(it=mp.begin(); it!=mp.end(); it++) 34 { 35 s[g++]=it->second; 36 } 37 sort(s,s+g,cmp); 38 for(i=0; i<g; i++) 39 { 40 if(i==n) 41 break; 42 ans[ck++]=s[i]; 43 } 44 for(; ck<n; ck++) 45 ans[ck]=0; 46 47 for(i=0; i<n; i++) 48 t[i]=ans[i]; 49 for(i=1; i<=100; i++) 50 { 51 for(int j=0; j<n; j++) 52 { 53 if(ans[j]-i<0) 54 { 55 int k,cur=ans[j]-i; 56 for(k=0; k<n; k++) 57 { 58 int kk=ans[k]-i; 59 if(kk>=i) 60 { 61 ans[k]-=i; 62 ans[j]=0; 63 break; 64 } 65 } 66 if(k==n) 67 { 68 flag=1; 69 break; 70 } 71 } 72 73 } 74 if(flag==1) 75 break; 76 for(int i=0; i<n; i++) 77 ans[i]=t[i]; 78 79 } 80 81 printf("%d\n",i-1); 82 83 84 return 0; 85 }
当然这道题也可以二分查找
1 #include<stdio.h> 2 #include<string.h> 3 #include<stack> 4 #include<string.h> 5 #include<queue> 6 #include<algorithm> 7 #include<iostream> 8 #include<map> 9 #include<vector> 10 #define PI acos(-1.0) 11 using namespace std; 12 typedef long long ll; 13 const int MAXN=3e5+10; 14 int m,n,l,flag; 15 int str[MAXN]; 16 map<int,int>::iterator it; 17 int solve(int mid) 18 { 19 int ans=0; 20 for(int i=0;i<l;i++) 21 { 22 ans+=str[i]/mid; 23 } 24 if(ans>=m) 25 flag=1; 26 if(ans>=m) 27 return 1; 28 return 0; 29 } 30 int main() 31 { 32 cin>>m>>n; 33 int k; 34 map<int,int>mp; 35 for(int i=0;i<n;i++) 36 { 37 cin>>k; 38 mp[k]++; 39 } 40 for(it=mp.begin();it!=mp.end();it++) 41 { 42 str[l++]=it->second; 43 } 44 int left=1,right=100,mid; 45 while(left<=right) 46 { 47 mid=(left+right)/2; 48 if(solve(mid)) 49 { 50 left=mid+1; 51 } 52 else 53 { 54 right=mid-1; 55 } 56 } 57 if(!flag) 58 right=0; 59 cout<<right<<endl; 60 return 0; 61 }
本文来自博客园,作者:左手边五十米,转载请注明原文链接:https://www.cnblogs.com/moomcake/p/9425419.html