Liars
InterviewTime limit1sMemory limit512 MB
Given each person's claimed range for the truth-teller count, find the maximum possible number of truth-tellers within any consistent assignment.
- Level
Medium6 of 10
- Topics
- Hash map, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
There are n people in a circle, numbered from 1 to n. Each person always tells the truth or always lies.
Each person i makes the claim: "the number of truth-tellers in this circle is between ai and bi, inclusive."
Compute the maximum number of people who could be telling the truth.
Input
The first line contains a single integer n. (1 ≤ n ≤ 103)
Each of the next n lines contains two space-separated integers ai and bi. (0 ≤ ai ≤ bi ≤ n)
Output
Print, on a single line, the maximum number of people who could be telling the truth.
If the given set of statements is inconsistent, print -1 instead.