题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4272点击打开链接
LianLianKan
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4208 Accepted Submission(s): 1274
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
给你一个序列 让你把两个相邻不超过6的同样数字消掉 问最后能不能消完
模拟消的过程
一开始怕超时用vector+erase消 发现需要维护的东西太多
后面直接用标记来记录他是不是消掉了 也不会超时
自己造了个样例
12
6 1 3 4 1 1 5 4 5 3 1 6
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int vis[n+1];
for(int i=0;i<=n;i++)
{
vis[i]=0;
}
vector<long long int > s;
map<long long int ,int > mmap;
for(int i=0;i<n;i++)
{
long long int mid;
cin >> mid;
s.push_back(mid);
}
int flag=0;
int check=0;
int num=s.size();
while(1)
{
flag=0;int cnt=0;
int i=0,j=0;
while(j<num)
{
if(vis[j])
{
j++;
continue;
}
if(mmap[s[j]]==0)
{
mmap[s[j]]=j+1;
j++;
if(j-i>=6+cnt)
{
mmap[s[i]]=0;
i++;
}
}
else
{
int mid=s[j];
vis[j]=1;
vis[mmap[mid]-1]=1;
mmap[mid]=0;
flag=1;
cnt+=2;
}
}
check+=cnt;
if(!flag)
break;
}
if(check!=n)
cout << "0" << endl;
else
cout << "1" << endl;
}
}