Teams
Time limit5sMemory limit256 MB
Split the row into the most contiguous teams so each student's team size lies within their given range, and count those optimal splits.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Segment tree, Intervals
- Solved
- No attempts yet
Problem
Mr. Bajtocki is the most popular physical education teacher at Travelling Salesman Bajtazar Primary School No. 64 in Byteotia. In every lesson, after a short warm up, he asks the students which team game they want to play and then helps them split into teams.
At the roll call the students stand in one row and take the numbers to in that order. Mr. Bajtocki forms the teams so that every team is a contiguous block of the row. Each student belongs to exactly one team.
The teacher knows his students well, so he knows that student is happy with the split only if the number of players in that student's team is at least and at most .
Decide whether the students can be split so that every student is happy. If they can, find the maximum possible number of teams and the number of splits that reach this maximum.
Input
The first line contains one integer (), the number of students.
Each of the next lines describes one student. The -th of these lines contains two integers and (). Student is happy when the size of that student's team lies in the range .
Output
If the students can be split so that every student is happy, print two integers separated by a single space. The first is the maximum number of teams, the second is the number of splits that reach this maximum, taken modulo .
If no such split exists, print NIE.