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 () {
flag = ;
if (v.size() <= ) break;/*只有一个元素的肯定不行*/
it1 = v.begin();
for (; it1 != v.end(); it1++) {
for (it2 = it1 + , i = ; i < && it2 != v.end(); i++ , it2++) {
if (*it1 == *it2) {
v.erase(it1);/*这里删除了it1后it2会往回移动一个*/
v.erase(it2-);/*所以由上面的可知这里删除it2-1位置*/
flag = ; 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 = ; i < n; i++) {
scanf("%d", &tmp);
v.push_back(tmp);/*全部插入vector*/
}
solve();
}
return ;
}

LianLianKan[HDU4272]的更多相关文章

  1. HDU-4272 LianLianKan

    http://acm.hdu.edu.cn/showproblem.php?pid=4272 据说是状态压缩,+dfs什么什么的,可我这样也过了,什么算法都是浮云 ,暴力才是王道.我也归类为状态压缩, ...

  2. hdoj 4272 LianLianKan 数据太水

    LianLianKan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. hdu 4272 LianLianKan 状态压缩

      LianLianKan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  4. LianLianKan - HDU 4272(状态压缩)

    题目大意:有一列数据,可以从最上面的开始连接下面相同的元素,然后消除,不过距离不能超过6,询问最后能不能消除完整个数列. 分析:首先讨论一点最远能消除的地方,比如点的位置是x,如若想要消除x+1位置处 ...

  5. HDU 4272 LianLianKan (状压DP+DFS)题解

    思路: 用状压DP+DFS遍历查找是否可行.假设一个数为x,那么他最远可以消去的点为x+9,因为x+1~x+4都能被他前面的点消去,所以我们将2进制的范围设为2^10,用0表示已经消去,1表示没有消去 ...

  6. LianLianKan HDU - 4272 状压dp

    题意:长度为n(n<=1000)的栈,栈顶元素可以与下面1~5个数中相同的元素消去,问最后能都完全消去. 题解: 比如这个序列12345678910112这个位置的最远可匹配位置能到11为什么呢 ...

  7. HDU 4272 LianLianKan(状压DP)题解

    题意:一个栈,每次可以选择和栈顶一样的数字,并且和栈顶距离小于6,然后同时消去他们,问能不能把所有的数消去 思路:一个数字最远能消去和他相距9的数,因为中间4个可以被他上面的消去.因为还要判断栈顶有没 ...

  8. HDU4272LianLianKan(dfs)

    Problem Description I like playing game with my friend, although sometimes looks pretty naive. Today ...

  9. JavaScript编写连连看

    这几天写题比较少,做了一下网页设计的期末大作业.搞了个连连看,核心代码和hdu 1175那个题目一样. 越来越觉得学ACM是十分有用的,软件的核心是数据结构和算法,学会了DFS,连连看就水到渠成了. ...

随机推荐

  1. 利用LineRenderer画出一个圆,类似于lol中的攻击范围

    http://www.unity蛮牛.com/blog-5945-1409.html 本人大四狗,学unity半年有余,写此文章纯粹记录自己的心得. 废话不多说,进入主题.... 效果如图: 首先要理 ...

  2. ios5之后arc的问题

    原创: 自从ios5以后, apple官方引进了ARC (automaically reference count 自动索引计数) 这个新特性, 简单的说就是对象设置为nil(空引用)之后, 当自动索 ...

  3. (转)WPF控件开源资源

    (转)WPF控件开源资源 Textbox Drag/Drop in WPFhttp://www.codeproject.com/Articles/42696/Textbox-Drag-Drop-in- ...

  4. makefile文件编写

    文件转载自:http://www.cppblog.com/lapcca/archive/2010/11/26/134714.html 下面这篇文章讲的很清楚,基本的用法也很简单.   一.Makefi ...

  5. sc 与net命令的区别

    windows服务操作命令有sc和net 两个命令; sc stop serviceName  sc start serviceName net stop serviceName  net start ...

  6. C标签判断两个值是否相等

    c标签判断两个值是否相等 Integer用:${user1.id eq user2.id}:int用:${user1.id == user2.id} 测试代码如下:<c:if test=&quo ...

  7. angularjs 指令(directive)详解(1)

    原文地址 什么是directive?我们先来看一下官方的解释: At a high level, directives are markers on a DOM element (such as an ...

  8. Java操作Session与Cookie

    1,Java操作Session Java操作Session非常简单,步骤如下 1.1,在servlet中通过request获取session HttpSession session = request ...

  9. [Android Pro] PackageManager#getPackageSizeInfo (hide)

    referce to : http://www.baidufe.com/item/8786bc2e95a042320bef.html 计算Android App所占用d的手机内存(RAM)大小.App ...

  10. php 面向对象要点汇总

    //类和对象//对象:一切东西都可以看做对象,对象是类的实例化.//类:类是对象的抽象,用来描述众多对象共有的特征. //定义类 class//成员变量 和 成员方法//访问修饰符 public共有的 ...