周练-a—lightoj1148
来源:互联网 发布:abook网络课程资源 编辑:程序博客网 时间:2024/06/06 04:01
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个人,每个人回答除其本人外还有几个人与他喜欢同一球队;
最后问满足上述的情况下,问该村最少有多少人?
解题思路:首先,如果N个人里面有2个的回答都是1,那么他们可以组成一组,(即他两都喜欢某个球队)这样会使最后总人数最少,当大于两个,每两个可以组一对;表示都喜欢同一求队,把这些可以2人组队的加到总数,再把剩余加个2,最后肯定剩一人不等组队,这就是回答一的人数使总人数最少的情况;
同理,回答2,3,4,5与上述类似;其实就是统计回答1,2,3,4...的人各有多少,然后将他们归到一队就可以使总人数最少,回答1的2人归一队,回答2的3个人归一队;。。。
AC代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct n{int x;int cnt;}ans[54];int main(){int t;int n,k,i,j,m,c;int a[100];scanf("%d",&t);c=1;while(t--){ memset(ans,0,sizeof(ans));scanf("%d",&n);for(i=0;i<n;i++) scanf("%d",&a[i]);sort(a,a+n);int sum=0,l=0;for(i=0;i<n;i++){m=a[i];k=0; if(m>=0) {for(j=0;j<n;j++){if(m==a[j]){ k++;a[j]=-1;}}ans[l].cnt=k;ans[l++].x=m;a[i]=-1; }}for(i=0;i<l;i++){if(ans[i].x==1){if(ans[i].cnt%2==0) sum+=(ans[i].cnt/2)*2;else sum+=(ans[i].cnt/2)*2+2;}else{ if(ans[i].cnt%(ans[i].x+1)==0) sum+=(ans[i].cnt/(ans[i].x+1))*(ans[i].x+1); else sum+=((ans[i].cnt/(ans[i].x+1))*(ans[i].x+1)+ans[i].x+1); } }printf("Case %d: ",c++);printf("%d\n",sum);}return 0;}
- 周练-a—lightoj1148
- lightoj1148 - Mad Counting
- HPU第七周周练 A
- 2016.13周 周练A - Wet Shark and Odd and Even【CF】
- 百练 1000 A+B Problem
- 并查集训练题解(A-E)
- A -KIDx's Pagination(练功底)
- A - Registration system STL专练
- (python)百练1000: A+B Problem
- 递归专练A 递归的函数
- 【周练】 求导
- 【周练】 物理题
- 百练 1005 I Think I Need a Houseboat
- 【C语言天天练(零)】a.out简介
- 百练/ 2015研究生上机测试-A:细胞分裂
- 问题 A: 银河英雄传说--并查集训练T2
- 2016多校训练Contest6: 1002 A Simple Chess hdu5794
- 2016多校训练Contest6: 1003 A Simple Nim hdu5795
- CasperJS学习笔记--循环
- Imageloader<1>-总体简介
- 封装
- OC-控制结构
- 集合
- 周练-a—lightoj1148
- 设计模式学习(一)代理模式
- java scanner 输入多行
- make条件判断ifeq,ifneq,ifdef,ifndef
- [LeetCode]Game of Life
- Teamtalk简介
- 1045. Favorite Color Stripe (30)——PAT (Advanced Level) Practise
- 探索Android中selector和shape的结合使用
- Linux防火墙iptables学习笔记(三)iptables命令详解和举例