LightOJ - 1148 - Mad Counting

时间:2023-03-09 00:34:29
LightOJ - 1148 - Mad Counting

先上题目:

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 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

  基础题,告诉你一个村子里面,某n个人支持他们支持的球队的村民(除了他自己以外)还有多少人,问村子里面至少有多少人。

  不多说,分析一下就可以出结果。

上代码:

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <utility>
#define MAX 1000002
#define ll long long
using namespace std; typedef pair<ll,int> pii; ll s[];
pii p[]; int main()
{
int n,t,x,lo;
ll ans;
//freopen("data.txt","r",stdin);
scanf("%d",&t);
for(int z=;z<=t;z++){
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&x);
s[i]=x+;
}
sort(s,s+n);
memset(p,,sizeof(p));
lo=;
p[].first=s[]; p[].second=;
for(int i=;i<n;i++){
if(p[lo].first==s[i]) p[lo].second++;
else{
lo++; p[lo].first=s[i]; p[lo].second=;
}
}
ans=;
for(int i=;i<=lo;i++){
if(p[i].first==p[i].second) ans+=p[i].second;
else if(p[i].first<p[i].second){
ans+=p[i].second/p[i].first*p[i].first;
if(p[i].second%p[i].first!=) ans+=p[i].first;
}else{
ans+=p[i].first;
}
}
printf("Case %d: %lld\n",z,ans);
}
return ;
}

/*1148*/