Bouquet
Time limit3sMemory limit1024 MB
Given N tulips with left and right protection ranges, find the largest set of tulips where no picked tulip falls inside another picked tulip's protected range.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Segment tree
- Solved
- No attempts yet
Problem
After visiting Keukenhof, one of the world's largest flower gardens, Lieke became very fond of flowers. She decided to pick some tulips growing next to the road to build a beautiful bouquet. However, the strict tulip protection laws in the Netherlands limit which flowers she can collect.
There are tulips numbered from to growing in a line along the road, from left to right. The protection law assigns two integers, and , to tulip . If Lieke picks tulip , then the tulips immediately to its left and the tulips immediately to its right cannot be in the bouquet. If fewer than tulips lie to the left (or fewer than to the right), all tulips on that side are excluded.
Find the maximum number of tulips Lieke can pick while following the law.
Input
The first line contains a single integer , the number of tulips.
The next lines describe the protection law. The th line contains two integers and .
Output
Print a single integer, the maximum number of tulips Lieke can pick.
Constraints
- .
- for .
Hint
In the first sample, if Lieke picks tulip , she cannot pick the two tulips on the right. Picking tulip does not prohibit her from picking tulip , but tulip prohibits her from picking tulip , so she cannot pick both. The maximum is .
In the second sample, the maximum is . The way to get it is shown in the picture below. Other choices give a smaller answer.
