zoukankan      html  css  js  c++  java
  • CSU1008: 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

    
    
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <map>
    using namespace std;
    const int maxn=100005;
    int n,x;
    struct Node
    {
        int col,num;
    }a[maxn];
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            int cnt=1;
            scanf("%d",&x);
            a[cnt].col=x;a[cnt].num=1;
            for(int i=2;i<=n;i++)
            {
                scanf("%d",&x);
                if(a[cnt].col==x)
                    a[cnt].num++;
                else
                {
                    if(i%2==0)
                    {
                        a[cnt].col=x;
                        a[cnt].num++;
                        if(cnt>1)
                        {
                            a[cnt-1].num+=a[cnt].num;
                            cnt--;
                        }
                    }
                    else
                    {
                        a[++cnt].col=x;
                        a[cnt].num=1;
                    }
                }
            }
            int ans=0;
            for(int i=1;i<=cnt;i++)
            {
                if(a[i].col==0)
                    ans+=a[i].num;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    
    
    /**********************************************************************
    	Problem: 1008
    	User: therang
    	Language: C++
    	Result: AC
    	Time:24 ms
    	Memory:2804 kb
    **********************************************************************/
    
    
    

      

    
    

     

  • 相关阅读:
    敏捷开发之Scrum扫盲篇
    select选项框特效
    jquery 实现 点击按钮后倒计时效果,多用于实现发送手机验证码
    如何使用CSS实现小三角形效果
    轻松学习Ionic (四) 修改应用图标及添加启动画面(更新官方命令行工具自动生成)
    轻松学习Ionic (三) 安装sass并在webstorm中为scss添加watcher
    轻松学习Ionic (二) 为Android项目集成Crosswalk(更新官方命令行工具)
    轻松学习Ionic (一) 搭建开发环境,并创建工程
    cmake时选择的VS生成器
    mysql更改密码
  • 原文地址:https://www.cnblogs.com/jkzr/p/9956660.html
Copyright © 2011-2022 走看看