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

本文介绍了一种通过统计调查结果来估算城镇最小人口数量的方法。该方法基于居民对自己支持球队的相似者数量的回答,利用算法优化了传统的逐个计数方式。
1148 - Mad Counting
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;
}




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值