F

来源:互联网 发布:2003版办公软件 编辑:程序博客网 时间:2024/05/17 02:37

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

2

4

1 1 2 2

1

0

Sample Output

Case 1: 5

Case 2: 1

题意:一个很懒的人要统计小镇的人数,他蛋疼的要求我们写一个代码帮他估算出小镇的最小可能有的人数,依据是这样的:他会问小镇n个人,每个人要说出除他以外和他一样支持 FIFA 2010世界杯队伍的人有多少(为啥不是NBA呢…………也对,唠嗑,君子雷退后也没啥喜欢的球队了,唉);

思路:写了几组数据后,你会发现要使人尽量的少,具有相同支持人数的可以凑一块,如果不需要在增加的人话,那么具有相同支持人数的人必须是支持人数+1的倍数;

所以这一题也就转化为了统计具有相同支持人数的人数,再对每一个队伍支持人数进行遍历,累加出除了被问的那n个人外需要增加的人数;

比如 :支持人数为2的有2人,那他需要再增加一人才能满足条件,而如果是3人的话则不需要再增加人数了;

下面附上代码:

#include<bits/stdc++.h>using namespace std;int num[55];int main(){int t,n,k=0,sum,p,g,l;cin>>t;while(t--){cin>>n;for(int i=0;i<n;i++)scanf("%d",&num[i]);sort(num,num+n);sum=0,l=1;for(int i=0;i<n;i++){g=1;p=num[i];for(int j=i+1;j<n;j++){if(num[j]==p)g++;else break;}i+=g-1;if(g%(num[i]+1)!=0) sum+=num[i]+1-g%(num[i]+1);}printf("Case %d: %d\n",++k,sum+n);}return 0;}





原创粉丝点击