csu oj 1008 Horcrux(计数)
题意:有一行黑白棋子,比如 110,当放偶数次时,比如放1 变成1101-> 1111,就会发生翻转!其他情况不发生,问最后有多少个白色棋子?
思路:用一个节点保存相同颜色的个数和颜色!
1101->1(4)
1110->0(4)
#define M 100005
struct soul{
int color,num;
}a[M];
int main()
{
int n,x,cnt;
while(~scanf("%d",&n))
{
cnt=0;
scanf("%d",&x);
a[cnt].color=x;
a[cnt].num=1;
for(int i=2;i<=n;i++)
{
scanf("%d",&x);
if(x==a[cnt].color)
a[cnt].num++;
else
{
if(i&1) //当前位置为奇数
{
cnt++;
a[cnt].color=x;
a[cnt].num=1;
}else
{
a[cnt].num++;
a[cnt].color=x;
if(cnt>0)//只有多于一种颜色,则合并
{
a[cnt-1].num+=a[cnt].num;
cnt--;
}
}
}
// printf("->>cnt=%d,a[i].color=%d\n",cnt,a[cnt].color);
}
x=0;
for(int i=0;i<=cnt;i++)
if(a[i].color==0)
x+=a[i].num;
printf("%d\n",x);
}
return 0;
}
特殊用例:
3
0 0 1
ans: 2
4
0 0 0 1
ans:0