zoukankan      html  css  js  c++  java
  • 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.

     

                                        
    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
    ");  
        else printf("1
    ");  
    }  
      
    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;  
    }  

     

  • 相关阅读:
    【java学习笔记】反射基础
    【java学习笔记】线程
    【java学习笔记】Properties
    【java学习笔记】序列化、反序列化
    Java Review (二十六、集合----- Set 集合)
    Java Review (二十五、集合----- Iterator接口)
    Java Review (二十四、集合-----Collection 接口)
    Java Review (二十三、集合-----概述)
    Java Review (二十二、正则表达式)
    Java Review (二十一、基础类库----日期、时间类)
  • 原文地址:https://www.cnblogs.com/dramstadt/p/3200007.html
Copyright © 2011-2022 走看看