LightOJ - 1148 - 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个人支持他们支持的球队的村民(除了他自己以外)还有多少人,问村子里面至少有多少人。
不多说,分析一下就可以出结果。
上代码:
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 }