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

Hint

题意:有一行黑白棋子,比如 110,当放偶数次时,比如放1 变成1101-> 1111,就会发生翻转!其他情况不发生,问最后有多少个白色棋子?
可以用一个节点表示相同颜色的节点的个数
#include<string.h>
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 100010
struct node
{
int color, num;
}a[MAXN];
int main()
{
int n,m;
while (cin >> n)
{
cin >> m;
int cnt = 0;
a[cnt].color = m;
a[cnt].num = 1;
for (int i = 0; i < n-1; i++)
{
cin >> m;
if (m == a[cnt].color)//颜色相同
a[cnt].num++;
else//颜色不同
{
if (i & 1)//为奇数时
{
cnt++;
a[cnt].color = m;
a[cnt].num = 1;
}
else
{
a[cnt].num++;
a[cnt].color = m;
if (cnt > 0)//如果颜色块个数不为零,改变一次颜色则会减少一个颜色块
{
a[cnt - 1].num += a[cnt].num;
cnt--;
}
}
} }
int sum = 0;
for (int i = 0; i <= cnt; i++)
{
if (a[i].color == 0)
sum += a[i].num;
}
cout << sum << endl;
} return 0;
}
/**********************************************************************
Problem: 1008
User: leo6033
Language: C++
Result: AC
Time:36 ms
Memory:2804 kb
**********************************************************************/

CSUOJ 1008 Horcrux的更多相关文章

  1. CSU1008: Horcrux

    Description A Horcrux is an object in which a Dark wizard or witch has hidden a fragment of his or h ...

  2. HDOJ 1008. Elevator 简单模拟水题

    Elevator Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. poj 1008:Maya Calendar(模拟题,玛雅日历转换)

    Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64795   Accepted: 19978 D ...

  4. POJ 1008 Maya Calendar

    链接:http://poj.org/problem?id=1008 Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  5. BZOJ 1008 题解

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 7845  Solved: 3359[Submit][Status] ...

  6. csuoj 1511: 残缺的棋盘

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511 1511: 残缺的棋盘 时间限制: 1 Sec  内存限制: 128 MB 题目描述 输入 ...

  7. HDU 4777 Rabbit Kingdom (2013杭州赛区1008题,预处理,树状数组)

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. 【BZOJ】1008: [HNOI2008]越狱(快速幂)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1008 刚开始看不会做啊,以为是dp,但是数据太大!!!所以一定有log的算法或者O1的算法,,,,还 ...

  9. PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)

    1008. Airline Routes (35) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a ...

随机推荐

  1. SQL - 查询某一字段值相同而另一字段值最大的记录

    有时需要以某一字段作为分组,筛选每一组的另一字段值最大(或最小)的记录.例如,有如下表 app,存储了 app 的 ID.名称.版本号等信息.现在要筛选出每个 app 版本最大的记录. 方法一 SEL ...

  2. ipynb to pdf

    Q: 如何把jupyter notebook 转为 pdf 文档? A: 尝试了几种python包, 结果都没有成功. 包括: xhtml2pdf, 查看官方的介绍说用pandoc也是一种方法, 但是 ...

  3. Python核心编程——Chapter15

    正则表达式在脚本语言里是最重要的一部分,这部分的题目真的不容怠慢. 开始这部分的题目的解答! 15.1识别下列字符串:bat,bit,but,hat,hit和hut. >>> imp ...

  4. Linux系统接入小区宽带

    jollywing(jollywing@foxmail.com) 安装 rp-pppoe 今天去联通营业厅开通了家庭宽带,回到家就搜索怎么用Linux接入小区宽带,发现大多数人都选择用PPPOE拨号上 ...

  5. HDU 1069 Monkey and Banana(最长递减子序列)

    题目链接 题意:摞长方体,给定长方体的长宽高,个数无限制,可随意翻转,要求下面的长方体的长和宽都大于上面的,都不能相等,问最多能摞多高. 题解:个数无限,其实每种形态最多就用一次,把每种形态都单独算一 ...

  6. 30、hashCode方法

    HashCode方法的作用 在HashSet中的元素是不能重复的,jvm可以通过equals方法来判断两个对象是否相同,假设自定义一个Person类里面有10个成员变量,每调用一次equals方法需要 ...

  7. 《区块链100问》第84集:资产代币化之对标黄金Digix

    黄金是避险的不二选择.Digix发行的黄金代币则是数字资产世界里的黄金,其代币简称DGX,能够在数字资产世界中起到避险的作用. DGX如何实现对标黄金呢?它将黄金资产进行了上链(即:区块链)操作.举个 ...

  8. XSS报警机制(前端防火墙:第二篇)

    XSS报警机制(前端防火墙:第二篇) 在第一章结尾的时候我就已经说了,这一章将会更详细的介绍前端防火墙的报警机制及代码.在一章出来后,有人会问为什么不直接防御,而是不防御报警呢.很简单,因为防御的话, ...

  9. SQLServer 学习相关资料整理【转】

    存储过程: SQL Server 存储过程 博客园上的一篇文章,讲解的非常详细,有测试代码,很实用. sqlserver存储过程中执行动态sql语句  The Curse and Blessings ...

  10. EPC摘抄

    S6a MME – HSS 完成用户位置信息的交换和用户签约信息的管理,传送控制面信息 Diameter MME:主要负责信令处理及移动性管理,功能包括:NAS信令及其安全:跟踪区域(Tracking ...