HOJ1941 Supermarket Cashier Problem _ greedy

Source: Internet
Author: User

Supermarket cashier Problem

L recently opened a small supermarket outside the big job. Because the supermarket is inexpensive, and the service attitude is enthusiastic, so just opened a very hot. However, the supermarket currently has only two cash registers, so that recently, customers often complain that the waiting time for payment is too long.

There is no way for the supermarket to add a new checkout at the moment, so I would like to mitigate the problem by other means. Now, suppose there are N customers waiting to be paid. Obviously, the first two customers can get the service right away, and the back will have to wait in line. L want to readjust the two teams so that the average waiting time for these n customers is minimal. Let's say the N customers won't come to new customers until they pay the bill. Please help him to finish the task.

Input

There are multiple sets of data. Each group of data occupies two rows, the first row is a positive integer n (n <= 1000), representing the number of customers waiting to be paid. The second line has n positive integers, representing the time it takes for the N customer to pay (this can be introduced by the number of things the customer buys), in seconds.

Output

For each set of data, please arrange these n customers into these two queues so that the average wait time for these n customers is minimal. A customer's waiting time refers to the time the customer takes from the queue to the start of the payment, not the time of the payment. You only need to output a floating-point number to represent the average waiting time for these customers. The unit is seconds, accurate to 3 digits after the decimal point.

Sample Input

6
16 15 12 14 13 22
Sample Output
13.167

#include <cstdio>
#include <algorithm>

int q[1010], n, ans;

int main () {while
    (~scanf ("%d", &n)) {for
        (int i = 0; i < n; i++)
            scanf ("%d", q + i);
        Std::sort (q, q + N);
        Ans = 0;
        int t = (n + 1)/2;
        for (int i = 0; i < n; i + + 2)
            ans + = (--t) * Q[i];
        t = N-(n+1)/2;
        for (int i = 1; i < n; i + + 2)
             ans + = (--t) * Q[i];
        printf ("%.3lf\n", ans * 1.0/n);
   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.