LianLianKan

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 243    Accepted Submission(s): 123


Problem Description
I like playing game with my friends, although sometimes look pretty naive. Today I invent a new game called LianLianKan. The game is about playing on a number stack.
Now we have a number stack, and we should link and pop the same element pairs from top to bottom. Each time, you can just link the top element with the same-value element. After pop them from stack, all left elements will fall down. Although the game seems to be interesting, it's really naive indeed.



To prove I am wisdom among my friends, I add an additional rule to the game: for each top element, it can just link with the same-value element whose distance is less than 5.
Before the game, I want to check whether I have a solution to pop all elements in the stack.
 

 

Input
There are multiple test cases.
The first line is an integer N indicating the number of elements in the stack initially. (1 <= N <= 1000)
The next line contains N integers ai indicating the elements from bottom to top. (0 <= ai <= 2,000,000,000)
 

 

Output
For each test case, output “1” if I can pop all elements; otherwise output “0”.
 

 

Sample Input
2 1 1 3 1 1 1 2 1000000 1
 

 

Sample Output
1 0 0
代码:

#include<stdio.h>
#include<string.h>
__int64 num[1005];
int visit[1005];
int n;
 
int fun()
{
  for(int i=1;i<=n;i++)
  {
     if(!visit[i])
  {
   int k=2;     
       for(int j=i+1;j<=n&&k<=5;j++)
       {
       if(num[i]==num[j])
    {
      visit[i]=visit[j]=1;
      break;    
       }    
    }
    if(!visit[i])
    return 0;
  }    
  } 
  return 1;
}

int main()
{
   while(scanf("%d",&n)!=-1&&n)
   {
      memset(visit,0,sizeof(visit));
   for(int i=1;i<=n;i++)
    scanf("%I64d",&num[i]);
    if(n%2==0&&fun())
    printf("1\n");
    else
    printf("0\n");        
   }  
   return 0;
}

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4272