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 () {
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]的更多相关文章
- HDU-4272           LianLianKan
		http://acm.hdu.edu.cn/showproblem.php?pid=4272 据说是状态压缩,+dfs什么什么的,可我这样也过了,什么算法都是浮云 ,暴力才是王道.我也归类为状态压缩, ... 
- hdoj 4272 LianLianKan 数据太水
		LianLianKan Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ... 
- hdu 4272 LianLianKan 状态压缩
		LianLianKan Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ... 
- LianLianKan - HDU 4272(状态压缩)
		题目大意:有一列数据,可以从最上面的开始连接下面相同的元素,然后消除,不过距离不能超过6,询问最后能不能消除完整个数列. 分析:首先讨论一点最远能消除的地方,比如点的位置是x,如若想要消除x+1位置处 ... 
- HDU 4272 LianLianKan (状压DP+DFS)题解
		思路: 用状压DP+DFS遍历查找是否可行.假设一个数为x,那么他最远可以消去的点为x+9,因为x+1~x+4都能被他前面的点消去,所以我们将2进制的范围设为2^10,用0表示已经消去,1表示没有消去 ... 
- LianLianKan HDU - 4272  状压dp
		题意:长度为n(n<=1000)的栈,栈顶元素可以与下面1~5个数中相同的元素消去,问最后能都完全消去. 题解: 比如这个序列12345678910112这个位置的最远可匹配位置能到11为什么呢 ... 
- HDU 4272 LianLianKan(状压DP)题解
		题意:一个栈,每次可以选择和栈顶一样的数字,并且和栈顶距离小于6,然后同时消去他们,问能不能把所有的数消去 思路:一个数字最远能消去和他相距9的数,因为中间4个可以被他上面的消去.因为还要判断栈顶有没 ... 
- HDU4272LianLianKan(dfs)
		Problem Description I like playing game with my friend, although sometimes looks pretty naive. Today ... 
- JavaScript编写连连看
		这几天写题比较少,做了一下网页设计的期末大作业.搞了个连连看,核心代码和hdu 1175那个题目一样. 越来越觉得学ACM是十分有用的,软件的核心是数据结构和算法,学会了DFS,连连看就水到渠成了. ... 
随机推荐
- lvs之dr技术的学习与实践
			1.配置测试环境 修改IP windows 200.168.10.4 lvs server ip:200.168.10.1 因为IP隧道模式只需要一个网卡 所以就停掉其他网卡 web server ... 
- 最近win7更新后出现第二次打开IDE(delphi2007)的时候提示无法打开"EditorLineEnds.ttr"这个文件
			kb2982791 - 2014年8月12日更新 - http://support.microsoft.com/kb/2982791kb2970228 - 2014年8月12日更新 - http:// ... 
- poj1182(食物链)续
			意 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用 ... 
- Balanced Teams (USACO Jan Bronze 2014)
			既然是bronze,毫无压力的AC了. 就是个深搜,当然加个剪枝--最后一个组不用搜. 恩可以一个一个组分层次dfs,这样会跑得飞起~~也不容易错 #include <cstdio> in ... 
- 【消息队列MQ】各类MQ比较
			目录(?)[-] RabbitMQ Redis ZeroMQ ActiveMQ JafkaKafka 目前业界有很多MQ产品,我们作如下对比: RabbitMQ 是使用Erlang编写的一个开源的消息 ... 
- Maximum Gap
			Given an unsorted array, find the maximum difference between the successive elements in its sorted f ... 
- swift init继承问题
			当在子类的 designated init方法中不手动调用 父类的 designated init方法时,如果父类有不接受任何参数的init,那么系统会自动调用它,编译器不会报错.但是如果父类中没有不 ... 
- Android 中的Resource
			Android与ios相比,各种各样Resource算个独特之处.详情请参见官网Resource Types Resource有许多种,常见的有图像资源,布局资源,等等.每一种资源的位置都是固定的,这 ... 
- (转)SQL SERVER的锁机制(三)——概述(锁与事务隔离级别)
			五.锁与事务隔离级别 事务隔离级别简单的说,就是当激活事务时,控制事务内因SQL语句产生的锁定需要保留多入,影响范围多大,以防止多人访问时,在事务内发生数据查询的错误.设置事务隔离级别将影响整条连接. ... 
- Java for LeetCode 047 Permutations II
			Given a collection of numbers that might contain duplicates, return all possible unique permutations ... 
