LianLianKan[HDU4272]

LianLianKan

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

 

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.

 

                                     LianLianKan[HDU4272]
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

想了半天不知咋办,在网上搜了一下原来这样也可以...

#include <algorithm>     

#include <iostream>    

#include <cstring>   

#include <cstdio>   

#include <vector>   

using namespace std;  

#define MAXN 1010      

#define INF 0xFFFFFFF   

  

int n;  

vector<int>v;  

  

void solve() {  

    int flag, i;  

    vector<int>::iterator it1, it2;  

    while (1) {  

        flag = 0;  

        if (v.size() <= 1) break;/*只有一个元素的肯定不行*/  

        it1 = v.begin();  

        for (; it1 != v.end(); it1++) {  

            for (it2 = it1 + 1, i = 1; i < 6 && it2 != v.end(); i++ , it2++) {  

                if (*it1 == *it2) {  

                    v.erase(it1);/*这里删除了it1后it2会往回移动一个*/  

                    v.erase(it2-1);/*所以由上面的可知这里删除it2-1位置*/  

                    flag = 1 ; break;  

                }  

            }  

            if (flag) break;  

        }  

        if (!flag || !v.size()) break;/*如果flag为0或v为空退出*/  

    }  

    if (v.size()) printf("0\n");  

    else printf("1\n");  

}  

  

int main() {  

    //freopen("input.txt", "r", stdin);   

    int tmp;  

    while (scanf("%d", &n) != EOF) {  

        v.clear();  

        for (int i = 0; i < n; i++) {  

            scanf("%d", &tmp);  

            v.push_back(tmp);/*全部插入vector*/  

        }  

        solve();  

    }  

    return 0;  

}  

 

你可能感兴趣的:(HDU)