Employees of JebTrains are on their way to celebrate the 256-th day of the year! There are n employees and k teams in JebTrains. Each employee is a member of some (exactly one) team. All teams are numbered from 1 to k. You are given an array of numbers t1,t2,…,tn where ti is the i-th employee’s team number.
JebTrains is going to rent a single bus to get employees to the feast. The bus will take one or more rides. A bus can pick up an entire team or two entire teams. If three or more teams take a ride together they may start a new project which is considered unacceptable. It’s prohibited to split a team, so all members of a team should take the same ride.
It is possible to rent a bus of any capacity s. Such a bus can take up to s people on a single ride. The total cost of the rent is equal to s⋅r burles where r is the number of rides. Note that it’s impossible to rent two or more buses.
Help JebTrains to calculate the minimum cost of the rent, required to get all employees to the feast, fulfilling all the conditions above.
Input
The first line contains two integers n and k (1≤n≤5⋅105,1≤k≤8000) — the number of employees and the number of teams in JebTrains. The second line contains a sequence of integers t1,t2,…,tn, where ti (1≤ti≤k) is the i-th employee’s team number. Every team contains at least one employee.
Output
Print the minimum cost of the rent.
题意:有N个员工,K个小组,每个员工都属于一个小组,现在坐车去一个地方,车只能租一辆,但是可以租任意容量的车,车上最多只能有两个小组的人,每个小组不能拆开,求车容量乘以车往返次数的最小值。
a【i】表示第i小组的人数,a从小到大排列,a【k】是车的最小容量,遍历1到k,求最大的a[i]+a[k-i+1],车的最大容量。遍历车的容量,往里面分配小组。
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
long long a[500010];
int main ()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
a[x]++;
}
if(k==1)
{
cout<<a[1]<<endl;
return 0;
}
sort(a+1,a+k+1);
long long maxx=0;
for(int i=1;i<=k;i++)
{
maxx=max(maxx,a[i]+a[k-i+1]);
}
long long ans=1e16;
for(long long i=a[k];i<=maxx;i++)
{
long long l=1,r=k,cnt=0;
while(l<r)
{
cnt++;
if(a[l]+a[r]<=i)
{
l++;
r--;
}
else
{
r--;
}
}
if(l==r)
{
cnt++;
}
ans=min(ans,i*cnt);
}
cout<<ans<<endl;
}