CSU-1008-Horcrux

来源:互联网 发布:软件项目管理内容 编辑:程序博客网 时间:2024/06/05 13:47

Description

A Horcrux is an object in which a Dark wizard or witch hashidden 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, denotedas. Topper has got N horcruxes, and he wants to destroy them towin the Dark wizard. Toper places all the horcruxes in a line from left toright, one by one, and then says a magic spell to destroy them. In order tomake the magic spell works, Toper needs to know the number of the whitehorcruxes.

Since the horcruxes also has magic, when placing the horcruxes,they will change color from white to black or from black to white under thefollowing rules:

1.     When Topper places the i-th horcrux and i is an even number: Ifthe i-th horcrux and the rightmost horcrux have different colors, allconsecutive horcruxes of the same color on the right change its color.

  1. 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 ofthe rightmost horcrux are the same. Therefore, the horcruxes on the line becomeas follows:

△△▲▲△△△

If the 8-th horcrux is black, since its color and the color ofthe rightmost horcrux are different, the 3 consecutive white stones on the rightchange 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 writea program to help Topper.

Input

There are some test cases. In each test case, the first linecontains 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 numberof white horcruxes on the line after Topper places n horcruxes.

SampleInput

8
1
0
1
1
0
0
0
0
8
1
0
1
1
0
0
0
1

SampleOutput

6
2

 

 

Thinking

都写在注释里了。

很简单的题,最开始打算的是,遇到一个满足条件的,向前循环加数字,可是超时了。

然后才考虑用结构体同时记录当前的颜色,和与之前相同的个数。这个方法并不是那么好,但是也勉强AC了哈。

 

 

AC代码

#include<iostream>

using namespace std;

struct ttt

{

         intyanse,qiantongshu;

}b[100005];

int main()

{

         int N;

         while(cin>>N)

         {

                  intzong=0;

                  //由于要比较前后,先单独输入第一个值

                  cin>>b[0].yanse;

                  b[0].qiantongshu=0;

                  if(b[0].yanse==0)zong++;

                  //万一只有一个值的情况

                  if(N==1)

                  {

                          cout<<zong<<endl;

                          continue;

                  }

                  //输入后面的值

                  for(inti=1;i<N;i++)

                  {

                          cin>>b[i].yanse;

                          //如果与前面的相同,

                          if(b[i].yanse==b[i-1].yanse)

                          {

                                   b[i].qiantongshu=b[i-1].qiantongshu+1;//前面相同的数字加1       

                                   if(b[i].yanse==0)zong++;continue;

                          }

                          //如果与前面的不相同

                          else

                          {

                                   b[i].qiantongshu=0;//一开始前同数赋值为0

                                   if(b[i].yanse==0)zong++;

                                   if((i+1)%2==0)//如果满足魔法条件

                                   {

                                   //总数加或减前一个的前同数

                                   if(b[i].yanse==0)zong=zong+b[i-1].qiantongshu+1;

                                   elsezong=zong-b[i-1].qiantongshu-1;

                                   //然后再改变当前这个数的前同数

                                   intkkk;

                                   if(i-b[i-1].qiantongshu-2<0)kkk=0;//如果是在i较小时,可能出现b[-1],这里单独拿出来说

                                   elsekkk=1;

                                   b[i].qiantongshu=(b[i-1].qiantongshu+1)+kkk*(b[i-b[i-1].qiantongshu-2].qiantongshu+1);

                                  

                                   }

                          }

                  }

                  //for(inti=0;i<N;i++)

                  //{

                  //       cout<<b[i].qiantongshu<<"";

                  //}

                  //cout<<endl;

                  cout<<zong<<endl;

                  //cout<<endl;

         }

         return0;

}

0 0
原创粉丝点击