LianLianKan
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2285 Accepted Submission(s): 701
Problem Description
I like playing game with my friend, although sometimes looks 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 one 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 a wisdom among my friend, 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 6 with it.
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 integer 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
Source
2012 ACM/ICPC Asia Regional Changchun Online
Recommend
liuyiding
1 //31MS 5904K 1563 B C++ 2 /* 3 4 题意: 5 给出一个栈,与栈顶的的元素距离五以内并且值相等的 6 元素可以和栈顶元素一起出栈,判断最后栈可否清空 7 8 状态压缩: 9 其实这题并不难,应为题目要求离栈顶距离为五的元素, 10 故取10位就可以了,应为每匹配掉一个元素,就会有下一个元素 11 进来,最坏情况1000011111,假如是0开头,状态将会左移一位并加一 12 13 dp[i][j]表示深度为i状态为j时的情况,0表示空,1表示非空 14 15 (不能用贪心解法) 16 9 17 1 1 2 1 1 1 1 1 2 18 19 */ 20 #include<stdio.h> 21 #include<string.h> 22 #define N 1200 23 int dp[N][N]; //dp[i][j]表示深度为i时状态j的情况 24 int a[N]; 25 int n; 26 int dfs(int depth,int temp) 27 { 28 if(depth<0){ 29 if(temp==0) return 1; 30 return 0; 31 } 32 if(dp[depth][temp]!=-1) return dp[depth][temp]; 33 int &ret=dp[depth][temp]; 34 ret=0; 35 if(!((1<<9)&temp)){ //如果该位已被选去 36 int ntemp=(temp<<1); 37 if(depth-10>=0) ntemp|=1; 38 ret=dfs(depth-1,ntemp); 39 }else{ 40 int cnt=0; 41 int ntemp=(temp^(1<<9)); //去掉栈顶元素,一点定要清零,不然移位会溢出32位 42 for(int i=1,j=8;i<=9,j>=0;i++,j--){ 43 if((1<<j)&ntemp){ //该位未被选过 44 cnt++; 45 if(cnt>5) break; 46 if(a[depth-i]==a[depth]){ 47 int tmp=(ntemp^(1<<j)); 48 tmp<<=1; 49 if(depth-10>=0) tmp|=1; 50 ret=dfs(depth-1,tmp); 51 if(ret) break; 52 } 53 } 54 } 55 } 56 return ret; 57 } 58 int main(void) 59 { 60 while(scanf("%d",&n)!=EOF) 61 { 62 for(int i=0;i<n;i++) 63 scanf("%d",&a[i]); 64 if(n&1){ 65 puts("0");continue; 66 } 67 memset(dp,-1,sizeof(dp)); 68 int temp=0; //初始状态 69 int j=0; 70 for(int i=n-1;i>=0 && j<=9;i--,j++) 71 temp=(temp<<1)+1; 72 while(j<=9){ 73 temp<<=1;j++; 74 } 75 int ans=dfs(n-1,temp); 76 if(ans) puts("1"); 77 else puts("0"); 78 } 79 return 0; 80 }