Lightoj1148--Mad Counting(模拟)

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 <algorithm>
#define N 51
using namespace std;
int num[N];
int main(){
    int t, Tim = 1;
    scanf("%d", &t);
    while(t--){
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            scanf("%d", &num[i]);
        sort(num, num+n);
        int cnt = 0, sum = 0;
        for(int i = 0; i < n; i++){
            if(i == 0 || num[i]==num[i-1])    //notice;
                cnt++;
            else{
                int k = (num[i-1]+cnt)/(num[i-1]+1);
                sum += k*(num[i-1]+1);
                cnt = 0;
            }
        }
        sum += (cnt+num[n-1])/(1+num[n-1])*(1+num[n-1]);   //*****
        printf("Case %d: %d
", Tim++, sum);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/soTired/p/5024190.html