50073. Wealth Histogram

I'm a slow walker, but I never walk backwards.

Task Description

Read a set of $N$ records from a binary file and make a histogram. The name of the file will be given as a command line argument. Each record has a name and an integer $W$ indicating his/her wealth. Now divide these records into a set of B buckets, where the first bucket has the records of wealth from $1$ to $999$, and the second bucket has the wealth from $1000$ to $1999$, and so on. Now output the number of people in non-empty buckets in increasing bucket index order. Note that you cannot declare an array to store the number of people in buckets because the distribution of wealth might be very uneven and most of the buckets may not have records.

Also you need to report the following statistics. First, report the poorest person in the file. If there are multiple of them, report the one with his/her name in the earliest dictionary order. Similarly report the wealthiest person in the file. If there are multiple of them, report the one with his/her name in earliest dictionary order. Finally report the median of the wealth. The median $m$ of a sequence of $N$ wealth (where $N$ is an odd number) is that both the numbers of wealth greater/smaller than m are the same. If $N$ is even, then the medium is the average (Integer Division) of the middle two wealth in sorted order.

Hint

An easy way is to sort the numbers so that you can scan through them in increasing order.
Note that you need to use long long int to prevent overflow when the medium is the average of the middle two wealth.

Input Format

1
./a.out [file_name]

The name of the binary file is given as a command line argument (You can reference the textbook chapter 21) , The file contains a set of $N$ records, you need to read the records till EOF.
Each record contains a 12-byte string ('\0' included), and a 4-byte integer.
0 $\lt $ N $\leqslant$ $1024. $
Length of a person's name $= 12. $ (null terminator '\0' included)
The name of a person are all lowercase letters without whitespace.
0 $\lt $ W $\leqslant$ $2,147,483,647. $

Output Format

Print the number of people in non-empty buckets in increasing bucket index order, the poorest person's name, the wealthiest person's name, and the median of the wealth on separate lines.

Sample Input

Comand Line Argument (download p10163-0.bin)

./a.out p10163-0.bin

The content of the binary file p10163-0.bin

laurawaneda 9584178
brookholden 276087
jamescorwin 91536
tonywinslow 7582
louisdarell 115862
kerstentami 7151
callistalex 605497
brettmarion 712997
marcibrande 91412
skylardella 605965
aurakendall 91786
lavernjenae 9584178
lycakrysten 340673
emiyamickey 605256
keliaraylen 7151

Sample Output

3
3
1
1
1
3
1
2
keliaraylen
laurawaneda
276087

Discussion