G - Grade (sort) 2014 ACM/ICPC Asia Regional Beijing Online

来源:互联网 发布:网络在线记账 编辑:程序博客网 时间:2024/05/29 04:36

G - Grade
Time Limit:1500MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u

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

分析:
问题描述:有n个蘑菇重量为wi,每个蘑菇的等级可通过式子s = 10000 - (100 - w)^2计算得到。问si中出现次数最多的si为多少?若有多个且不包括所有的si,则从小到大输出si,否则输出“Bad Mushroom”.

方法:首先sort一遍所有的si,使si从小到达排列,然后遍历一遍,记录每个不同的si和它出现的次数。然后在sort一遍(出现次数多的排在前面,一样的话si小的排在前面)。

AC代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#include <bitset>#define maxn 1005#define MAXN 1000010#define INF 0x3f3f3f3f#define mod 1000000007#define eps 1e-6const double pi=acos(-1.0);typedef long long ll;using namespace std;struct node{    int num,v;};node s[MAXN];int w[MAXN];int cmp(node a,node b){    return a.num>b.num || (a.num==b.num && a.v<b.v);}int main(){    int t,n,x,cas=1;    scanf("%d",&t);    while(t--)    {        printf("Case #%d:\n",cas++);        scanf("%d",&n);        for(int i=0;i<n;i++)        {            scanf("%d",&x);            w[i]=(10000-(100-x)*(100-x));        }        sort(w,w+n);        int num=1,q=0;        node t;        for(int i=0;i<n;i++)        {            if(i==n-1)            {                t.num=num;t.v=w[i];                s[q++]=t;                break;            }            if(w[i+1]==w[i])            {                num++;            }            else            {                t.num=num;t.v=w[i];                s[q++]=t;                num=1;            }        }        sort(s,s+q,cmp);//        for(int i=0;i<q;i++)//            cout<<s[i].num<<" "<<s[i].v<<endl;        if(q>1 && s[0].num==s[q-1].num) printf("Bad Mushroom\n");        else        {            printf("%d",s[0].v);            for(int i=1;i<q;i++)            {                if(s[i].num==s[0].num) printf(" %d",s[i].v);                else break;            }            printf("\n");        }    }    return 0;}
1 0
原创粉丝点击