首页 > 代码库 > [ACM] hdu 5038 Grade
[ACM] hdu 5038 Grade
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.
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.
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”.
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
3 6 100 100 100 99 98 101 6 100 100 100 99 99 101 6 100 100 98 99 99 97
Sample Output
Case #1: 10000 Case #2: Bad Mushroom Case #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; }
[ACM] hdu 5038 Grade
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。