程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 5038 Grade(簡單模擬求解)2014 ACM/ICPC Asia Regional 北京 Online

hdu 5038 Grade(簡單模擬求解)2014 ACM/ICPC Asia Regional 北京 Online

編輯:C++入門知識

hdu 5038 Grade(簡單模擬求解)2014 ACM/ICPC Asia Regional 北京 Online


Grade

Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)


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

開始做這道題時一直在糾結題意,後來看了管理員的回復才終於明白了題意。 題意:用給出的公式求出每個蘑菇的grade,求出現次數最多的grade。如果有多個grade出現的次數一樣多,且還有其他的grade,則把這些出現次數最多的grade按升序輸出;否則,輸出“Bad Mushroom”。
題目並不難,比賽時我用容器寫的,浪費了很多內存,代碼如下:
#include
#include
#include
#include
#include
#include
#include
using namespace std;

vector  g[10005];

int fun(int x) {
    return 10000 - (100 - x) * (100 - x);
}

int main()
{
    int n, w, T, cas = 0;
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        set  s;
        memset(g, 0, sizeof(g));
        for(int i = 0; i < n; i++) {
            scanf("%d", &w);
            int grade = fun(w);
            s.insert(grade);
            g[grade].push_back(w);
        }
        set  ::iterator it;
        vector  v;
        for(it = s.begin(); it != s.end(); it++) {
            v.push_back(g[*it].size());
        }
        sort(v.begin(), v.end());
        int ssize = v.size();
        int cnt = 0;
        for(int i = ssize - 1; i >= 0; i--) {
            if(v[i] == v[ssize-1])
                cnt++;
        }
        printf("Case #%d:\n", ++cas);
        if(cnt == 1) {
            for(it = s.begin(); it != s.end(); it++)
            if(g[*it].size() == v[ssize-1]) {
                printf("%d\n", *it);
                break;
            }
        }

        else {
            if(cnt == ssize)
                printf("Bad Mushroom\n");
            else {
                vector  ans;
                for(it = s.begin(); it != s.end(); it++) {
                    int tmp = *it;
                    if(g[tmp].size() == v[ssize-1]) {
                        ans.push_back(tmp);
                    }
                }
                sort(ans.begin(), ans.end());
                int z = ans.size();
                for(int i = 0; i < z - 1; i++)
                    printf("%d ", ans[i]);
                printf("%d\n", ans[z-1]);
            }
        }
    }
    return 0;
}

比賽結束後想了想,因為grade不會超過10000,直接用數組記錄出現了哪些grade以及每個grade出現的次數,然後按照題目要求輸出即可。
#include
#include

const int N = 1e4 + 5;
int cnt[N];

int main() {
    int T, n, w, cas = 0;
    scanf("%d",&T);
    while(T--) {
        scanf("%d", &n);
        int S_cnt = 0; //有幾個不同的s
        int Max_cnt = 0; //出現次數最多的數出現的次數
        int Max_s = 0, Min_s = 10005; //s的最大最小值
        memset(cnt, 0, sizeof(cnt));
        for(int i = 0; i < n; i++) {
            scanf("%d",&w);
            int s = 10000 - (100 - w) * (100 - w);
            if(s > Max_s) Max_s = s;
            if(s < Min_s) Min_s = s;
            if(!cnt[s]) S_cnt++;
            cnt[s]++;
            if(cnt[s] > Max_cnt) Max_cnt = cnt[s];
        }
        printf("Case #%d:\n", ++cas);
        if(Max_cnt * S_cnt == n && S_cnt > 1)
            printf("Bad Mushroom\n");
        else {
            int p = 0;
            for(int i = Min_s; i <= Max_s; i++) {
                if(cnt[i] == Max_cnt) {
                    if(p) printf(" ");
                    printf("%d", i);
                    p = 1;
                }
            }
            printf("\n");
        }
    }
    return 0;
}




  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved