LianLianKan
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2577 Accepted Submission(s): 802
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.
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.
![](https://i-blog.csdnimg.cn/blog_migrate/791b82e30179bc1b498d58e6ad7733d3.jpeg)
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)
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
Recommend
网上有一种一遍过的超简单代码。但是根据题意
8
5 1 2 3 3 2 1 5
这组数据是输出0的。一遍过的神代码是输出1。只能说数据真的水了。
其实我这种暴搜的方法,估计也是数据水了才能过。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1010];
int vis[1010];
int b[1010];
int dfs(int x)
{
while(x>0 && vis[x]) x--;
if(x==0) return 1;
if(x==1) return 0;
int sum=0;
int j=x-1;
while(sum<=5)
{
if(j<=0) return 0;
if(vis[j])
{
j--;
continue;
}
if(a[x]==a[j])
{
vis[j]=1;
if(dfs(x-1)) return 1;
vis[j]=0;
}
sum++;
j--;
}
return 0;
}
int main()
{
int n;
int i,j;
while(cin>>n)
{
for(i=1;i<=n;i++)
{
cin>>a[i];
b[i-1]=a[i];
}
sort(b,b+n);
memset(vis,0,sizeof(vis));
int flag1=1;
int aa=b[0];
int ans=1;
for(i=1;i<=n;i++)
{
if(aa==b[i]) ans++;
else
{
if(ans%2) {flag1=0;break;}
ans=1;
aa=b[i];
}
}
// cout<<"f"<<flag1<<endl;
if(!flag1) cout<<"0"<<endl;
else
{
if(dfs(n))
cout<<"1"<<endl;
else
cout<<"0"<<endl;
}
}
return 0;
}