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:
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
#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\n",ans);
}
return 0;
} /**********************************************************************
Problem: 1008
User: therang
Language: C++
Result: AC
Time:24 ms
Memory:2804 kb
**********************************************************************/
CSU1008: Horcrux的更多相关文章
- CSUOJ 1008 Horcrux
Description A Horcrux is an object in which a Dark wizard or witch has hidden a fragment of his or h ...
- ural1439 Battle with You-Know-Who
Battle with You-Know-Who Time limit: 2.0 secondMemory limit: 64 MB Rooms of the Ministry of Magic ar ...
- Manthan, Codefest 17
A. Tom Riddle's Diary time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Marvolo Gaunt's Ring(巧妙利用前后缀进行模拟)
Description Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as h ...
- codeforce 855B
B. Marvolo Gaunt's Ring time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 855B:Marvolo Gaunt's Ring(枚举,前后缀)
B. Marvolo Gaunt's Ring Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaun ...
随机推荐
- Python 元组、列表
Python中,最常用的数据结构就是序列了.序列有以下几种:元组,列表,字符串等.注:代码都是在IDE中编辑的,没有用交互式环境. 一.元组(tuple) 元组其实有点类似C++中的数组,即一旦指定了 ...
- 洛谷P3250 [HNOI2016]网络(整体二分+树状数组+树剖)
传送门 据说正解是树剖套堆???然而代码看着稍微有那么一点点长…… 考虑一下整体二分,设当前二分到的答案为$mid$,如果所有大于$mid$的边都经过当前点$x$,那么此时$x$的答案必定小于等于$m ...
- [POI2007]山峰和山谷Grz
Description FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷.为了能够让他对他的旅程有一个安排,他想知道山峰和山谷的数量.给定一个地图,为FGD想要旅行的区域,地图被分为\(n\ ...
- 题解报告:poj 3061 Subsequence(前缀+二分or尺取法)
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- javaweb-JSP action中附件下载的写法
附件下载(包括图片,exl,word)在前台必须给出一个iframe 或者类似于window的窗口,另外,Java文件下载不能通过ajax进行请求,必须添加src属性首选,前台的链接拼接html如下 ...
- 一个页面通过iframe,获取另一个页面的form
document.getElementsByTagName("iframe")[0].contentWindow.document.forms[0].submit(); var z ...
- js类、原型——学习笔记
js 内置有很多类,我们用的,都是从这些类实例化出来的. function Object () {} function Array () {} function String () {} functi ...
- 可变类型的安全性——更锋利的C#代码小记(2)
ReadOnlyCollection类型是.NET系统类库提供的一个只读集合类型,它与原来的List<string>不存在任何类型转换关系,因此可以从根本上阻止外部对其的修改操作using ...
- iOS 中集成海康威视 摄像视频
本文原文地址 http://www.cnblogs.com/qianLL/p/6652104.html 一.要导入相关的库,注意 这里比较坑的是 要用和他一样的 如果开始的工程中用了AFN或者MJE ...
- 希尔排序法及其js实现
希尔(Shell)排序又称为缩小增量排序,它是一种插入排序.它是直接插入排序算法的加强版. 希尔增量: 希尔增量是指希尔提出了一种冲破二次时间屏障的算法. Donald Shell 提出了一种冲破二次 ...