LianLianKan
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 840 Accepted Submission (s): 280
Problem DescriptionI 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 shoshould 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.
InputThere 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)
OutputFor each test case, output "1" if I can pop all elements; otherwise output "0 ".
Sample Input2 1 1 3 1 1 1 2 1000000 1
Sample Output1 0 0
Source2012 ACM/ICPC Asia Regional Changchun Online
Recommendliuyiding is directly violent to DFS. Very simple... Pay attention to the details. We also need to use map to determine whether or not it will time out. I have heard of a difficult question... It seems that the distance between 5 and 6 can be AC.
# Include <stdio. h> # include <string. h >#include <iostream >#include <map> # include <algorithm> using namespace std; const int MAXN = 1010; int a [MAXN]; bool used [MAXN]; int dfs (int n) {while (n> 0 & used [n]) n --; if (n = 0) return 1; if (n = 1) return 0; int I = 0; int j = n-1; for (; I <= 5 ;) // here, both I <5 and I <= 5 can be AC {if (j <= 0) return 0; // no equal if (used [j]) is found. {j --; continue;} if (a [n] = a [j]) {used [j] = true; if (dfs (n-1) return 1; used [j] = False;} I ++; j --;} return 0;} map <int, int> mp; int main () {int n; while (scanf ("% d ", & n )! = EOF) {mp. clear (); for (int I = 1; I <= n; I ++) {scanf ("% d", & a [I]); used [I] = false; mp [a [I] ++;} if (n & 1) {printf ("0 \ n"); continue ;} int t = 1; // Add a map to judge whether it is 0 ms; otherwise, it is TLE map <int, int>: iterator it; for (it = mp. begin (); it! = Mp. end (); it ++) {if (it-> second) % 2 = 1) {t = 0; break ;}} if (t = 0) {printf ("0 \ n"); continue;} printf ("% d \ n", dfs (n);} return 0 ;}