2014ACM网络赛北京——1007 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
  
   
   
   
   
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


题意:多来几次哈希


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;

#define INF 0xffffff
#define ll long long int
#define MEM(a) memset(a, 0, sizeof(a))
#define MEMM(a) memset(b, -1, sizeof(b))

#ifdef ONLINE_JUDGE
#define FOI(file) 0
#define FOW(file) 0
#else
#define FOI(file) freopen(file,"r",stdin);
#define FOW(file) freopen(file,"w",stdout);
#endif


int num[10001];
int list[10001];

void init()
{
    for(int i = 0; i <=200; i++)
    {
        num[i] = 10000 - (100-i)*(100-i);
    }
}

struct g
{
    int val;
    int num;
};

int main()
{
    //FOI("input");
    //FOW("output");
    //write your programme here

    int t;
    int n, temp;
    int i;
    init();
    cin >> t;
    int c;
    g gg[10001];
    g ma[10001];
    for(int j = 1; j <= t; j++)
    {
        MEM(list);
        scanf("%d", &n);
        for(i = 0; i < n; i++)
        {
            scanf("%d", &temp);
            list[num[temp]]++;
        }

        c = 0;
        for(i = 0; i <= 10000; i++)
        {
            if(list[i] != 0)
            {
                gg[c].val = i;
                gg[c].num = list[i];
                // cout << i << " " << list[i] << endl;
                // cout << gg[c].val << " " << gg[c].num<< endl;
                c++;
            }
            if(c == n)
                break;
        }

        // for(i = 0; i < c; i++) {
            // cout << gg[i].val << " " << gg[i].num << endl;
        // }
                
        int cc = 0;
        int max = 0;
        for(i = 0; i < c;i ++)
        {
            // num 频率
            if(gg[i].num > max)
            {
                cc = 0;
                ma[cc].val = gg[i].val;
                ma[cc].num = gg[i].num;
                max = gg[i].num;
            }
            else if(gg[i].num == max)
            {
                cc++;
                ma[cc].val = gg[i].val;
                ma[cc].num = gg[i].num;
            }
            else
                continue;
        }

        printf("Case #%d:\n", j);
        // cout << cc << endl;

        if(cc == c-1 && cc != 0)
        {
            puts("Bad Mushroom");
            continue;
        }
        else 
        {
            if(cc == 0)
                printf("%d\n", ma[0].val);
            else
            {
                for(i = 0; i <= cc; i++)
                {
                    if(i != cc )
                        printf("%d ", ma[i].val);
                    else
                        printf("%d\n", ma[i].val);
                }
            }
        }
    }
    return 0;
}


你可能感兴趣的:(2014ACM网络赛北京——1007 Grade (打表+哈希))