hdu 1029(模拟)

来源:互联网 发布:淘宝店能上传多少宝贝 编辑:程序博客网 时间:2024/05/29 14:27
False coin
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 18531 Accepted: 5210

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 32 1 2 3 4<1 1 4=1 2 5=

Sample Output

3

Source

Northeastern Europe 1998

   哈哈,真幸运,本来想着写不写这篇博客,因为有个小问题一直纠结我很久了,一直想不通,想着写写吧,没想到刚上手就把那个小问题想通了。

题意:有N枚编号为1~N的硬币,重量一样,只有一枚质量不同,现有一天平,能在左边和右边放置数量相同的硬币,并且能看出来哪堆重,哪堆轻,哪堆一样。现问你能否通过这K次测量,找出那枚硬币。

    思路:因为只有一枚不同的硬币,每次在比较的时候,只要那枚有问题的硬币出现天平就会产生倾斜。这时我们分别标记下来在天平轻的一方那些硬币和重的一方那些硬币,并统计它们出现的次数, 而平衡时说明上面的硬币都没有问题,把那些硬币标记下来。所以最后我们只要统计一下天平倾斜的次数,就代表了那枚问题硬币的出现次数,找出有多少枚硬币在轻的一方或重的一方出现的次数正好等于天平倾斜的次数就行了,思路不难,但是WA了很多次,因为有很多需要考虑的地方,至于我WA的地方,代码中会给你们指明清楚。


#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <stack>#include <map>#include <set>#include <queue>#include <vector>#define inf 0x6fffffff#define LL long long#define mem(p,k) memset(p,k,sizeof(p));using namespace std;int p2[1010],p1[1010],h1[1010],h2[1010],f[1010];int main(){    int n,k;    while(cin>>n>>k){        int  cur=0;//用来储存天平倾斜的次数        mem(f,0);        mem(p1,0);        mem(p2,0);        for(int i=0;i<k;i++){            int num;            cin>>num;            for(int j=0;j<num;j++){                scanf("%d",h1+j);//h1数组用来储存轻的一方硬币            }            for(int j=0;j<num;j++){                scanf("%d",h2+j);//h2数组用来储存重的一方            }            char c;            getchar();            scanf("%c",&c);            if(c=='='){//相同时用F数组储存下来没有问题的硬币                for(int j=0;j<num;j++){                    f[h1[j]]=1;                    f[h2[j]]=1;                }            }            else {//记录下来天平出现倾斜时,在轻的一方硬币出现的次数和重的一方出现的次数                if(c=='<'){                  for(int j=0;j<num;j++){                         p1[h1[j]]++;                         p2[h2[j]]++;                  }                }                else{                    for(int j=0;j<num;j++){                         p2[h1[j]]++;                         p1[h2[j]]++;                  }                }                cur++;            }        }        int cur1=0,z=0;        for(int i=1;i<=n;i++){            if(!f[i]&&p1[i]==cur){//当出现的次数等于天平倾斜的次数时记录,并且当出现多枚时无法判断                if(!z)z=i;                else {                    cur1=1;                    break;                }            }            if(!f[i]&&p2[i]==cur){                if(!z||z==i)z=i;//这里就是出现问题的地方,因为我没有考虑cur==0的情况,                else {          //当cur==0时,它的p1,p2数组里的值相同都是0,所以要加一个z==i的条件                    cur1=1;                    break;                }            }        }        if(cur1==0){            cout<<z<<endl;        }        else {            cout<<0<<endl;        }    }    return 0;}


1 0
原创粉丝点击