Wooden Sticks
InterviewTime limit1sMemory limit128 MB
Given n sticks with length and weight, order them to minimize the number of setup steps, where a setup is needed unless both length and weight are nondecreasing from the previous stick.
- Level
Medium5 of 10
- Topics
- Sorting, Greedy, Dynamic programming
- Solved
- No attempts yet
Problem
A woodworking shop has wooden sticks. Each stick has a fixed length and weight. The sticks are processed by a machine one at a time. Before processing a stick, the machine may need some time to be prepared for that stick — this is called the setup time. The setup time is determined by the following rules.
- Processing the very first stick requires a setup time of 1 minute.
- Suppose a stick of length and weight has just been processed, and the next stick to process has length and weight . If and , then no setup time is needed. Otherwise the machine must change its tool, so a setup time of 1 minute is required.
You may process the sticks in any order. Find the minimum total setup time needed to process all of the sticks.
For example, suppose there are five sticks . Processing them in the order needs a total setup time of 2 minutes, and this cannot be reduced any further, so the answer is 2.
Input
The first line contains the number of test cases .
Each test case consists of two lines. The first line contains the number of sticks (). The second line contains separated by spaces, where and are the length and weight of the -th stick. All of these values are integers not greater than .
Output
For each test case, print the minimum required setup time on its own line.