1011B. Planning The Expedition - Codeforces Solution C++

  Problem Link : 1011B. Planning The Expedition 


✅ C++ Solution :

 
#include <bits/stdc++.h>
using namespace std;
bool f(int days,vector<int>& v,int n)
{
	int cnt=0;
	for(int i=0;i<v.size();i++)
	{
		cnt+=v[i]/days;
	//	cout<<cnt<<" ";
		if(cnt>=n)
			return true;
	}
	return false;
}
int main() {
	int n,m;
	cin>>n>>m;
//	cout<<n<<endl;
	int arr[m+1];
	vector<int>v;
	int i;
	for(i=0;i<m;i++)
		cin>>arr[i];
	sort(arr,arr+m);
/*	for(i=0;i<m;i++)
		cout<<arr[i]<<" ";*/
	i=0;
	int x=arr[0];
	int cnt=0;
	//	cout<<"n:"<<n<<endl;
	while(i<m)
	{
		while(x==arr[i])
		{
			cnt++;
			i++;
		}
		v.push_back(cnt);
		cnt=0;
		x=arr[i];
	}

	sort(v.begin(),v.end(),greater<int>());
/*	for(i=0;i<v.size();i++)
		cout<<v[i]<<" ";*/
	int low=0,high=1000;
	int mid;
	while(low<high)
	{
		mid=low+(high-low+1)/2;
		if(f(mid,v,n))
			low=mid;
		else
			high=mid-1;
	}
        if(high==51)
            high=50;
	cout<<high<<endl;

	
		
		
	return 0;
}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!