Demand-Responsive Bus
InterviewTime limit1sMemory limit1024 MB
Match as many requests (passengers, max wait) to buses (capacity, arrival) as possible, where a bus fits a request if its capacity and arrival time are both at least as small or large as required.
- Level
Medium7 of 10
- Topics
- Greedy, Sorting, Two pointers, Binary search
- Solved
- No attempts yet
Problem
Hyundai AutoEver is a company that works on software and infrastructure across both the In-Car and Out-Car domains. Hyundai AutoEver is currently developing a demand-responsive bus (MOD). A demand-responsive bus generates the fastest route in real time and dispatches a vehicle when a passenger calls for one. It is a new mobility solution that can improve convenience for residents in the intermediate stage of urban development, when a route system is just beginning to take shape. Because it is dispatched and operated by real-time calls without fixed routes, the waiting time and travel time for citizens are shortened, which greatly improves public transit convenience.
You are developing a system that matches requests simultaneously when they flood in, such as during rush hour. There are dispatch requests, and each dispatch request consists of the number of passengers and the maximum waiting time. There are buses, and each bus has a capacity and an estimated arrival time. A bus does not wait more than minute after arriving before leaving.
To assign a bus to a dispatch request, the bus capacity must be greater than or equal to the number of passengers, and the estimated arrival time must be less than or equal to the maximum waiting time. For example, if the number of passengers is and the maximum waiting time is minutes, a bus with capacity of at least and an estimated arrival time within minutes can be dispatched.
When matching dispatch requests with buses, there is a constraint that they must correspond one-to-one. That is, a single dispatch request cannot be assigned to two or more buses, and conversely, assigning a single bus to two or more dispatch requests is not permitted by policy. Also, multiple requests can be processed simultaneously at the same time.
You must implement a program that matches buses to as many dispatch requests as possible.
Input
The input is given as follows.
- is the number of dispatch requests, and is the number of buses. ()
- and represent the information of a dispatch request. They indicate that the -th dispatch request has passengers and a maximum waiting time of minutes. ()
- and represent the information of a bus. They indicate that the -th bus has a capacity of people and an estimated arrival time of minutes. ()
- All numbers in the input are integers.
Output
On the first line, print the maximum number of dispatch requests that can have a bus assigned.