Rokua trial field P1233 Queue water (sort, greedy)

Source: Internet
Author: User

A water problem ....

Links: https://www.luogu.org/problem/show?pid=1223

Title Description

There are n people in the front row of a faucet to connect water, if everyone to water time for TI, please program to find out the order of this n individual queue, so that the average waiting time for n people minimum.

Input/output format

Input format:

Input file a total of two lines, the first behavior N, the second line represents the 1th person to the nth person each of the water time t1,t2,...,tn, each data has 1 spaces.

Output format:

The output file has two lines, the first behavior is a queueing order, that is, an arrangement of 1 to N; the second behavior is the average wait time under this arrangement scheme (the output is accurate to two digits after the decimal point).

Input and Output Sample input example # #:
10 56 12 1 99 1000 234 33 55 99 812
Sample # # of output:
3 2 7 8 1 4 9 6 10 5291.90
Description

n<=1000

Ti<=1e6, does not guarantee that TI does not repeat

Analysis:

The topic is very water, probably means that a group of people to pick up water, some people want more time, and some people want less time, ask how to queue average waiting time minimum?

Then we know that the total waiting time is the first time to add to the last person, so let the time to play first, the total waiting time is the least.

Code:

1#include <bits/stdc++.h>//C + + Universal header file2 using namespacestd;3 structsb{4     intA;5     intID;6}s[10086]; 7 intCMP (SB a1,sb A2) {8     if(a1.a>a2.a)return 0;9     if(a1.a<a2.a)return 1;Ten}//because the struct is used, you define how to sort One intMain () { A     intN; -     DoubleZ; -Cin>>N; the      for(intI=1; i<=n;i++){ -cin>>s[i].a;//input time for water connection -S[i].id=i;//the number of the person who is currently receiving water -     } +Sort (s+1, s+n+1, CMP); -      for(intI=1; i<=n;i++){ +cout<<s[i].id<<" ";//Order of output water connection Az=z+s[i].a* (n-i);//Calculate Total Time at     } -printf"\n%.2f", z/n);//input Average Time -     return 0; -}

Rokua trial field P1233 Queue water (sort, greedy)

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.