#P6704. [COCI 2010/2011 #7] GITARA
[COCI 2010/2011 #7] GITARA
Background
Darko has an imaginary alien friend who has one billion fingers. The alien quickly picks up a guitar, finds a simple melody online, and starts playing it.
The guitar, as usual, has six strings, labeled from to . Each string is divided into frets (segments), labeled from to .
A melody is a sequence of notes. Each note is produced by pressing a certain fret on a certain string (for example, string fret ). If multiple frets are pressed on the same string at the same time, the produced note is the one corresponding to the largest fret number among those pressed.
Example: On string , if fret is already pressed and you want to play the note at fret , you only need to press fret and do not need to release fret , because only the highest pressed fret affects the note produced by that string (in this example, fret ). Similarly, if you now want to play the note at fret , you must release both fret and fret .
Please write a program to compute the minimum number of finger movements needed for the alien to play the given melody.
Problem Description
You have a matrix , and initially all entries are .
For each required position , you must satisfy:
-
At that moment, is .
-
For where , the value is .
You need to find the minimum number of state changes while satisfying all requirements.
Input Format
The first line contains two positive integers , which represent the number of notes in the melody and the number of frets on each string.
The next lines each contain two positive integers , representing the position (string number and fret number) that can produce the corresponding note, in the order played by the alien.
Output Format
Output one non-negative integer: the minimum number of finger movements made by the alien.
5 15
2 8
2 10
2 12
2 10
2 5
7
7 15
1 5
2 3
2 5
2 7
2 4
1 5
1 3
9
Hint
Explanation for Sample 1
All notes are produced on the second string. First press frets in order (). Then release fret (). Finally, press fret and release frets ().
Explanation for Sample 2
For each operation, the required numbers of finger movements are .
Constraints
Pressing or releasing a fret counts as one finger movement. Plucking the string does not count as a finger movement; it is a guitar-playing action (meaning you do not need to care how it is plucked, only the pressing is considered. Maybe the alien can use superpowers.)
For of the testdata, , .
Notes
This problem is worth points in total.
Translated from COCI2010-2011 CONTEST #7 T3 GITARA.
Translated by ChatGPT 5