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 whereN 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 containN 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 |
题意:询问n个人,问他们与他支持相同的球队的人数,根据他们的回答,求这个城市中最少有多少人。
代码1:
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int T,n;
int i;
int a[60];
int k=1;
scanf("%d",&T);
while(T--)
{
int sum=0;
int num=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
for(i=0;i<n;i++)
{
if(i==0||a[i]==a[i-1])
{
num++;
}
else
{
sum+=(a[i-1]+num)/(a[i-1]+1)*(a[i-1]+1);
num=0;
}
}
sum+=(a[n-1]+num)/(a[n-1]+1)*(a[n-1]+1);
printf("Case %d: %d\n",k++,sum);
}
return 0;
}
#include<stdio.h>
#include<algorithm>
using namespace std;
#include<string.h>
int a[100];
int b[1000010];
int main()
{
int T;
int n,i,k=1,e,d;
scanf("%d",&T);
while(T--)
{
int s=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
memset(b,0,sizeof(b));
b[a[0]]=1;
for(i=1;i<n;i++)
{
if(a[i]==a[i-1])
b[a[i]]++;
else
b[a[i]]=1;
}
for(i=0;i<=a[n-1];i++)
{
e=b[i]%(i+1);
d=b[i]/(i+1);
if(e==0)
{
s+=d*(i+1);
}
else
{
s+=d*(i+1);
s+=(i+1);
}
}
printf("Case %d: %d\n",k++,s);
}
return 0;
}