[ACM] hdu 5038 Grade

来源:互联网 发布:查询当前数据库名称 编辑:程序博客网 时间:2024/05/16 13:40

Grade



Problem Description
Ted 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.
 

Input
The 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.
 

Output
For 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
 

Source
2014 ACM/ICPC Asia Regional Beijing Online



解题思路:

有n个蘑菇,重量为w[i],根据公式为这n个蘑菇分等级(给出一个分数),其中出现频率最多的那个分数成为mode,要求输出这个mode, 注意mode 可能不唯一,按升序输出符合是mode的分数。如果打出的分数每个都可以成为mode,那么则输出 Bad Mushroom.

可能上边说的不是很明白,为每个蘑菇打出分数后,有三种情况,举例子说明,假设有6个蘑菇:

1.  打出的分数为   2  2  2   1  1  6

其中2 出现的次数最多,mode唯一,输出 2

2. 打出的分数为   4   4  2   2  1   3

其中2,4出现的次数最多,mode不唯一,升序输出 2  4

3.打出的分数为   2   2   3   3   3   2

其中2,3出现的次数最多,但没有其它的分数了,也就是打出的分数每个都是mode,输出 Bad Mushroom.

其中第三种情况要特别注意一下,还要判断mode出现了几种,上面例子是出现了两种2,3,如果只出现一种,比如打出的分数为 2 2 2 2 2 2,要输出2,不能输出Bad Mushroom,所以要特判一下。

代码:

#include <iostream>#include <stdio.h>#include <map>#include <string.h>#include <cmath>#include <algorithm>using namespace std;const int maxn=1000020;int w[maxn];//重量map<int,int>mp;//first 为求出的mode, second 为该mode出现的次数int output[maxn];//输出int n;int main(){    int t;cin>>t;    int c=1;    while(t--)    {        mp.clear();        scanf("%d",&n);        bool one=0;//只有一种mode        for(int i=1;i<=n;i++)        {            scanf("%d",&w[i]);            int temp=10000-(100-w[i])*(100-w[i]);            mp[temp]++;            if(mp[temp]==n)                one=1;        }        int maxn=-1;//找mode出现最多的次数        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;//判断mode出现的次数是否相同        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)//当出现的mode的次数相同且mode的种类不唯一时        {            cout<<"Bad Mushroom"<<endl;            continue;        }        for(int i=0;i<len-1;i++)            cout<<output[i]<<" ";        cout<<output[len-1]<<endl;    }    return 0;}


2 0