LightOJ 1148 - Mad Counting (找规律)

Mob was hijacked by the mayor of the Town"TruthTown". Mayor wants Mob to count the total population of thetown. Now the naive approach to this problem will be counting people one byone. But as we all know Mob is a bit lazy, so he is finding some other approachso that the time will be minimized. Suddenly he found a poll result of thattown where N people were asked "How many people in this town otherthan 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 thetown from this statistics. Note that no people were asked the question morethan 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 (0to 106) of the people.

Output

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

Sample Input

Output for Sample Input

2

4

1 1 2 2

1

0

Case 1: 5

Case 2: 1

 

题意:

分别询问n 个人和他支持同一支队伍的人数有多少,从而算出城市里的最少总人数。

思路:

将输出的人数 y +1,人数有(y+1)的(y + 1) 个集合 可为一组,不足(y+1)个集合的则总人数加 y+1。

使用数组的下标进行循环,0ms AC.

CODE:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
typedef long long ll;
using namespace std;

int vis[55];
int a[55];
set<int> s;
int main()
{
    //freopen("in.in","r",stdin);
    int T;
    scanf("%d",&T);
    for(int tt=1;tt<=T;tt++)
    {
        int n;
        scanf("%d",&n);
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            a[i]++;
            s.insert(a[i]);
        }
        sort(a,a+n);
        int k=0;
        int ans=0;
        set<int>::iterator it;
        for(it=s.begin();it!=s.end();it++)
        {
            k=upper_bound(a,a+n,*it)-lower_bound(a,a+n,*it);
            ans += (k/(*it))*(*it);
            if(k%(*it) != 0)
            ans += (*it);
        }
         printf("Case %d: %d\n",tt,ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值