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

Print a single integer — the maximum number of balls Iahub can destroy.

Sample Input

6 2 2
1 1 2 2 1 1

Sample Output

6

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 并查集的更多相关文章

  1. Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集

    E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...

  2. Codeforces Round #345 (Div. 2) E. Table Compression 并查集

    E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...

  3. 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, ...

  4. Codeforces Round #245 (Div. 2) B - Balls Game

    暴利搜索即可 #include <iostream> #include <vector> #include <iostream> using namespace s ...

  5. Codeforces Round #600 (Div. 2) D题【并查集+思维】

    题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...

  6. 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 ...

  7. Codeforces Round #600 (Div. 2) - D. Harmonious Graph(并查集)

    题意:对于一张图,如果$a$与$b$连通,则对于任意的$c(a<c<b)$都有$a$与$c$连通,则称该图为和谐图,现在给你一张图,问你最少添加多少条边使图变为和谐图. 思路:将一个连通块 ...

  8. 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 ...

  9. Codeforces Round #582 (Div. 3) G. Path Queries (并查集计数)

    题意:给你带边权的树,有\(m\)次询问,每次询问有多少点对\((u,v)\)之间简单路径上的最大边权不超过\(q_i\). 题解:真的想不到用最小生成树来写啊.... 我们对边权排序,然后再对询问的 ...

随机推荐

  1. Android SDK的安装与环境变量的配置

    配置Andriod环境变量前提是要先安装好JAVA环境 1.下载Android SDK,点击安装,放在任意不含空格.特殊符号和中文的路径即可. 2.默认路径安装后,安装完成,开始配置环境变量. 3.打 ...

  2. NASA: SpaceX的猎鹰9号火箭将龙飞船发射到国际空间站

    At 5:42 a.m. EDT Friday, June 29, 2018, SpaceX’s Dragon spacecraft lifts off on a Falcon 9 rocket fr ...

  3. Thinkphp的自定义路由(route.php)

    废话:因为thinkphp的默认路由会导致URL特别长,从而会影响搜索引擎优化.所以就衍生了自定义路由,尽量将URL缩短. 这是默认的路由文件: <?php return [ '__patter ...

  4. 010 JVM类加载

    转自http://www.importnew.com/23742.html 前言 我们知道我们写的程序经过编译后成为了.class文件,.class文件中描述了类的各种信息,最终都需要加载到虚拟机之后 ...

  5. python 异常知识点

    raise from python 在3.0 之后引入了raise from 表达式: raise exception from otherexception 当使用该语法时,第二个表达式指定了另一个 ...

  6. Python模块之pxssh

    pxssh模块用于在python中ssh远程连接,执行命令,返回结果,但注意不支持Windows系统 #!/usr/bin/env python #-*- coding:utf-8 -*- from ...

  7. Freemaker语法

    包含文件 <a href="${latestProduct.url}">${latestProduct.name}</a> 基本语法 ${...}:Free ...

  8. linux 定时执行任务 at atq atrm命令的使用

    1.at命令在指定时刻执行指定的命令序列 at [-V] [-q 队列] [-f 文档名] [-mldbv] 时间 下面对命令中的参数进行说明.-V 将标准版本号打印到标准错误中.-q queue 使 ...

  9. MySQL-开发规范升级版

    一.基础规范 表存储引擎必须使用InnoDB   表字符集默认使用utf8,必要时候使用utf8mb4 解读: (1)通用,无乱码风险,汉字3字节,英文1字节 (2)utf8mb4是utf8的超集,有 ...

  10. C#取色器

    闲来无事,就写了一个取色器.原理其实很简单,只需要两步, 获取鼠标光标的位置, 获取当前鼠标光标的位置的RGB颜色值. 获取鼠标光标的位置: System.Drawing.Point p = Mous ...