2012 ACM/ICPC Asia Regional Changchun Online

1006

LianLianKan

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


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


英語爛也有好處啊,less than 是小於,一直以爲是小於等於。一不小心就ac了,rp吼吼,簡單貪心模擬水水的過了,貌似後來看了下還是偶們學校第一個ac的吼吼,rp萬歲

然後剩下四個半小時一題沒做出來....實力不濟(Y-Y)

#include<stdio.h>
#include<string.h>
#define begin freopen("a.in","r",stdin); freopen("a.out","w",stdout);
#define end fclose(stdin); fclose(stdout);
int a[1010],b[1010];
int main()
{
 int i,n,j,dis,top,f;
 while (scanf("%d",&n)!=EOF)
 {
 memset(b,0,sizeof(b));
 for (i=1;i<=n;i++)
 {
  scanf("%d",&a[i]);
  b[i]=1;
 }
 if (n%2) printf("0\n");
 else
 {
 top=1; f=0;
 while (top<n)
 {
  for (i=top;i<=n;i++)
  if (b[i]==1)
  {
   j=i; dis=0;
   while (dis<=5&&j<=n)
   {
    if (b[j+1]==1)
    {
     ++dis;
     if (a[i]==a[j+1]&&dis<5) {b[i]=0;b[j+1]=0;f=1;}
    }
    ++j;
    if (f) break;
   }
  }
  if (f) f=0;
    else  break;
  while (b[top]==0&&top<=n) ++top;
 }
 f=1;
 for (i=1;i<=n;i++)
 if (b[i]){f=0; break;}
 printf("%d\n",f);
 }
 }
 return 0;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章