HDU2690 Boys and girls

Boys and girls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 900    Accepted Submission(s): 247


Problem Description
In a big party,there are N people some are boys and the others are girls.Now we will ask them how many girls can they see around the party(expect themselves).And the number will be A1,A2,A3...AN.Then can you tell the total number of girls using the information above?If the information is wrong and doesn't correspond to a unique valid situation,just output"Impossible!".
 

Input
The first line will contain a integer T,then T cases.
At first,there is a integer N,means N people(1<=N<10000),then N integers A1,A2...AN,indicating how many girls he/she sees(except himself/hersels).
 

Output
If the information correspond to a valid situation,output the number of people,otherwise output"Impossible!".
 

Sample Input
  
  
1 3 2 1 1
 

Sample Output
  
  
2
 

Author
Teddy
 

Source
 

Recommend
yifenfei   |   We have carefully selected several similar problems for you:   2685  2689  2687  2686  2688 
 

题目意思是说,在一个宴会上,总共有n个人(包括男生和女生),假设每个人都能看到除自己以外的任何人,告诉你每个人看到的女生人数,要你求出宴会上总的女生人数。假设有人说谎或者无法计算出女生人数,则输出Impossible!

题目虽然简单易懂,但是有坑,特别容易wrong answer。

分析:
因为每个人都不能看到自己,所以男生看到的女生人数才是正确的女生人数,而女生看到的人数比实际上少一个。假设有 x 个男生看到的女生人数为 a y 个女生看到的女生人数为 b ,那么当且仅当( a-b ==1 && x+y ==n && a==y)这个表达式成立的时候才可能确定出女生人数。特别注意, 当n==1时是无法判断的,因为不管是男生还是女生看到得都是0。,所以直接输出“Impossible!”。表达能力有限,还是直接看代码吧……%>_<%……

/*
Author:ZXPxx
Memory: 1720 KB		Time: 15 MS
Language: C++		Result: Accepted
*/

#include<cstring>
#include<cstdio>
#include<cmath>

int t,n,a,b,x,y,tmp;
int main() {
    scanf("%d",&t);
    while(t--) {
        scanf("%d",&n);
        bool flag=1;
        a=b=-1; //表示男女生看到的女生人数
        x=y=0;  //累加器,表示男女生人数
        for(int i=0; i<n; i++) {
            scanf("%d",&tmp);
            if(x==0 || tmp==a) {
                a=tmp;
                x++;
            } else if(y==0 && tmp!=a) {
                b=tmp;
                y++;
            } else if(tmp==b)
                y++;
            else if(x && y && tmp!=a && tmp!=b) {
                printf("Impossible!\n");    //出现第三种说法,表示有人说谎
                flag=0;
                break;
            }
        }
        if(!flag)
            continue;
        if(a != -1 && b == -1) {
            if(a == 0 && n == 1) {      //只有一个人无法判断
                printf("Impossible!\n");
                continue;
            }
            if(a == 0 && n > 1 && x == n) {    //当n>1 ,并且 所有人都没看到女生,说明全是男生,则女生人数是0
                printf("0\n");
                continue;
            }
            if(a == n - 1 && x == n) {      //当n>1,且每个人看到的女生数都是n-1,说明全都是女生,即女生数为 n
                printf("%d\n", n);
                continue;
            }
            printf("Impossible!\n");
            continue;
        }
        int da,xiao,fm;
        if(a>b) {
            da=a;
            xiao=b;
            fm=y;
        } else {
            da=b;
            xiao=a;
            fm=x;
        }
        //printf("%d  %d^^^%d\n",da,xiao,fm);
        if(da-xiao ==1 && x + y == n  && fm==da) {
            printf("%d\n",da);
        } else puts("Impossible!");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值