Start with the question:
1148-mad counting
|
PDF (English) |
Statistics |
Forum |
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 whereNPeople 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 integerT (≤ 100), Denoting the number of test cases.
Each case starts with an integerN (1 ≤ n ≤ 50). The next line will containNIntegers denoting the replies(0-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 |
This topic tells you how many villagers (except themselves) who support the team supported by N people in a village and how many people are there in the village.
Not to mention, you can analyze the results.
Code:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <utility> 5 #define MAX 1000002 6 #define ll long long 7 using namespace std; 8 9 typedef pair<ll,int> pii;10 11 ll s[52];12 pii p[52];13 14 int main()15 {16 int n,t,x,lo;17 ll ans;18 //freopen("data.txt","r",stdin);19 scanf("%d",&t);20 for(int z=1;z<=t;z++){21 scanf("%d",&n);22 for(int i=0;i<n;i++){23 scanf("%d",&x);24 s[i]=x+1;25 }26 sort(s,s+n);27 memset(p,0,sizeof(p));28 lo=0;29 p[0].first=s[0]; p[0].second=1;30 for(int i=1;i<n;i++){31 if(p[lo].first==s[i]) p[lo].second++;32 else{33 lo++; p[lo].first=s[i]; p[lo].second=1;34 }35 }36 ans=0;37 for(int i=0;i<=lo;i++){38 if(p[i].first==p[i].second) ans+=p[i].second;39 else if(p[i].first<p[i].second){40 ans+=p[i].second/p[i].first*p[i].first;41 if(p[i].second%p[i].first!=0) ans+=p[i].first;42 }else{43 ans+=p[i].first;44 }45 }46 printf("Case %d: %lld\n",z,ans);47 }48 return 0;49 }
/X 1148 */