Description
There are N Customers waiting for a service at the same time. The service time required by customer I is tI, 1 ≤ I ≤ n. A total of S can provide this service. How should we arrange the service order of N customers to minimize the average waiting time? The average waiting time is the total waiting time for N Customers divided by N.
Calculate the optimal service order for the given service time and S value required by N Customers.
Input
The first line of the input data has two positive integers n (n ≤ 10000) and S (S ≤ 1000), indicating that there are N Customers and S can provide services required by customers. There are n positive integers in the next line, indicating the service time required by N Customers.
Output
The output data has only one INTEGER (the calculation result is rounded to the nearest integer), indicating the minimum average wait time.
Sample Input
10 2 56 12 1 99 1000 234 33 55 99 812
Sample output
336
Greedy policy: the shortest service time is preferred.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
int i,n,j,k,minx;
int s;
double t;
int a[10005],b[1005];
while(cin>>n>>s)
{
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
memset(b,0,sizeof(b));
for(i=0;i<n;i++)
{
minx=0x7fffffff;
k=0;
for(j=0;j<s;j++)
{
if(minx>b[j])
{
minx=b[j];
k=j;
}
}
b[k]+=a[i];
a[i]=b[k];
}
t=0;
for(i=0;i<n;i++)
t+=a[i];
t/=n;
printf("%d\n",(int)(t+0.5));
}
return 0;
}