zoukankan      html  css  js  c++  java
  • POJ False coin (模拟)

    False coin

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 4   Accepted Submission(s) : 1
    Problem Description
    The "Gold Bar"bank received information from reliable sources that in their last group of N coins exactly one coin is false and differs in weight from other coins (while all other coins are equal in weight). After the economic crisis they have only a simple balance available (like one in the picture). Using this balance, one is able to determine if the weight of objects in the left pan is less than, greater than, or equal to the weight of objects in the right pan. In order to detect the false coin the bank employees numbered all coins by the integers from 1 to N, thus assigning each coin a unique integer identifier. After that they began to weight various groups of coins by placing equal numbers of coins in the left pan and in the right pan. The identifiers of coins and the results of the weightings were carefully recorded. You are to write a program that will help the bank employees to determine the identifier of the false coin using the results of these weightings.
     
    Input
    The first line of the input file contains two integers N and K, separated by spaces, where N is the number of coins (2<=N<=1000 ) and K is the number of weightings fulfilled (1<=K<=100). The following 2K lines describe all weightings. Two consecutive lines describe each weighting. The first of them starts with a number Pi (1<=Pi<=N/2), representing the number of coins placed in the left and in the right pans, followed by Pi identifiers of coins placed in the left pan and Pi identifiers of coins placed in the right pan. All numbers are separated by spaces. The second line contains one of the following characters: '<', '>', or '='. It represents the result of the weighting: '<' means that the weight of coins in the left pan is less than the weight of coins in the right pan, '>' means that the weight of coins in the left pan is greater than the weight of coins in the right pan, '=' means that the weight of coins in the left pan is equal to the weight of coins in the right pan.
     
    Output
    Write to the output file the identifier of the false coin or 0, if it cannot be found by the results of the given weightings.
     
    Sample Input
    5 3 2 1 2 3 4 < 1 1 4 = 1 2 5 =
     
    Sample Output
    3
     
    Source
    PKU
     
     

    后来,从discuss里看到这种方法,感觉思路很特别,

     

    对于每一次测验结果
    如果是平局,那么所有参与测验的硬币都肯定没问题
    如果是一边轻
    那么所有没参与测验的硬币都肯定没问题
    而轻的一边则可能是轻的,重的一边可能是重的
    这样从信息上没有漏掉任何信息
    
    最后遍历所有硬币
    if 他的normal[i]是true,他肯定没问题
    else if 他的light[i]和heavy[i]同时是true,他也肯定没问题,因为如果他是轻的或者重的,那么测验结果不可能暗示他既可能轻也可能重
    所以这样遍历之后,我们知道每个硬币是安全/不安全的
    如果不安全的硬币只有一个,输出之
    如果不安全的硬币大于1,输出0

     

    于是根据这个思想写出代码,AC了 (特别注意标粗的地方,这个处理很特别)

     

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int maxn=1010;
    
    int normal[maxn],light[maxn],heavy[maxn];
    int num[maxn];
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int n,k;
        while(~scanf("%d%d",&n,&k)){
            memset(normal,0,sizeof(normal));
            memset(light,0,sizeof(light));
            memset(heavy,0,sizeof(heavy));
            int m;
            char ch;
            for(int i=0;i<k;i++){
                scanf("%d",&m);
                for(int j=0;j<2*m;j++)
                    scanf("%d",&num[j]);
                while((ch=getchar())!='=' && ch!='>' && ch!='<')    //以后多用这种读取单个输入字符的方式 
                    ;
                if(ch=='>'){
                    for(int j=0;j<m;j++)
                        heavy[num[j]]=1;
                    for(int j=m;j<2*m;j++)
                        light[num[j]]=1;
                    for(int j=1;j<=n;j++)
                        if(!normal[j]){
                            int flag=1;
                            for(int k=0;k<2*m;k++)
                                if(j==num[k]){
                                    flag=0;
                                    break;
                                }
                            if(flag)
                                normal[j]=1;
                        }
                }else if(ch=='='){
                    for(int j=0;j<2*m;j++)
                        normal[num[j]]=1;
                }else if(ch=='<'){
                    for(int j=0;j<m;j++)
                        light[num[j]]=1;
                    for(int j=m;j<2*m;j++)
                        heavy[num[j]]=1;
                    for(int j=1;j<=n;j++)
                        if(!normal[j]){
                            int flag=1;
                            for(int k=0;k<2*m;k++)
                                if(j==num[k]){
                                    flag=0;
                                    break;
                                }
                            if(flag)
                                normal[j]=1;
                        }
                }
            }
            int ans,cnt=0;
            for(int i=1;i<=n;i++){
                if(normal[i])
                    continue;
                if(heavy[i] && light[i])
                    continue;
                ans=i;
                cnt++;
            }
            //printf("cnt=%d\n",cnt);
            if(cnt>1)
                printf("0\n");
            else
                printf("%d\n",ans);
        }
        return 0;
    }
  • 相关阅读:
    win8 vs2010 openni2 配置
    写一个程序,分析一个文本文件(英文文章)中各个词出现的频率,并且把频率最高的十个词打印出来。
    电梯调度
    new 一个button 然后dispose,最后这个button是null吗???
    org.apache.hadoop.security.AccessControlException
    算法导论第二章、插入排序
    算法导论第六章、堆排序
    算法导论第八章、计数排序
    观后感
    第二次随笔
  • 原文地址:https://www.cnblogs.com/jackge/p/3035741.html
Copyright © 2011-2022 走看看