1021 sicily Couples出列的问题

Description

N couples are standing in a circle, numbered consecutively clockwise from 1 to 2N. Husband and wife do not always stand together. We remove the couples who stand together until the circle is empty or we can't remove a couple any more.

Can we remove all the couples out of the circle?

今晚随便写了一下,用数组模拟,结果效率比较低,时间超过了要求。

#include <stdlib.h>

#include <iostream>

using namespace std;

#define N 100000

//数组模拟 

int stack[N*2+1];

int couple[N*2];



//每个人存储的是它邻居的编号,最后一个存的是1 

void init(int n)

{

 for(int i=1;i<n*2;i++)

 {

  stack[i]=i+1;

 }

 stack[n*2]=1;

}

int main()

{

 int n;

 while(cin>>n&&n!=0)

 {

	  init(n);

	  int nCount=n;

	  for(int i=0;i<2*n;i++)

	  {

	   	cin>>couple[i];

	  }

	  bool endFlag=true;

	  while(endFlag)

	  {

	   	   int temp=nCount;

		   for(int i=0;i<n;i++)

		   {

			   	if(stack[couple[2*i]]!=0)

				{

					 //表示夫妇,出列 

					 if(stack[couple[2*i]]==couple[2*i+1])

					 {

						  nCount--;

						  if(nCount==0)

						  {

						   cout<<"YES"<<endl;

						   endFlag=false;

						   i=n;

						   continue;

						  }

						  bool flag=true;//表示是否找到 

						  for(int j=couple[2*i];flag;j--)

					  	  {

							   if(stack[j]==couple[2*i])

							   {

							   	stack[j]=stack[couple[2*i+1]];

								stack[couple[2*i]]=stack[couple[2*i+1]]=0;

								flag=false;

							   }

							   if(j==1)

							   {

							   	j=2*n;

							   }

					  	 } 

					 }

				}

		   }

                    //表示没有减少数量 

		  if(temp==nCount)

		  {

			   cout<<"NO"<<endl;

			   endFlag=false;

		  }

	  }

 }

 system("pause");

 return 0;

}

 改用栈结构模拟,但还是超时,可能要使用本身类库的栈结构实现

#include <stdlib.h>

#include <iostream>

#define N 10000

using namespace std;



int stack[N*2];

int top;

//出栈操作 

void pop()

{

 	 top--;

} 

//入栈操作 

void push(int n)

{

 	 stack[++top]=n;

}

//判断栈是否为空 

bool isEmpty()

{

 	 if(top>-1)

	 {

	  	return false;

	 }

	 return true;

}

int main()

{

 	int n;

	while(cin>>n&&n!=0)

	{

	 	int couples[2*N+1];

		int a,b;

		for(int i=0;i<n;i++)

		{

		 	cin>>a>>b;

			couples[a]=b;	

			couples[b]=a;		

		}

//		for(int i=1;i<2*n+1;i++)

//		{

//		 	cout<<i<<"***"<<couples[i]<<"**"<<endl;

//		}

	 	top=-1;

	 	for(int i=1;i<=n*2;i++)

		{

		 	if(!isEmpty()&&couples[i]==stack[top])

			{

			 	//cout<<"pop "<<stack[top]<<endl;

			 	pop();

			}

			else

			{

			 	//cout<<"push "<<i<<endl;

			 	push(i);

			}

		}

		if(isEmpty())

		{

		 	cout<<"Yes"<<endl;

		}

		else

		{

		 	cout<<"No"<<endl;

		}

	}

 	system("pause");

	return 0;

}

 //使用stack本身的类库,确实降低了时间,提高了效率

#include <stdlib.h>

#include <iostream>

#include <stack>

#define N 100000

using namespace std;



int main()

{

    int n;

    int couples[2*N+1];

	int a,b;

    while(cin>>n&&n!=0)

    {

        stack<int> mystack;



        for(int i=0;i<n;i++)

        {

            cin>>a>>b;

            couples[a]=b;   

            couples[b]=a;       

        }

//      for(int i=1;i<2*n+1;i++)

//      {

//          cout<<i<<"***"<<couples[i]<<"**"<<endl;

//      }

        for(int i=1;i<=n*2;i++)

        {

            if(!mystack.empty()&&couples[i]==mystack.top())

            {

                //cout<<"pop "<<stack[top]<<endl;

                mystack.pop();

            }

            else

            {

                //cout<<"push "<<i<<endl;

                mystack.push(i);

            }

        }

        if(mystack.empty())

        {

            cout<<"Yes"<<endl;

        }

        else

        {

            cout<<"No"<<endl;

        }    

    }

    system("pause");

    return 0;

}

 

 

 

 

你可能感兴趣的:(UP)