#P3510. [POI2010] JED-Ones
[POI2010] JED-Ones
题目描述
Let be a sequence of zeros and ones.
An utterly forlorn one (UFO) in is the extreme (either first or last) one that additionally does not neighbour with any other one.
For instance, the sequence 10001010 has two UFOs, while the sequence 1101011000 has no UFO, and the sequence 1000 has only one UFO.
Let us denote the total number of UFOs in the binary representations of the numbers from 1 to with .
For example, , , , .
We will be working with very large numbers.
Therefore, we shall represent them in a succinct way.
Suppose is a positive integer and is its binary representation (starting with 1). Then the succinct representation of is the sequence consisting of positive integers denoting the lengths of successive blocks of the same digits.
For example:
Your task is to write a program that finds the sequence given .
输入格式
The first line of the standard input holds one integer () denoting the length of the succinct representation of a positive integer .
The second line of the standard input holds integers (), separated by single spaces.
The sequence forms the succinct representation of the number .
You may assume that , i.e., .
输出格式
Your program is to print out two lines to the standard output.
The first one should contain a single positive integer .
The second line should hold positive integers , separated by single spaces.
The sequence is to form the succinct representation of .
题目大意
设 是一个由 组成的序列。一个 UFO 指的是序列中第一个 或者最后一个 且不和任何一个 相邻。例如 有两个 UFO, 没有 UFO, 只有一个 UFO。
设 到 的数的二进制表示中 UFO 的总数为 。例如,.
我们需要处理非常大的数字。因此 会用压缩的形式表示。设 是一个正整数 是其二进制表示(最高位为 ),则该数的压缩形式 为一个序列,表示连续相同数位的数量。例如:
已知 ,求 。
输入格式:
第一行有一个整数 ,表示一个正整数 的压缩形式。
接下来一行有 个整数 ,用空格分隔,表示 的压缩形式的序列。保证 ,也就是说 。
输出格式:
输出两行,第一行有一个正整数 ,第二行有 个正整数,用空格分隔,表示 的压缩形式。
6
1 1 1 1 1 1
5
1 1 2 1 1