#QHD0000020. Hòa nhạc bàn phím (Keyboards in Concert)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Hòa nhạc bàn phím (Keyboards in Concert)
Keyboards in Concert
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
A concert consists of notes to be played in their given order, using one of electronic keyboards. Each keyboard supports only a specified set of notes; every note in the composition can be played by at least one keyboard.
Before the first note, you may choose any keyboard for free. You may stay on the same keyboard while it supports the next note, or switch to a different one, paying one switch each time.
Determine the minimum number of keyboard switches needed to play the entire composition.
Input
The first line contains and , the number of keyboards and notes.
For each keyboard , the next line begins with , followed by distinct notes it supports.
The last line contains the notes of the composition in order.
Output
Print the minimum number of keyboard switches.
Subtasks
- Subtask 1 (20 points): ; ; ; Every musical note is between and ; Within each keyboard the notes are distinct; each requested note can be played on at least one keyboard.
- Subtask 2 (30 points): ; ; ; Every musical note is between and ; Within each keyboard the notes are distinct; each requested note can be played on at least one keyboard.
- Subtask 3 (50 points): ; ; ; Every musical note is between and ; Within each keyboard the notes are distinct; each requested note can be played on at least one keyboard.
Examples
Input
2 10
2 1 2
2 2 3
1 2 1 2 3 3 2 3 1 3
Output
3
Explanation
Keyboard can play notes and keyboard can play notes . A note requires keyboard , while note requires keyboard . Play the first four notes on keyboard . Switch to keyboard for notes through , then back to keyboard for note , and finally to keyboard for note . Exactly switches are needed.