Arranging Tickets
Time limit4sMemory limit256 MB
Given N stations on a circular railway and M travel requests with passenger counts, find the minimum number of ticket packages needed to serve all requests.
- Level
Hard9 of 10
- Topics
- Prefix sum, Binary search, Greedy
- Solved
- No attempts yet
Problem
Republic of JOI has stations numbered from 1 to . They are placed in order along a circular railway, clockwise.
There are types of train tickets, numbered from 1 to . One ticket of type () lets one person travel from station to station , or from station to station . One ticket of type lets one person travel from station 1 to station , or from station to station 1. Tickets can only be bought as a package of tickets, with one ticket of each type.
You work at a travel agency in Republic of JOI, and your job is to arrange tickets for customers. Today you have requests. The -th request says that people want to travel from station to station . These people do not need to take the same route.
Find the minimum number of ticket packages you need to buy to handle all requests.
Input
The first line contains two space-separated integers and , the number of stations and the number of requests.
Each of the next lines contains three space-separated integers , , and . This means people want to travel from station to station .
Output
Print one line containing the minimum number of ticket packages needed to handle all requests.
Constraints
- ()
- ()
- ()
- ()