Codeforces 430 B. Balls Game
2014-05-13 11:03
295 查看
暴力匹配...
B. Balls Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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
Print a single integer — the maximum number of balls Iahub can destroy.
Sample test(s)
input
output
input
output
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int Stack[110],top;
int n,k,c;
int a[110];
int doit(int x)
{
int ans=0,nt=top;
if(Stack[top]!=x)
{
for(int i=top;i>0;i--)
{
if(Stack[i]==Stack[top]) ans++,nt=i;
else break;
}
}
if(ans<3)
{
Stack[++top]=x;
ans=0;
}
else
{
top=nt;
Stack[top]=x;
}
return ans;
}
int ck(int pos,int x)
{
int ret=0;
top=0;
for(int i=0;i<n;i++)
{
ret+=doit(a[i]);
if(i==pos)
{
ret+=doit(x);
}
}
ret+=doit(999);
return ret;
}
int main()
{
scanf("%d%d%d",&n,&k,&c);
for(int i=0;i<n;i++)
{
scanf("%d",a+i);
}
int ans=0;
for(int i=0;i<n-1;i++)
{
if(a[i]==a[i+1]&&a[i]==c)
ans=max(ans,ck(i,c)-1);
}
printf("%d\n",ans);
return 0;
}
B. Balls Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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
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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int Stack[110],top;
int n,k,c;
int a[110];
int doit(int x)
{
int ans=0,nt=top;
if(Stack[top]!=x)
{
for(int i=top;i>0;i--)
{
if(Stack[i]==Stack[top]) ans++,nt=i;
else break;
}
}
if(ans<3)
{
Stack[++top]=x;
ans=0;
}
else
{
top=nt;
Stack[top]=x;
}
return ans;
}
int ck(int pos,int x)
{
int ret=0;
top=0;
for(int i=0;i<n;i++)
{
ret+=doit(a[i]);
if(i==pos)
{
ret+=doit(x);
}
}
ret+=doit(999);
return ret;
}
int main()
{
scanf("%d%d%d",&n,&k,&c);
for(int i=0;i<n;i++)
{
scanf("%d",a+i);
}
int ans=0;
for(int i=0;i<n-1;i++)
{
if(a[i]==a[i+1]&&a[i]==c)
ans=max(ans,ck(i,c)-1);
}
printf("%d\n",ans);
return 0;
}
相关文章推荐
- Codeforces 430 A. Points and Segments (easy)
- codeforces 430 A Points and Segments (easy)
- CodeForces 824D Round#430 Div2D:Vitya and Strange Lesson :01字典树
- Codeforces 300C Beautiful Numbers lucas求组合数逆元
- CodeForces 483B Friends and Presents(二分+容斥定理)
- CodeForces 427D-Match & Catch
- CodeForces 742C Arpa's loud Owf and Mehrdad's evil plan
- codeforces 814D An overnight dance in discotheque(贪心)
- codeforces 120F Spiders
- Codeforces 455C Civilization 树的直径+并查集
- CodeForces 44D-C - Hyperdrive-暴力
- codeforces 241 E Flights
- 【codeforces 246D Colorful Graph 】+ set
- Codeforces 822 D. My pretty girl Noora(多项式加法+dp)
- CodeForces - 219D Choosing Capital for Treeland【树形dp*好题】
- 【CodeForces】455D Serega and Fun 双向链表分块暴力
- Codeforces 592A. PawnChess
- CodeForces 474A Keyboard (水题)
- CodeForces 739C Alyona and towers
- CodeForces 367B Sereja ans Anagrams