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/BDescription
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.
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=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';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=0; int l=x+1; int r=fi(x)-1; ans=l-r-1; while(1) { int ll=l,rr=r; int t=2; for(int i=l;i<=n;i++) { if(a[i]==a[i+1]) t++; else { l=i; break; } } for(int i=r;i;i--) { if(a[i]==a[i-1]) t++; else { r=i; break; } } if(a[l]!=a[r]) break; if(t<=2) break; //cout<<l<<" "<<r<<endl; ans=l-r+1; //cout<<ans<<endl; l++; r--; } return ans; } int main() { cin>>n>>m>>q; for(int i=1;i<=n;i++) { fa[i]=i; cin>>a[i]; if(a[i]==a[i-1]) { dp[i]=dp[i-1]+1; fa[i]=fi(i-1); } else dp[i]=1; } /* for(int i=1;i<=n;i++) cout<<dp[i]<<" "; cout<<endl; */ int ans=0; for(int i=n;i;i--) { if(dp[i]>=2&&a[i]==q) { //cout<<i<<" 1390183018209"<<endl; flag[i]=1; ans=max(dfs(i),ans); i=fi(i); } } cout<<ans<<endl; }