Multiple optimal service order problems

Source: Internet
Author: User
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;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.