[OJ]1008:
Description
A Horcrux is an object in which a Dark wizard or witch has hidden a fragment of his or her soul for the purpose of attaining immortality. Constructing a Horcrux is considered Dark magic of the foulest, most evil kind, as it violates laws of nature and morality, and requires a horrific act (a.k.a. murder) to accomplish.
There are two kinds of horcruxes, the white one, denoted as △
, and the black one, denoted as ▲
.
Topper has got N horcruxes, and he wants to destroy them to win the
Dark wizard. Toper places all the horcruxes in a line from left to
right, one by one, and then says a magic spell to destroy them. In order
to make the magic spell works, Toper needs to know the number of the
white horcruxes.
Since the horcruxes also has magic, when placing the horcruxes, they will change color from white to black or from black to white under the following rules:
-
When Topper places the i-th horcrux and i is an even number: If the i-th horcrux and the rightmost horcrux have different colors, all consecutive horcruxes of the same color on the right change its color.
- In other situations, no magic works.
For example, suppose the horcruxes on the line are:
△△▲▲△△△
After placing 7 horcruxes.
If the 8-th horcrux is white, since its color and the color of the rightmost horcrux are the same. Therefore, the horcruxes on the line become as follows:
△△▲▲△△△△
If the 8-th horcrux is black, since its color and the color of the rightmost horcrux are different, the 3 consecutive white stones on the right change their color. Therefore, the stones on the line become as follows:
△△▲▲▲▲▲▲
You see, it’s not an easy job to beat the Dark wizard. So write a program to help Topper.
Input
There are some test cases. In each test case, the first line contains a positive integer n (1≤n≤100,000), which is the number of horcruxes. The following n lines denote the sequence in which Topper places the horcruxes. 0 stands for white horcrux, and 1 stands for black horcrux.
Output
For each test case, output one line containing only the number of white horcruxes on the line after Topper places n horcruxes.
#include <stdio.h> typedef struct _Node{ int color; int num; }Node; void main(){ int n, t, whites, i, tmp; Node stack[100000]; int top = -1; while(scanf(" %d", &t) != EOF){ i = 0; top = -1; while(t--){ i++; scanf(" %d", &n); if(top < 0){ stack[++top].color = n; stack[top].num = 1; continue; } if(n != stack[top].color){ if(i%2 == 0){ tmp = stack[top--].num + 1; if(top<0){ top = 0; stack[top].num = tmp; stack[top].color =n; }else{ stack[top].num += tmp; stack[top].color = n; } }else{ stack[++top].color = n; stack[top].num = 1; } }else{ stack[top].num++; } } whites = 0; while(top>=0){ if(stack[top].color == 0){ whites += stack[top].num; } top--; } printf("%d\n", whites); } }
标签: C
日历
最新微语
- 有的时候,会站在分叉路口,不知道向左还是右
2023-12-26 15:34
- 繁花乱开,鸟雀逐风。心自宁静,纷扰不闻。
2023-03-14 09:56
- 对于不可控的事,我们保持乐观,对于可控的事情,我们保持谨慎。
2023-02-09 11:03
- 小时候,
暑假意味着无忧无虑地玩很长一段时间,
节假意味着好吃好喝还有很多长期不见的小朋友来玩...
长大后,
这是女儿第一个暑假,
一个半月...
2022-07-11 08:54
- Watching the autumn leaves falling as you grow older together
2018-10-25 09:45
分类
最新评论
- Goonog
i get it now :) - 萧
@Fluzak:The web host... - Fluzak
Nice blog here! Also... - Albertarive
In my opinion you co... - ChesterHep
What does it plan? - ChesterHep
No, opposite. - mojoheadz
Everything is OK!... - Josephmaigh
I just want to say t... - ChesterHep
What good topic - AnthonyBub
Certainly, never it ...
发表评论: