Zigzag Histogram Partition
Time limit1sMemory limit1024 MB
Split a histogram of N bars into contiguous pieces so that each piece's largest rectangle forms a zigzag sequence, maximizing the number of pieces.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Segment tree, Stack
- Solved
- No attempts yet
Problem
A histogram is formed by placing rectangles of width and heights , , in order.
You cut the histogram into several pieces by slicing perpendicular to the base. Each piece must have a positive integer width. When the pieces are listed in order, the largest rectangle area fully contained in each piece forms a zigzag sequence. Maximize the number of pieces.
Formally, a sequence is a zigzag sequence if it satisfies at least one of the following two conditions.
- For every , if is even then , and if is odd then .
- For every , if is even then , and if is odd then .
You can transform the histogram with the following operation.
- Take a strictly increasing sequence with and .
- The result is a sequence of length , written .
- For every , is the largest rectangle area in the histogram formed by joining rectangles through in order.
For every valid sequence , take the resulting sequence . Find the length of the longest one that is a zigzag sequence.
Input
The first line contains a positive integer ().
The second line contains positive integers separated by spaces ().
Output
Print the answer on the first line.