poj-False coin

 

描述

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.

 

输入

 

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.

 

输出

 

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.

样例输入

5 3
2 1 2 3 4
<
1 1 4
=
1 2 5
=

样例输出

3

解题思路:

有两种思路,其实本质差不多。

思路一,对于错的硬币,如果有<或者>,那么这个硬币一定出现。其次对于所有测试,这个硬币出现只有两种情况,要么比其他所有硬币质量都轻,要么比其他都重,我们只要设两个数组,一个专门保存轻的一边的数据,另一个专门保持重一边的数据。然后统计两个数组中的元素等于总次数的硬币,即为所求。

思路二,对于错的硬币而言,无论他被分在左边还是分在右边,只有有<或者>,该硬币每次都会变化。所以最后变化绝对值最大的一定是有问题的硬币。然而.....这种思路在牛客网可以过,在poj上却不行......并不知道为什么:(

解法1:

# include<stdio.h>
# include<iostream>
using namespace std;
# define INF 123123123
int a[1001],b[1001];
int pl[1001],pr[1001];
int main()
{
   int n,i,j;
   while(cin>>n)
   {
      for(i=0;i<=1001;i++)
	  {
	     a[i]=b[i]=0;
	  }

	  int p,num=0;
	  cin>>p;
	  while(p--)
	  {
	     int cnt;
		 cin>>cnt;
	     for(j=1;j<=cnt;j++)
		 {
		    cin>>pl[j];
		 }
		 for(j=1;j<=cnt;j++)
			 cin>>pr[j];
		 char c;
		 cin>>c;


		 if(c=='=')
		 {
		    for(i=1;i<=cnt;i++)
			{
			   a[pl[i]]=a[pr[i]]=INF;
			   b[pl[i]]=b[pr[i]]=INF;
			}
		 }

		 else if(c=='<')
		 {
			 num++;
			 for(i=1;i<=cnt;i++){
				 if(a[pl[i]]!=INF)
				    a[pl[i]]++;
				 if(a[pr[i]]!=INF)
					 b[pr[i]]++;
			 }
		 }
		 else if(c=='>')
		 {
			 num++;
			 for(i=1;i<=cnt;i++){
				 if(a[pl[i]]!=INF)
				     b[pl[i]]++;
				 if(a[pr[i]]!=INF)
					 a[pr[i]]++;
			 }
		 }

	  }

	  int count=0,minv=0,mini=0;
	  for(i=1;i<=n;i++)
	  {
	     if(a[i]==num||b[i]==num)
		 {
		    mini=i;
			count++;
			if(count>1)
				break;
		 }
	  }

	  if(count==1)
		  cout<<mini<<endl;
	  else
		  cout<<0<<endl;
   }
   return 0;
}

解法2:牛客网上能过,但是poj上不能过........ :(

# include<stdio.h>
# include<iostream>
using namespace std;
int abs(int x)
{
	return x>0?x:-x;
}
int mark[1001];
int main()
{
   int n,i,j;
   while(cin>>n)
   {
      for(i=0;i<=1001;i++)
		  mark[i]=0;
	  int p;
	  cin>>p;
      int nn=n,np=p;
	  while(p--)
	  {
	     int cnt;
		 cin>>cnt;
		 int b[1001];
	     for(j=1;j<=cnt*2;j++)
		 {
		    cin>>b[j];
		 }
		 char c;
		 cin>>c;

		 if(c=='=')
		 {
		    for(i=1;i<=2*cnt;i++)
				mark[b[i]]=1000;
		 }
		 else if(c=='<')
		 {
			 for(i=1;i<=cnt;i++){
				 if(b[i]!=1000){
				    mark[b[i]]--;
				 }
			 }
			 for(i=cnt+1;i<=2*cnt;i++){
				 if(b[i]!=1000){
				   mark[b[i]]++;
				 }
			 }
		 }
		 else if(c=='>')
		 {
			 for(i=1;i<=cnt;i++){
				 if(b[i]!=1000){
				    mark[b[i]]++;
				 }
			 }
			 for(i=cnt+1;i<=2*cnt;i++){
				 if(b[i]!=1000){
				   mark[b[i]]--;
				 }
			 }
		 }

	  }


	  int count=0,minv=0,mini=0;
	  for(i=1;i<=n;i++)
	  {
	     if((mark[i]!=1000) &&(abs(mark[i])>=minv))
		 {
		    minv=abs(mark[i]);
			mini=i;
		 }
	  }
	  for(i=1;i<=n;i++)
	  {
	      if(abs(mark[i])==minv)
			  count++;
	  }

	  if(count==1)
		  cout<<mini<<endl;
	  else
		  cout<<0<<endl;
   }
   return 0;
}

 

 

 

posted @ 2018-05-03 22:05  xzhws  阅读(65)  评论(0编辑  收藏  举报