lightoj 1148 - Mad Counting 【一个模拟题 因为看错题目 WA到死。。。】

1148 - Mad Counting
PDF (English) Statistics Forum
Time Limit: 0.5 second(s) Memory Limit: 32 MB

Mob was hijacked by the mayor of the Town "TruthTown". Mayor wants Mob to count the total population of the town. Now the naive approach to this problem will be counting people one by one. But as we all know Mob is a bit lazy, so he is finding some other approach so that the time will be minimized. Suddenly he found a poll result of that town where N people were asked "How many people in this town other than yourself support the same team as you in the FIFA world CUP 2010?" Now Mob wants to know if he can find the minimum possible population of the town from this statistics. Note that no people were asked the question more than once.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with an integer N (1 ≤ N ≤ 50). The next line will contain N integers denoting the replies (0 to 106) of the people.

Output

For each case, print the case number and the minimum possible population of the town.

Sample Input

Output for Sample Input

2

4

1 1 2 2

1

0

Case 1: 5

Case 2: 1

 

PROBLEM SETTER: MUHAMMAD RIFAYAT SAMEE
SPECIAL THANKS: JANE ALAM JAN


题意:一个城镇里面的人都有自己所支持的球队。现在问n个人,每个人给出与他支持相同球队的人数a[i](不包括),问你这个城镇 最小的可能人数。

一开始以为就两支队伍,然后WA了2小时。。。


AC代码:



#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define debug printf("\n");
#define MAXN 1000000+1
#define MAXM 100000
#define LL long long
using namespace std;
map<LL, LL> fp;
LL num[100];
int main()
{
    int t;
    int kcase = 1;
    scanf("%d", &t);
    while(t--)
    {
        int n;
        scanf("%d", &n);
        int a;
        if(n == 1)
        {
            scanf("%d", &a);
            printf("Case %d: %d\n", kcase++, a + 1);
            continue;
        }
        fp.clear();
        LL ans = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%lld", &num[i]);
            fp[num[i]]++;
        }
        for(int i = 1; i <= n; i++)
        {
            if(fp[num[i]])
            {
                if(num[i] == 0)
                    ans += (num[i]+1)*fp[num[i]];
                else
                {
                    if(fp[num[i]] == num[i] + 1)
                        ans += num[i]+1;
                    else if(fp[num[i]] < num[i] + 1)
                        ans += num[i]+1;
                    else
                    {
                        LL temp = fp[num[i]] / (num[i]+1);
                        if(fp[num[i]] % (num[i]+1))
                            ans += (num[i]+1) * temp + num[i]+1;
                        else
                            ans += (num[i]+1) * temp;
                    }
                }
                fp[num[i]] = 0;
            }

        }
        printf("Case %d: %lld\n", kcase++, ans);
    }
    return 0;
}




你可能感兴趣的:(lightoj 1148 - Mad Counting 【一个模拟题 因为看错题目 WA到死。。。】)