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个人询问在他们的村庄有多少人和他自己喜欢同一个球队根据数据估计村庄的最少人数
题解:输入数据对数据从小到大排序,如果有连续相同的数a则将这些连续相同的看做是喜欢的同一个球队的人,(相同的个数最多是a+1 如果超过这个则按另一支队算)
#include<stdio.h> #include<string.h> #include<algorithm> #define MAX 1000 #define MAXM 1001000 #define INF 0x7fffff #define LL long long using namespace std; int s[MAXM]; int main() { int n,m,j,i,t,k; int sum; //LL sum; scanf("%d",&t); k=1; while(t--) { sum=0; int ans; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&s[i]); sort(s,s+n); for(i=0;i<n;i++) { int num=0; ans=s[i]; num=s[i]; while(s[i+1]==ans&&num) { i=i+1; num--; } sum=sum+ans+1; } printf("Case %d: ",k++); printf("%d ",sum); } return 0; } #include<stdio.h> #include<string.h> #include<algorithm> #define MAX 1000 #define MAXM 1001000 #define INF 0x7fffff #define LL long long using namespace std; int s[MAXM]; int main() { int n,m,j,i,t,k; int sum; //LL sum; scanf("%d",&t); k=1; while(t--) { sum=0; int ans; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&s[i]); sort(s,s+n); for(i=0;i<n;i++) { int num=0; ans=s[i]; num=s[i]; while(s[i+1]==ans&&num) { i=i+1; num--; } sum=sum+ans+1; } printf("Case %d: ",k++); printf("%d ",sum); } return 0; }