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,连连看就水到渠成了. ...
随机推荐
- Android 将可以按地点自动启动应用程序
导读Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发.尚未有统一中文名称,中国大陆地区较多人使用“安 ...
- onfiguration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/security]
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Una ...
- HDOj 1010 DFS优化
#include<cstdio> #include<cstring> ]={,,,-}; ]={,,-,}; ][]; int x1,y1,x2,y2; int step; i ...
- 学习jquery mobile
学习jquery mobile的时间不是很长,在学习的过程当中也遇到了很多令人抓狂的问题,在网上搜索问题答案的时候发现,现在关于jquery mobile的文章还不是很多,所以,我也是一边学习,一边摸 ...
- lambda 表达式
C++0x 的语法还是比较简单.下面通过几个例子来介绍下. 先是通过 std::for_each 演示一个简单的例子: ? std::for_each 和 lambda 1 2 3 4 5 6 7 8 ...
- [Android Pro] RecyclerView实现瀑布流效果(二)
referece to : http://blog.csdn.net/u010687392 在上篇中我们知道RecyclerView中默认给我们提供了三种布局管理器,分别是LinearLayoutMa ...
- CSS备忘
垂直居中: 先height再ling-height,都设成一样高 span 垂直居中:vertical-align:50%设置外边距: margin-top,m ...
- 昂贵的聘礼(poj 1062)
Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...
- Ajax中的XMLHttpRequest对象详解
XMLHttpRequest对象是Ajax技术的核心.在Internet Explorer 5中,XMLHttpRequest对象以ActiveX对象引入,被称之为XMLHTTP,它是一种支持异步请求 ...
- (七)STM32的RTC简单操作
简单说明: /********************************************************************************************* ...