lightOJ 1148 【水题】

来源:互联网 发布:js在线混淆 编辑:程序博客网 时间:2024/05/21 07:53
C - 楼下水题
Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Submit Status Practice LightOJ 1148

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个数进行排序,然后相同的数将会在一起,然后统计相同的数的个数,如果相同的数的个数相遇等于这个数的值,那么就将这个值降到sum上,如果大于这个值,那么就重新开一个组,也就是重新统计这个数的个数(也就是将这个数赋值为1,继续往后统计),就这样一直统计,直到统计结束为止!


代码:


#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int n;int a[55];int main(){int T;int N;scanf("%d",&T);N=T;while(T--){scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&a[i]);a[i]++;}printf("Case %d: ",N-T);sort(a+1,a+n+1);int sum=0;int t=a[1];int num=1;for(int i=2;i<=n;i++){if(a[i]==t&&num<a[i]){num++;}else{t=a[i];num=1;sum+=a[i-1];}}printf("%d\n",sum+a[n]);}return 0;}



0 0
原创粉丝点击