| 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 |
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define debug printf("\n");
#define MAXN 1000000+1
#define MAXM 100000
#define LL long long
using namespace std;
map<LL, LL> fp;
LL num[100];
int main()
{
int t;
int kcase = 1;
scanf("%d", &t);
while(t--)
{
int n;
scanf("%d", &n);
int a;
if(n == 1)
{
scanf("%d", &a);
printf("Case %d: %d\n", kcase++, a + 1);
continue;
}
fp.clear();
LL ans = 0;
for(int i = 1; i <= n; i++)
{
scanf("%lld", &num[i]);
fp[num[i]]++;
}
for(int i = 1; i <= n; i++)
{
if(fp[num[i]])
{
if(num[i] == 0)
ans += (num[i]+1)*fp[num[i]];
else
{
if(fp[num[i]] == num[i] + 1)
ans += num[i]+1;
else if(fp[num[i]] < num[i] + 1)
ans += num[i]+1;
else
{
LL temp = fp[num[i]] / (num[i]+1);
if(fp[num[i]] % (num[i]+1))
ans += (num[i]+1) * temp + num[i]+1;
else
ans += (num[i]+1) * temp;
}
}
fp[num[i]] = 0;
}
}
printf("Case %d: %lld\n", kcase++, ans);
}
return 0;
}
本文介绍了一种通过统计调查结果来估算城镇最小人口数量的方法。该方法基于居民对自己支持球队的相似者数量的回答,利用算法优化了传统的逐个计数方式。

1504

被折叠的 条评论
为什么被折叠?



