Joke with Turtles
Time limit2sMemory limit128 MB
Each turtle claims a count of turtles ahead and behind it; choose positions to maximize how many claims hold at once.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Greedy, Sorting, Intervals
- Solved
- No attempts yet
Problem
There is a famous riddle-joke for children:
Three turtles are crawling along a road. The first turtle says: "There are two turtles ahead of me." The second turtle says: "There are two turtles behind me." The third turtle says: "There are two turtles ahead of me and two turtles behind me." How can this be?
The answer is: the third turtle is lying!
In this problem, turtles are crawling along a road. Several turtles may crawl together in a group at the same position, so that turtles in the same group see none of their group members either ahead or behind them. Each turtle makes a statement of the form: "There are turtles ahead of me and turtles behind me." Find the minimum possible number of turtles that are lying.
Formally, turtle has a coordinate , and different turtles may share the same coordinate. Turtle tells the truth if and only if the number of turtles with is exactly and the number of turtles with is exactly ; otherwise turtle is lying. You may choose the coordinates freely, and you must report the minimum number of turtles that must be lying, taken over all arrangements (equivalently, minus the maximum number that can simultaneously tell the truth).
Input
The first line contains an integer (). Each of the next lines contains two integers and () describing the statement of turtle for from to .
Output
Print a single integer : the minimum number of turtles that must be lying.