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

URL:  http://codeforces.com/contest/430/problem/B

                                                       B. Balls Game

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 ofk colors. Initially the row doesn't contain three or more contiguous balls with the same color. Iahub has a single ball of colorx. 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) andx (1 ≤ x ≤ k). The next line containsn space-separated integers c1, c2, ..., cn (1 ≤ ci ≤ k). Numberci means that thei-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

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

Sample test(s)
Input
6 2 2
1 1 2 2 1 1
Output
6
Input
1 1 1
1
Output
0

水题,用穷举法即可

#include <cstdio>
int n, k, x;
int a[110];

int main(){
  int i, l, r, cnt, tmp, ans = 0;
  scanf("%d %d %d", &n, &k, &x);
  for(i = 0; i < n; i++)
      scanf("%d", &a[i]);
  for(i = 0; i < n; i++)
      if(a[i] == x){
          l = r = i; cnt = 0;
          while(a[l] == a[r]) {
              tmp = 2;
              while(l > 0 && a[l-1] == a[l])   l--,tmp++;
              while(r < n-1 && a[r+1] == a[r])   r++,tmp++;
              --l; ++r;
              if(tmp < 3)  break;
              cnt += tmp;
              if(l < 0 || r >= n)  break;
                   }
      if(cnt-1 > ans) ans = cnt-1;
      }
   printf("%d\n", ans);
   return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值