Grade
Problem descriptionted is a employee of always cook mushroom (ACM ). his boss Matt gives him a pack of mushrooms and ask him to grade each mushroom according to its weight. suppose the weight of a mushroom is W, then it's grade s is
S = 10000-(100-W) ^ 2
What's more, Ted also has to report the mode of the grade of these mushrooms. the mode is the value that appears most often. mode may not be unique. if not all the value are the same but the frequencies of them are the same, there is no mode.
Inputthe first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow.
The first line of each test cases contains one integers n (1 <= n <= 10 ^ 6), denoting the number of the mushroom.
The second line contains N integers, denoting the weight of each mushroom. The weight is greater than 0, and less than 200.
Outputfor each test case, output 2 lines.
The first line contains "case # X:", where X is the case number (starting from 1)
The second line contains the mode of the grade of the given mushrooms. If there exists multiple modes, output them in ascending order. If there exists no mode, output "bad mushroom ".
Sample Input
36100 100 100 99 98 1016100 100 100 99 99 1016100 100 98 99 99 97
Sample output
Case #1:10000Case #2:Bad MushroomCase #3:9999 10000
Source2014 ACM/ICPC Asia Regional Beijing online
Solution:
There are n mushrooms and the weight is W [I]. According to the formula, the score of the N mushrooms is classified (one score is given), and the score with the most frequent occurrence becomes mode, output this mode is required. Note that the mode may not be unique and the output in ascending order matches the mode score. If each score can be set to mode, the bad mushroom is output.
The above may not be quite clear. There are three cases after scores are given for each mushroom. For example, suppose there are 6 mushrooms:
1. The score is 2 2 2 1 1 6
Where 2 appears most times, mode is unique, and output 2
2. The score is 4 4, 2, 2, and 3.
Among them, 2 and 4 appear most times, mode is not unique, and output 2 4 in ascending order
3. The score is 2 2 3 3 3 2.
Among them, 2 and 3 appear the most times, but there is no other score, that is, each score is a mode, and the output is bad mushroom.
In the third case, pay special attention to several modes. In the above example, there are two types of 2 and 3. If there is only one, for example, if the score is 2, 2, 2, 2, and 2 is to be output, the bad mushroom cannot be output.
Code:
# Include <iostream> # include <stdio. h> # include <map> # include <string. h >#include <cmath> # include <algorithm> using namespace STD; const int maxn = 1000020; int W [maxn]; // weight Map <int, int> MP; // first is the obtained mode, and second is the number of occurrences of this mode int output [maxn]; // output int N; int main () {int t; CIN> T; int c = 1; while (t --) {MP. clear (); scanf ("% d", & N); bool one = 0; // only one mode for (INT I = 1; I <= N; I ++) {scanf ("% d", & W [I]); int temp = 10000-(1 00-w [I]) * (100-w [I]); MP [temp] ++; If (MP [temp] = N) One = 1 ;} int maxn =-1; // The maximum number of times the mode is queried. Map <int, int>: iterator I; for (I = MP. begin (); I! = MP. end (); I ++) {If (I-> second)> maxn) maxn = I-> second;} int Len = 0; I = MP. begin (); bool same = 1; // judge whether the number of times the mode appears is the same int flag = I-> second; for (I = MP. begin (); I! = MP. end (); I ++) {If (I-> second) = maxn) Output [Len ++] = I-> first; if (I-> second )! = Flag) Same = 0;} cout <"case #" <C ++ <":" <Endl; If (same &&! One) // {cout <"Bad mushroom" <Endl; Continue ;}for (INT I = 0; I <len-1; I ++) cout <output [I] <""; cout <output [len-1] <Endl;} return 0 ;}
[ACM] HDU 5038 grade