Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

一次ac,要注意三个个要点(附代码,解题思路)

Posted by zjwzh at 2011-07-26 20:20:25 on Problem 1029
一个是假币在不等号两边出现的次数一定等于不等式的个数,这个是显然的,否则就没有那么多不等式。第二是等号两边出现过的都是真币。第三是当所有的式子都是等式时候,要检查一下没出现在等式的硬币,如果一个没出现就是确定它是假币,否则不能确定。

#include<iostream>
using namespace std;
bool coin[100];
int Min[100];
int Max[100];
int N;
int K;
int main()
{
	memset(coin,false,sizeof(bool));

	memset(Min,0,sizeof(int));

	memset(Max,0,sizeof(int));

	cin>>N>>K;

	int countNotEqual=0;

	for(int i=0;i<K;i++)
	{
		int count=0;

		cin>>count;

		int record[200];

		for(int j=0;j<count*2;j++)
		{
			cin>>record[j];
		}

		char oper;

		cin>>oper;

		if(oper=='=')
		{
			for(int j=0;j<count*2;j++)
				coin[record[j]-1]=true;
		}
		else
		{
			countNotEqual++;

			if(oper=='<')
			{
				for(int j=0;j<count;j++)
				{
					Min[record[j]-1]++;
				}

				for(int j=count;j<2*count;j++)
				{
					Max[record[j]-1]++;
				}
			}
			else
			{
				
				for(int j=0;j<count;j++)
				{
					Max[record[j]-1]++;
				}

				for(int j=count;j<2*count;j++)
				{
					Min[record[j]-1]++;
				}
			}
		}
	}

	bool minexit=false;

	int minresult=0;

	int countOfMinResult=0;

	//找最小的
	for(int i=0;i<N;i++)
	{
		if(Min[i]==countNotEqual && !coin[i])
		{
			if(countOfMinResult==0)//第一个结果
			{
				minresult=i+1;

				countOfMinResult++;

				minexit=true;
			}
			else
			{
				minexit=false;

				break;
			}

		}
	}

	bool maxexit=false;

	int maxresult=0;

	int countOfMaxResult=0;

	for(int i=0;i<N;i++)
	{
		if(Max[i]==countNotEqual && !coin[i])
		{
			if(countOfMaxResult==0)//第一个结果
			{
				maxresult=i+1;

				countOfMaxResult++;

				maxexit=true;
			}
			else
			{
				maxexit=false;

				break;
			}

		}
	}
	int result=0;

	if((minexit && maxexit) || (!minexit && !maxexit))
	{
		result=0;
	}
	else
	{
		if(minexit)
			result=minresult;
		else
			result=maxresult;
	}

	if(result==0)
	{
		bool only=true;

		for(int i=0;i<N;i++)
		{
			if(!coin[i] )
			{
				if(only)
				{
					result=i+1;

					only=false;
				}
				else
				{
					result=0;

					break;
				}
			}
		}
	}

	cout<<result<<endl;

}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator