POJ1029 False Coin(暴力)

题目链接

False coin

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21217   Accepted: 5958

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

暴力枚举每一枚硬币分别是假币的情况,假币有可能比真币轻也有可能比真币重,与所有称量次数相比较,若矛盾则该种情况不可能,若不矛盾则该硬币是假币的假设成立,最后统计成立的假设个数,若超过2或等于0都是无法判断,只有等于1的情况下等确定假币。

AC代码:

#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<vector>
#include<stack>
using namespace std;
struct node
{
	int p;//天平每边放的硬币个数
	vector<int> pi;//硬币
	char ch;//称量结果
};
vector<node> wt;//称量记录
bool islegal(int n,int m)//n号硬币在比真硬币重或轻的情况下是否满足每一次称量记录 
{
    //m=-1是比真币轻的假币,m=1是比真币重的假币
	vector<int>::iterator f;
	for(int i=0;i<wt.size();i++)
	{   
		f=find((wt[i].pi).begin(),(wt[i].pi).end(),n);
		if(f<(wt[i].pi).begin()+wt[i].p)//该硬币出现在此次称量记录中且位于左盘
		{
			if(m==-1&&wt[i].ch!='<') return 0;
			else if(m==1&&wt[i].ch!='>') return 0; 	
		} 
		else if(f>=(wt[i].pi).begin()+2*wt[i].p) //未出现
		{
			if(wt[i].ch!='=') return 0;
		}
		else //该硬币出现在此次称量记录中且位于右盘
		{
			if(m==-1&&wt[i].ch!='>') return 0;
			else if(m==1&&wt[i].ch!='<') return 0; 
		}
	} 
	return 1;
}
int main()
{
	int N,K,i,j;
	cin>>N>>K;
	while(K--)
	{
		node t;
        int n;
        cin>>t.p;
        for(i=1;i<=2*t.p;i++) 
        {cin>>n;(t.pi).push_back(n);}
        cin>>t.ch;
        wt.push_back(t);	
	}
	int num=0,ans=0;
	for(i=1;i<=N&&num<2;i++)
	{
		for(j=-1;j<=1;j+=2)
		{	
		    if(islegal(i,j))
			{num++;ans=i;break;}	
		}
	}
	if(num==1) cout<<ans<<endl;
	else cout<<'0'<<endl;
}

猜你喜欢

转载自blog.csdn.net/qq_40889820/article/details/81409116