Photo
InterviewTime limit1sMemory limit128 MB
Given N cows in a line and K unfriendly pairs that cannot share a photo, find the minimum number of consecutive-range photos covering every cow.
- Level
Medium7 of 10
- Topics
- Greedy, Intervals, Two pointers, Sorting
- Solved
- No attempts yet
Problem
Farmer John wants to take photos of his cows (), which are standing in a line and conveniently numbered from left to right. Each photograph can capture a consecutive range of cows from the lineup, and Farmer John wants to make sure that each cow appears in at least one photo.
Unfortunately, there are unfriendly pairs of cows () that each refuse to be in the same photograph. Given the locations of these unfriendly pairs, determine the minimum number of photos Farmer John needs to take.
Input
- Line 1: Two space-separated integers, and .
- Lines : Line contains two integers, and , stating that the cows in positions and are unfriendly and therefore cannot be in the same photograph (, ).
Output
- Line 1: A single integer specifying the minimum number of photos Farmer John needs to take.
Hint
When and the unfriendly pairs are , , , Farmer John can take 3 photos:
- One ranging from to .
- One ranging from to .
- One ranging from to .