Acjoy群赛D-Grade题解

#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int N = 10005;
int vis[N], n;
struct Num {
    int value;
    int cnt;
}num[N];

bool cmp(Num a, Num b) {//结构体排序
    if (a.cnt != b.cnt)
        return a.cnt > b.cnt;
    return a.value < b.value;
}

int main() {
    int t, cas = 1;
    scanf("%d", &t);
    while (t--) {
        memset(vis, 0, sizeof(vis));
        scanf("%d", &n);
        int a, k = 0;
        for (int i = 0; i < n; i++) {
            scanf("%d", &a);
            int temp = 10000 - (100 - a) * (100 - a);
            if (!vis[temp]) {
                vis[temp]++;
                num[k].cnt = 0;
                num[k++].value = temp;
            }
            else
                vis[temp]++;
        }
        for (int i = 0; i < k; i++)
            num[i].cnt = vis[num[i].value];//这样可以避免再次从头到尾的查找
        sort(num, num + k, cmp);
        int m = 1;
        for (int i = 0; i < k - 1; i++) {
            if (num[i].cnt == num[i + 1].cnt)
                m++;
            else
                break;
        }
        if (m == k && k != 1) {
            printf("Case #%d:\nBad Mushroom\n", cas++);
            continue;
        }
        printf("Case #%d:\n", cas++);
        printf("%d", num[0].value);
        for (int i = 1; i < m; i++)
            printf(" %d", num[i].value);
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(Acjoy群赛D-Grade题解)