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 ...
随机推荐
- java笔记之IO1
File:文件和目录(文件夹)路径名的抽象表示形式 * 构造方法: * File(String pathname):根据一个路径得到File对象 * File(String parent, S ...
- springmvc h5上传图片
工作中开发一个评价功能,需要上传拍照的图片,后台使用springmvc接收文件,前端FormData异步提交. 1. spring配置multipartResolver <bean id=&qu ...
- Swift4 类与继承, 类型转换, 类型判断
创建: 2018/03/05 完成: 2018/03/07 更新: 2018/03/09 完善标题 [Swift4 类与继承, 类型转换] -> [Swift4 类与继承, 类型转换与判断] 补 ...
- 鸭子类型(Duck Typing)
鸭子类型(Duck Typing) 动态类型.没有类型检验.不关注类型,关注方法 相当于静态类型语言的多态 这是程序设计中的一种类型推断风格,这种风格适用于动态语言(比如PHP.Python.Ruby ...
- C#中遍历ArrayList的三种方法(转)
using System; using System.Collections; using System.Linq; using System.Text; namespace ArrayListDem ...
- 在Python解释器运行程序
在解释器中运行 ***.py文件的方法:使用import添加模块 ***.py,然后调用 ***.py中的函数 例:在zoo.py中定义hours函数 运行方法: >>> impo ...
- _bzoj2002 [Hnoi2010]Bounce 弹飞绵羊【分块】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 见一周目记录:http://www.cnblogs.com/ciao-sora/p/6 ...
- (三)python函数式编程
一.高阶函数 高阶函数英文叫Higher-order function.什么是高阶函数?我们以实际代码为例子,一步一步深入概念. 变量可以指向函数 结论:函数本身也可以赋值给变量,即:变量可以指向函数 ...
- 题解报告:hdu 1124 Factorial(求N!尾数有多少个0。)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1124 Problem Description The most important part of a ...
- 题解报告:hdu 1230 火星A+B(字符串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1230 Problem Description 读入两个不超过25位的火星正整数A和B,计算A+B.需要 ...