lightOJ 1148 【水题】
来源:互联网 发布:js在线混淆 编辑:程序博客网 时间:2024/05/21 07:53
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个数进行排序,然后相同的数将会在一起,然后统计相同的数的个数,如果相同的数的个数相遇等于这个数的值,那么就将这个值降到sum上,如果大于这个值,那么就重新开一个组,也就是重新统计这个数的个数(也就是将这个数赋值为1,继续往后统计),就这样一直统计,直到统计结束为止!
代码:
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int n;int a[55];int main(){int T;int N;scanf("%d",&T);N=T;while(T--){scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&a[i]);a[i]++;}printf("Case %d: ",N-T);sort(a+1,a+n+1);int sum=0;int t=a[1];int num=1;for(int i=2;i<=n;i++){if(a[i]==t&&num<a[i]){num++;}else{t=a[i];num=1;sum+=a[i-1];}}printf("%d\n",sum+a[n]);}return 0;}
- lightOJ 1148 【水题】
- lightOJ 1148 - Mad Counting 【水题】
- LightOJ 1148
- LightOJ 1148 - Mad Counting【水题+规律】
- LightOj 1148 Basic Math
- lightoj -1006 水题
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- [LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- Android 线程之Handler使用
- Xcode7新功能
- ubuntu14.04 配置tomcat8
- Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory
- 走马观花了解jvm spec
- lightOJ 1148 【水题】
- opencv导入android studio
- gluPerspective的牛逼解释
- Ubuntu 软件仓库公钥不可用解决方案
- Access restriction:The type JPEGCodec is not accessible due to restriction on required library
- 完善Corona工程中的config.lua文件
- hdu1561 The more, The Better
- css的link a:hover 必须在 a:link 和 a:visited 之后
- 搞晕我的openGL坐标系