Trips
InterviewTime limit1sMemory limit128 MB
Match group sizes to trip intervals so that each interval gets at most one group and the number of matched intervals is maximized.
- Level
Medium7 of 10
- Topics
- Greedy, Sorting, Two pointers, Intervals
- Solved
- No attempts yet
Problem
During the upcoming holiday season, many people want to take an unforgettable trip, and everyone prefers to travel together with a group of friends. A travel agency offers several group trips. For each trip the group size is restricted: a minimum and a maximum number of people are given. Each group may choose at most one trip, and each trip may be assigned to at most one group. A group can be assigned to a trip only if the group's size lies within that trip's allowed range.
The agency wants to organize as many trips as possible. Given the group sizes and the size limits of every trip, determine the maximum number of trips that can be arranged.
Input
The first line contains two integers and (, ): the number of groups and the number of trips. Groups are numbered from to , and trips are numbered from to .
Each of the next lines contains one integer (), the size of the -th group.
Each of the following lines contains two integers and (), the minimum and the maximum group size that trip can accept.
Output
Output a single integer (): the maximum number of trips that can be arranged.