周练-a—lightoj1148

来源:互联网 发布:abook网络课程资源 编辑:程序博客网 时间:2024/06/06 04:01

Description

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 Npeople 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个人,每个人回答除其本人外还有几个人与他喜欢同一球队;

最后问满足上述的情况下,问该村最少有多少人?

解题思路:首先,如果N个人里面有2个的回答都是1,那么他们可以组成一组,(即他两都喜欢某个球队)这样会使最后总人数最少,当大于两个,每两个可以组一对;表示都喜欢同一求队,把这些可以2人组队的加到总数,再把剩余加个2,最后肯定剩一人不等组队,这就是回答一的人数使总人数最少的情况;

同理,回答2,3,4,5与上述类似;其实就是统计回答1,2,3,4...的人各有多少,然后将他们归到一队就可以使总人数最少,回答1的2人归一队,回答2的3个人归一队;。。。

AC代码:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct n{int x;int cnt;}ans[54];int main(){int t;int n,k,i,j,m,c;int a[100];scanf("%d",&t);c=1;while(t--){    memset(ans,0,sizeof(ans));scanf("%d",&n);for(i=0;i<n;i++) scanf("%d",&a[i]);sort(a,a+n);int sum=0,l=0;for(i=0;i<n;i++){m=a[i];k=0;            if(m>=0)            {for(j=0;j<n;j++){if(m==a[j]){  k++;a[j]=-1;}}ans[l].cnt=k;ans[l++].x=m;a[i]=-1;   }}for(i=0;i<l;i++){if(ans[i].x==1){if(ans[i].cnt%2==0) sum+=(ans[i].cnt/2)*2;else sum+=(ans[i].cnt/2)*2+2;}else{   if(ans[i].cnt%(ans[i].x+1)==0)    sum+=(ans[i].cnt/(ans[i].x+1))*(ans[i].x+1);   else    sum+=((ans[i].cnt/(ans[i].x+1))*(ans[i].x+1)+ans[i].x+1);    }   }printf("Case %d: ",c++);printf("%d\n",sum);}return 0;}


0 0