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. ab的使用方法【转】

    使用方法 ab -n 800 -c 800  http://192.168.0.10/ (-n发出800个请求,-c模拟800并发,相当800人同时访问,后面是测试url) ab -t 60 -c 1 ...

  2. curl: (6) Couldn’t resolve host ‘www.ttlsa.com’【转】

    上周, 部分站点出现Couldn't resolve host.....问题,  导致公司所有走api的程序都无法正常使用(系统redhat 6.3的都出现问题, redhat 5一切OK). 最后解 ...

  3. 338.Counting Bits---位运算---《剑指offer》32

    题目链接:https://leetcode.com/problems/counting-bits/description/ 题目大意:求解从0到num的所有数的二进制表示中所有1的个数. 法一:暴力解 ...

  4. ubuntu遇到的 the system is runing low-graphics mode 问题

    不知道修改了什么,然后开机显示the system is runing low-graphics mode 看过博客使用如下方法成功进入系统,但是显示分辨率很低,显示 built-in display ...

  5. Loadrunner下WebTours系统自带的用户名和密码

    打开:http://127.0.0.1:1080/WebTours/ 系统默认自带两个用户名和密码,位于~\WebTours\MercuryWebTours\users: 1.用户名:joe,密码:y ...

  6. 清理oracle的用户中的日志垃圾以及修改sys用户的密码

    清理oracle的用户中的日志垃圾1.进入:/opt/oracle/product/11g/network/admin目录2.注释掉listener.ora文件中的TRACE_LEVEL_LISTEN ...

  7. .Net Core 部署到 CentOS7 64 位系统中的步骤

    建议使用 root 管理员账户操作 1.安装工具 1.apache 2..Net Core(dotnet-sdk-2.0) 3.Supervisor(进程管理工具,目的是服务器一开机就启动服务器 上发 ...

  8. Photon3Unity3D.dll 解析一

    IPhotonPeerListener  Photon客户端回调接口 1: //只要有来自Photon Server的事件就触发 2: public virtual void OnEvent( Eve ...

  9. BootStarp的form表单的基本写法

    代码如下: <!DOCTYPE html> <html> <head> <title>BootStrap的基础入门</title> < ...

  10. spring源码解析--事务篇(前篇)

    对于每一个JAVA程序员,spring应该是再熟悉不过的框架了,它的功能有多强大我就不多说了,既然他有这么强大的功能,是如何实现的呢?这个就需要从他的原理去了解,而最直接了解原理的方式莫过于源码.当然 ...