Minimizing Maximizer
Time limit1sMemory limit512 MB
Given a pipeline of range-sort operations, find the minimum number of operations (kept in order) whose composition still guarantees the last position always holds the overall maximum.
- Level
Hard8 of 10
- Topics
- Greedy, Intervals, Dynamic programming
- Solved
- No attempts yet
Problem
A company is building a new sorting device called Maximizer. The Maximizer has inputs numbered from to ; each input carries one integer. It has a single output that must always equal the maximum of the values on its inputs.
The Maximizer is implemented as a pipeline of sorters . Every sorter has inputs and outputs. sorts the values on positions into non-decreasing order and passes every other position through unchanged. The -th output of the last sorter is the output of the Maximizer.
An engineer noticed that some sorters can be removed from the pipeline while the Maximizer still produces the correct result for every possible input. Determine the length of the shortest subsequence of the given pipeline (keeping the sorters in their original order) that still produces the correct result for all possible input values.
Write a program that reads the description of a Maximizer (its initial pipeline of sorters), computes the length of the shortest such subsequence, and writes that length.
Input
The first line contains two integers and (, ) separated by a single space, where is the number of inputs and is the number of sorters in the pipeline.
Each of the next lines describes one sorter in pipeline order. The -th of these lines contains two integers and () separated by a single space, the parameters of the -th sorter.
Output
Print a single line containing one integer: the length of the shortest subsequence of the initial pipeline of sorters that still produces correct results for all possible inputs.