CSUOJ 1008 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:
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.
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
#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的更多相关文章
- CSU1008: Horcrux
Description A Horcrux is an object in which a Dark wizard or witch has hidden a fragment of his or h ...
- HDOJ 1008. Elevator 简单模拟水题
Elevator Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- poj 1008:Maya Calendar(模拟题,玛雅日历转换)
Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64795 Accepted: 19978 D ...
- POJ 1008 Maya Calendar
链接:http://poj.org/problem?id=1008 Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- BZOJ 1008 题解
1008: [HNOI2008]越狱 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 7845 Solved: 3359[Submit][Status] ...
- csuoj 1511: 残缺的棋盘
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511 1511: 残缺的棋盘 时间限制: 1 Sec 内存限制: 128 MB 题目描述 输入 ...
- HDU 4777 Rabbit Kingdom (2013杭州赛区1008题,预处理,树状数组)
Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 【BZOJ】1008: [HNOI2008]越狱(快速幂)
http://www.lydsy.com/JudgeOnline/problem.php?id=1008 刚开始看不会做啊,以为是dp,但是数据太大!!!所以一定有log的算法或者O1的算法,,,,还 ...
- PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)
1008. Airline Routes (35) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a ...
随机推荐
- Java并发编程原理与实战二十:线程安全性问题简单总结
一.出现线程安全性问题的条件 •在多线程的环境下 •必须有共享资源 •对共享资源进行非原子性操作 二.解决线程安全性问题的途径 •synchronized (偏向锁,轻量级锁,重量级锁) •vol ...
- Windows服务的安装和卸载
## install %SystemRoot%\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe ***Cache.WinService.exe # ...
- 基于Ubuntu搭建GMS测试环境
一.版本信息: 系统版本:Ubuntu 18.04.2 LTS JDK版本: 1.8.0_171 SDK版本:android-sdk_r24.4.1-linux.tgz ADB版本:1.0.40 ap ...
- 网页中嵌入swf文件的几种方法
1. object + embed 传统的方法 优点:浏览器兼容性好,是 Macromedia 一直以来的官方方法缺点:a.embed 标签是不符合 W3C 的规范的,无法通过验证.当然, ...
- CodeForces 724G: Xor-matic Number of the Graph
题目传送门:CF724G. 题意简述: 一张 \(n\) 个点的无向图,边有边权. 定义三元组 \((u,v,w)(1\le u < v\le n)\) 合法当且仅当存在从点 \(u\) 到点 ...
- linux arm mmu基础【转】
转自:http://blog.csdn.net/xiaojsj111/article/details/11065717 ARM MMU页表框架 先上一张arm mmu的页表结构的通用框图(以下的论述都 ...
- Splay算法摘要
先介绍变量定义 int n; struct Node { //Splay节点定义 ],val,num,siz; //fa:它爸爸:son它儿子,左0右1:val:这个节点的值 //num:这个值的数量 ...
- 微信小程序入坑之自定义组件
前言 最近接触微信小程序,再次之前公司用的前端框架是vue ,然后对比发现,开发小程序是各种限制,对于开发者非常不友好.各种槽点太多,完全吐槽不过来,所以在此不多说,打算下次专门写一篇文章吐槽一下.本 ...
- 【鬼脸原创】JQuery获取元素的方法总结
目录 一.说明 二.获取本身 三.获取同级元素 四.获取父级元素 五.获取子级元素 一.说明 获取元素的方法分为两种:jQuery选择器.jQuery遍历函数. 做个总结,巩固下知识. 二.获取本 ...
- python进阶学习之匿名函数lambda
定义: lambda 参数列表:return(表达式) 匿名函数的表达式,只允许有一个. 匿名函数一般用于较简单的情况. 实例1: 实例2,配合高阶函数filter()使用: filter()函数是 ...