#P2556. Image Pixel Segment Encoding

    ID: 15825 Type: Default 1000ms 256MiB

Image Pixel Segment Encoding

Image Pixel Segment Encoding

During an experiment in introductory biological genetics, Coco performed an imaging experiment at home. In her experiment, an image is represented as a sequence of pixels, and the total number of pixels is always a multiple of 8. Every 8 pixels are grouped to form a byte. In the bitmap method, each byte is an 8‐bit binary number where the first bit (most significant) corresponds to the first pixel and the last bit (least significant) corresponds to the last pixel. A bit 0 represents a white pixel and a bit 1 represents a black pixel.

For example, the byte sequence 210, 0, 255 represents 24 pixels. Converting these numbers to binary gives:

  • 210: \(11010010\)
  • 0: \(00000000\)
  • 255: \(11111111\)

Coco noticed that the image has many continuous segments of pixels with the same color. She proposes a pixel segment method to encode the image: the image is divided into segments such that every segment consists of consecutive pixels of the same color. It is guaranteed that each segment contains at least 1 pixel and fewer than 128 pixels.

Each segment is encoded with one byte: the most significant bit represents the color of the pixel segment (0 for white, 1 for black) and the remaining 7 bits represent the length of the segment. For example, the bitmap byte sequence 210, 0, 255 corresponds to the pixels that can be segmented into:

11, 0, 1, 00, 1, 000000000, 11111111

Using the pixel segment method, these segments are encoded as follows:

  • Segment "11": black with length 2 \(\to 1\text{ (color)}\,\ 0000010 = 130\)
  • Segment "0": white with length 1 \(\to 0\, 0000001 = 1\)
  • Segment "1": black with length 1 \(\to 1\, 0000001 = 129\)
  • Segment "00": white with length 2 \(\to 0\, 0000010 = 2\)
  • Segment "1": black with length 1 \(\to 129\)
  • Segment "000000000": white with length 9 \(\to 9\)
  • Segment "11111111": black with length 8 \(\to 136\)

Your task is to convert the image from the bitmap method to the pixel segment method.

Note: When a segment exceeds 127 pixels, it must be split into multiple segments since each segment’s length is represented with 7 bits.

inputFormat

The input begins with an integer N (\(1 \leq N \leq 10^5\)) on the first line, representing the number of bytes in the bitmap representation. The second line contains N integers (each between 0 and 255) separated by spaces, representing the bytes of the image. It is guaranteed that the total number of pixels (\(8 \times N\)) is at least 8 and is a multiple of 8.

outputFormat

Output the pixel segment method representation as a sequence of decimal integers separated by a single space. Each integer represents one encoded segment.

sample

3
210 0 255
130 1 129 2 129 9 136