CSU1008-Horcrux-栈

B: Horcrux

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:

  1. 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.
  2. 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.

Sample Input

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

Sample Output

6
2

这题就是裸的栈的应用,不过没有火车进站(Uva514)那么裸

#include <iostream>
#include <stack>
#include <utility>
#define N 10100
#define INF 0x3f3f3f3f
#define LL long long
#define mem(a,n) memset(a,n,sizeof(a))
#define fread freopen("in.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int num[2];
int main()
{
    ios::sync_with_stdio(false);
    stack<pair<int,int> > st;
    int n,cur,tempcnt;
    while(cin>>n){
        num[0]=num[1]=0;
        for(int i=0;i<n;++i){
            cin>>cur;
            if(!i){
                st.push(make_pair(cur,1));
            }else if(i%2){
                tempcnt=st.top().second;
                st.pop();
                if(!st.empty()&&cur==st.top().first){
                    tempcnt+=st.top().second;
                    st.pop();
                }
                st.push(make_pair(cur,tempcnt+1));
            }else{
                if(st.top().first==cur){
                    tempcnt=st.top().second;
                    st.pop();
                    st.push(make_pair(cur,tempcnt+1));
                }else{
                    st.push(make_pair(cur,1));
                }
            }
        }
        while(!st.empty()){
            num[st.top().first]+=st.top().second;
            st.pop();
        }
        cout<<num[0]<<endl;
    }
    return 0;
}

/**********************************************************************
    Problem: 1008
    User: CSUzick
    Language: C++
    Result: AC
    Time:20 ms
    Memory:1692 kb
**********************************************************************/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值