Codeforces Round #245 (Div. 2) B. Balls Game 并查集
B. Balls Game
Time Limit: 1 Sec Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/430/problem/B
Description
Iahub is training for the IOI. What is a better way to train than playing a Zuma-like game?
There are n balls put in a row. Each ball is colored in one of k colors. Initially the row doesn't contain three or more contiguous balls with the same color. Iahub has a single ball of color x. He can insert his ball at any position in the row (probably, between two other balls). If at any moment there are three or more contiguous balls of the same color in the row, they are destroyed immediately. This rule is applied multiple times, until there are no more sets of 3 or more contiguous balls of the same color.
For example, if Iahub has the row of balls [black, black, white, white, black, black] and a white ball, he can insert the ball between two white balls. Thus three white balls are destroyed, and then four black balls become contiguous, so all four balls are destroyed. The row will not contain any ball in the end, so Iahub can destroy all 6 balls.
Iahub wants to destroy as many balls as possible. You are given the description of the row of balls, and the color of Iahub's ball. Help Iahub train for the IOI by telling him the maximum number of balls from the row he can destroy.
Input
The first line of input contains three integers: n (1 ≤ n ≤ 100), k (1 ≤ k ≤ 100) and x (1 ≤ x ≤ k). The next line contains n space-separated integers c1, c2, ..., cn (1 ≤ ci ≤ k). Number ci means that the i-th ball in the row has color ci.
It is guaranteed that the initial row of balls will never contain three or more contiguous balls of the same color.
1000000000.
Output
Sample Input
1 1 2 2 1 1
Sample Output
HINT
题意
类似对对碰一样,大于三个的同样颜色的球连在一起就可以消灭
题解:
直接暴力枚举插入,然后用一个并查集维护一下就好啦`
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff; //无限大
const int inf=0x3f3f3f3f;
/* int buf[10];
inline void write(int i) {
int p = 0;if(i == 0) p++;
else while(i) {buf[p++] = i % 10;i /= 10;}
for(int j = p-1; j >=0; j--) putchar('0' + buf[j]);
printf("\n");
}
*/
//**************************************************************************************
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int a[maxn];
int dp[maxn];
int flag[maxn];
int fa[maxn];
int fi(int x)
{
if(x!=fa[x])
fa[x]=fi(fa[x]);
return fa[x];
}
int n,m,q;
int dfs(int x)
{
int ans=;
int l=x+;
int r=fi(x)-;
ans=l-r-;
while()
{
int ll=l,rr=r;
int t=;
for(int i=l;i<=n;i++)
{
if(a[i]==a[i+])
t++;
else
{
l=i;
break;
}
}
for(int i=r;i;i--)
{
if(a[i]==a[i-])
t++;
else
{
r=i;
break;
}
}
if(a[l]!=a[r])
break;
if(t<=)
break;
//cout<<l<<" "<<r<<endl;
ans=l-r+;
//cout<<ans<<endl;
l++;
r--;
}
return ans;
}
int main()
{
cin>>n>>m>>q;
for(int i=;i<=n;i++)
{ fa[i]=i;
cin>>a[i];
if(a[i]==a[i-])
{
dp[i]=dp[i-]+;
fa[i]=fi(i-);
}
else
dp[i]=;
}
/*
for(int i=1;i<=n;i++)
cout<<dp[i]<<" ";
cout<<endl;
*/
int ans=;
for(int i=n;i;i--)
{
if(dp[i]>=&&a[i]==q)
{
//cout<<i<<" 1390183018209"<<endl;
flag[i]=;
ans=max(dfs(i),ans);
i=fi(i);
}
}
cout<<ans<<endl;
}
Codeforces Round #245 (Div. 2) B. Balls Game 并查集的更多相关文章
- Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集
E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...
- Codeforces Round #345 (Div. 2) E. Table Compression 并查集
E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...
- Codeforces Round #603 (Div. 2) D. Secret Passwords 并查集
D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, ...
- Codeforces Round #245 (Div. 2) B - Balls Game
暴利搜索即可 #include <iostream> #include <vector> #include <iostream> using namespace s ...
- Codeforces Round #600 (Div. 2) D题【并查集+思维】
题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...
- Codeforces Round #345 (Div. 2) E. Table Compression 并查集+智商题
E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #600 (Div. 2) - D. Harmonious Graph(并查集)
题意:对于一张图,如果$a$与$b$连通,则对于任意的$c(a<c<b)$都有$a$与$c$连通,则称该图为和谐图,现在给你一张图,问你最少添加多少条边使图变为和谐图. 思路:将一个连通块 ...
- Codeforces Round #345 (Div. 1) C. Table Compression (并查集)
Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorith ...
- Codeforces Round #582 (Div. 3) G. Path Queries (并查集计数)
题意:给你带边权的树,有\(m\)次询问,每次询问有多少点对\((u,v)\)之间简单路径上的最大边权不超过\(q_i\). 题解:真的想不到用最小生成树来写啊.... 我们对边权排序,然后再对询问的 ...
随机推荐
- Struts结果跳转方式(四种result配置)
1.转发(默认转发)
- libuv 一 环境搭建, hello TTY
引言 - 一时心起, libuv linux 搭建 有一天突然想起来想写个动画. 找了一下 ui 库太大. 后面想起以前弄过的 libuv. 但发现 libuv 相关资料也很少. 所以就有了这些内容. ...
- Webcollector应用(一)
webcollector是一个开源的Java网络爬虫框架.最近的爬虫改用java写了,对这一周的工作进行简要总结.对于内部机制了解不深入,主要侧重在应用. 一.环境搭建 需要安装一个webcollec ...
- caffe源码整个训练过程
Caffe源码 Blob protected: shared_ptr<SyncedMemory> data_; shared_ptr<SyncedMemory> diff_; ...
- 模块定义文件.def
一作用 DLL中导出函数的声明有两种方式:一种为在函数声明中加上__declspec(dllexport),这里不再举例说明:另外一种方式是采用模块定义(.def) 文件声明,.def文件为链接器提供 ...
- HDU 3746 Cyclic Nacklace(KMP找循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 题目大意:给你一个字符串,求出将字符串的最少出现两次循环节需要添加的字符数. 解题思路: 这题需 ...
- EasyUi–7.tab和datagrid和iframe的问题
1. 多个tab切换,第2个不显示 动态添加tab Iframe页面的方法 展开 折叠 <script type="text/javascript"> $(functi ...
- jquery li a 样式
jQuery(".CwebtopNavContainer").find("li:last a").css("color","red ...
- function(函数)中的动态参数
我们可向函数传递动态参数,*args,**kwargs,首先我们来看*args,示例如下: 1.show(*args) def show(*args): print(args,type(arg ...
- <c:if></c:if>用法-转载
<c:if test="value ne, eq, lt, gt,...."> 用法 类别 运算符 算术运算符 + . - . * . / (或 div )和 % (或 ...