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,连连看就水到渠成了. ...
随机推荐
- windows下的文件到linux下乱码 iconv 修改文件编码
conv [选项...] [文件...] 有如下选项可用: 输入/输出格式规范:-f, --from-code=名称 原始文本编码-t, --to-code=名称 输出编码 信息:-l, --list ...
- [POJ1157]LITTLE SHOP OF FLOWERS
[POJ1157]LITTLE SHOP OF FLOWERS 试题描述 You want to arrange the window of your flower shop in a most pl ...
- 台大《机器学习基石》课程感受和总结---Part 1(转)
期末终于过去了,看看别人的总结:http://blog.sina.com.cn/s/blog_641289eb0101dynu.html 接触机器学习也有几年了,不过仍然只是个菜鸟,当初接触的时候英文 ...
- meanshift和camshift
参考:http://www.cnblogs.com/tornadomeet/archive/2012/03/15/2398769.html 照着这位大神的代码运行了一下,发现meanshift的跟踪效 ...
- Linux lsof详解
简介 lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以如传输控 ...
- 能用Shell就别编程-海量文本型数据的处理
对于txt文本类数据,优先采用shell脚本,实在不行才用Python,Java,MySQL 1) Shell命令行或脚本的处理速度极快,比Java快得多. 2) Shell代码量少,几个命令就能完成 ...
- android文章学习 侧滑菜单实现
http://blog.csdn.net/jj120522/article/details/8075249 http://blog.csdn.net/lilybaobei/article/detail ...
- php获取网页内容方法总结
抓取到的内容在通过正则表达式做一下过滤就得到了你想要的内容,至于如何用正则表达式过滤,在这里就不做介绍了,有兴趣的,以下就是几种常用的用php抓取网页中的内容的方法. 1.file_get_conte ...
- datetime中strftime和strptime用法
from datetime import * format = "%Y-%m-%d %H:%M:%S" a=datetime.now() day=a.day b=a.replace ...
- js指定标签的id只能添加不能删除
<body> <form id="form1" runat="server"> <div> <input id=&qu ...