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 |
要统计一个镇的人数,问一些人,他们一组的除了他自己还有几个人,求出这种情况下最少的人数,假如是 2 2 1,第一个人可以喝第二个人是一组,这样保证人数可以最少!
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int m,cot=1;
long long a[1000];
scanf("%d",&m);
while(m--)
{
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%lld",&a[i]);
sort(a,a+n);
long long all=0,num=0;
for(i=0;i<n;i++)
{
if(i==0||a[i]==a[i-1])
num++;//记录每个数出现的次数
else
{
int t=(num+a[i-1])/(a[i-1]+1);//t是最少的组数
all+=t*(a[i-1]+1);
num=1;
}
}
all+=(num+a[i-1])/(a[i-1]+1)*(a[i-1]+1);//最后一组
printf("Case %d: %lld\n",cot++,all);
}
return 0;
}