False coin
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 27760 | Accepted: 7856 |
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
Northeastern Europe 1998
代码:
#include <iostream>
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define Max 1000
#define Maxx 100
using namespace std;
struct Node {//天平结构体
int num; //硬币编号
int left[Max / 2]; //左盘
int right[Max / 2]; //右盘
char result; //< > = 三种结果
}node[Maxx];
//函数判断一次假设与结果是否矛盾
//state创建一个假设对象
//bool weight表示假设假币比真币重还是轻 true表示重 false表示轻
//falaseCoin 表示假币
bool JugeOnce(int falseCoin, struct Node state, bool weight)
{
switch (state.result)//< > = 三种情况
{
case'<':
if (weight)//重 符合< 则假币在右盘
{
for (int i = 0; i <= state.num; i++)
{
if (state.right[i] == falseCoin)
{
return true;//假设没矛盾
}
}
return false;//假设与称量结果存在矛盾
}
else//轻 假币在左边
{
for (int i = 0; i <= state.num; i++)
{
if (state.left[i] == falseCoin)
{
return true;
}
}
return false;
}
break;
//以下两种情况类似上述情况
case'>':
if (weight)
{
for (int i = 0; i <= state.num; i++)
{
if (state.left[i] == falseCoin)
{
return true;
}
}
return false;
}
else
{
for (int i = 0; i <= state.num; i++)
{
if (state.right[i] == falseCoin)
{
return true;
}
}
return false;
}
break;
default:
for (int i = 0; i <= state.num; i++)
{
if (state.right[i] == falseCoin)
{
return false;
}
}
for (int i = 0; i <= state.num; i++)
{
if (state.left[i] == falseCoin)
{
return false;
}
}
return true;
break;
}
}
int main()
{
int n, m, ans;
scanf("%d%d", &n, &m);
int pivote = 1; //从node[1]开始记录称重次数
while (n > 0 && m > 0)
{
for (int i = 1; i <= m; i++)//循环输入m次,枚举每一个的个数
{
scanf("%d", &node[pivote].num);//输入左右盘的币
for (int j = 1; j <= node[pivote].num; j++)
scanf("%d", &node[pivote].left[j]);//左盘编号
for (int j = 1; j <= node[pivote].num; j++)
scanf("%d", &node[pivote].right[j]);//右盘编号
getchar();
node[pivote++].result = getchar();
}
int falsecount = 0;//假币计数
for (int i = 1; i <= n; i++) {
bool trag = true;//trag表示硬币i是假币
for (int j = 1; j < pivote; j++) {//测试为轻的情况
if (!JugeOnce(i, node[j], false)) {
trag = false;
break;
}
}
if (trag) {
falsecount++;
if (falsecount > 1) {
printf("0\n");
return 0;
}
ans = i;
}
else {
trag = true;
for (int j = 1; j < pivote; j++) {
if (!JugeOnce(i, node[j], true)) {
trag = false;
break;
}
}
if (trag) {
falsecount++;
if (falsecount > 1)
{
printf("0\n");
return 0;
}
ans = i;
}
}
}
printf("%d\n", ans);//输出结果
return 0;
}
}