Shuffle
Time limit1sMemory limit128 MB
Given a song-play log and playlist size s, count starting offsets that split the log into blocks of length s (first/last possibly shorter) with no repeated song inside any block.
- Level
Medium6 of 10
- Topics
- Sliding window, Array, Implementation
- Solved
- No attempts yet
Problem
Jugyeong loves listening to music, and when she does she uses the shuffle feature of her music player. The player generates a random permutation of the songs in the playlist and plays every song in that order; once all songs have been played, it creates a new permutation (another shuffle) and keeps playing.
Jugyeong has a record of the songs that have been played so far. However, she realized that the record does not begin at the very first song that was ever played. In other words, the record is a contiguous segment that starts at some moment after playback began and ends at some later moment.
Let be the number of songs in the playlist. The record can be split into consecutive blocks of length , where only the first and the last block may be shorter than (they correspond to the tail and the head of a shuffle, respectively). Because each block is part of a single permutation produced by one shuffle, no song may appear more than once within the same block.
Such a way of splitting the record is fixed by the position of the first block boundary, which is a single value from to . Count the number of ways to split the record so that every block satisfies the condition. This count is exactly the number of possibilities for how the next shuffle could continue.
Input
The first line contains the number of test cases . is at most .
For each test case, the first line contains two integers and (). is the number of songs in the playlist, and is the number of songs recorded so far.
The second line contains integers separated by spaces, describing the songs that were played ().
Output
For each test case, print on a single line the number of ways to split the record into valid blocks (the number of possible positions for the first block boundary). If no valid split exists, print .