UCF Local Programming Contest 2019(Practice)

Anya’s Favorite CD

Anya’s Favorite CD

A few years ago, Arup started submitting problems to the UCF Local Contest regarding herdaughter Anya’s CD requests. Unfortunately, his latest question wasn’t good enough to makethe cut for the 2019 UCF Local Contest. Luckily, a couple coaches,nostalgic for solving Anya’s CD problems, suggested that Arup’s latest question about Anya’s CD requests be added to thepractice local contest. Arup is so ecstatic that his question is being used in this contest that he will personally present the first solver with a copy of Ed Sheeran’s CD “Divide”.

To this day,Arup drives Anya to school daily and Anya makes CD requests in the car. When Anya was four (in 2017), she would request a sequence of track numbers for Arup to play and Arup had to determine the fewest number of button presses to satisfy the requests. If you didn’t compete in 2017, here is an explanation of how the CD player in Arup’s car works:

Arup can only change tracks by pressing a forward button or a backward button. If track numberk has completed, when Arup presses the backward button, track number k will play again.Alternatively, if he presses the forward button when track k has completed, then track k+2 will play. The only exception to the latter is that if track k+2 doesn’t exist. In this case, the tracks just wrap back around to the beginning, starting with 1, then 2, etc. Similarly, if Arup presses the backward button twice right after track 1 completes, this will move the CD to track t, where t is the number of tracks on the CD. In the absence of a button press, after track k completes,track k+1 plays, except for when k = t, in which case, track 1 plays next.

Now that Anya is six, her requests have become slightly more flexible. Instead of insisting on particular songs in a sequence, for each song, Anya gives Arup several choices1. For example,for the first song, Anya may tell Arup that she wants to hear one of tracks 1, 3, 8 or 9, and for thesecond song Anya may tell Arup she wants to hear one of tracks 2 or 12.

Even though Arup has some choice in what songs he plays, he still gets caught pressing either the forward or backward button a great deal. Help him minimize the number of times he presses the buttons. In the example above, given that the CD is originally queued to start with track 1,no button presses are required because he can simply select to play track 1 followed by track 2.

The Problem:
Given the number of tracks on Anya’s favorite CD and the set of possible tracks for each song Anya wants played, determine the minimum number of button presses Arup must make to get avalid sequence of songs played. For the purposes of this problem, assume that at the very beginning the CD player is queued up to play track number 1, so that if the first song played is anything but track 1, some buttons will have to be pressed before the first song plays.

The Input:
The first input line consists of two (space separated) positive integers: t (1 ≤ t ≤ 109), the number of tracks on Anya’s favorite CD and s (1 ≤ s ≤ 1000), the number of songs Anya would like to listen to from the CD. The next s input lines contain the possible track numbers for each song she would like to listen to. The ith of these lines starts with a positive integer, ni (1 ≤ n_i ≤ 10),representing the number of choices Anya has provided for the ith song she listens to. This is followed by ni distinct positive integers, each in between 1 and t, inclusive, indicating the tracknumbers of the possible ith song Anya will listen to.

The Output:
Output a single integer on a line by itself indicating the minimum number of bu

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值