luogu#P16424. [IATI 2022] Lifts
[IATI 2022] Lifts
Problem Description
You have been hired to build a lift system for a hotel!
The hotel has lifts, and initially you can choose which floors they are on. During the day there are requests, each characterized by a pair . This means someone is on the -th floor and wants to go to the -th floor.
People don’t like waiting around, so we have the requirement to complete the requests sequentially. More formally, request number must be completed before request number . The lifts are small and must be empty or carrying exactly one passenger at any given time. Also, every request can be completed by any of the lifts.
We want to build a smart system, so we want to minimize the total number of floors when lifts are travelling empty. More precisely, if a lift travels from floor to floor without a passenger, then we consider that it has travelled floors empty. We should note that the lifts can wait at the same floor for any amount of time, without incurring any additional cost.
Unfortunately, the hotel is old and the machines only have 64 MB of memory! However, we know you’re a great programmer, so this should be no issue for you.
Write a program lifts that computes the optimal schedule that minimizes the total number of floors that the lifts are travelling empty.
Input Format
The first line of the standard input contains the numbers and . The next lines contain 2 numbers each - for the respective request.
Output Format
On the single line of the standard output, print the minimum sum of floors where the lifts travel empty.
3 2
5 20
8 100
2 80
12
Hint
Explanation of the examples
Lift is initially on floor and lift is on floor .
Lift travels floors empty and carries the passenger to the th floor.
Lift travels floors empty and carries the passenger to the th floor.
Lift travels floors empty and carries the passenger to the th floor.
The total number of floors where the lifts travel empty is .
Constraints
Subtasks
| No | Points | |
|---|---|---|
| - |