A few years ago, Arup started submitting problems to the UCF Local Contest regarding her daughter Anya’s CD requests. Unfortunately, his latest question wasn’t good enough to make the 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 the practice 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 number k 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 choices(Currently, Anya’s favorite CD is Divide by Ed Sheeran, and her favorite track numbers on this CD are 4, 5, and 7.). 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 the second 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 a valid 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 ≤10^9 ), 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,
n i(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 track numbers 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 button presses Arup can use to play the desired sequence of songs from the CD.
Sample Input
15 2
4 1 3 8 9
2 12 2
Sample Output
0
Sample Input
12 5
2 5 7
2 5 7
3 12 2 4
1 9
2 4 5
Sample Output
16
#include <bits/stdc++.h>
#define ll long long
#define pil pair<int,ll>
using namespace std;
const int N = 1e3 + 10;
const ll inf = 1e15;
vector<pil > List[N];
bool v[N][12];
int s, t;
struct node {
int i, j, x;//i第几首歌,x歌曲序号
ll d;
bool operator<(const node &a) const {
return d > a.d;
}
};
inline void read() {
cin >> t >> s;
int n;
for (int i = 1; i <= s; i++) {
scanf("%d", &n);
for (int j = 1; j <= n; j++) {
ll x;
scanf("%lld", &x);
List[i].push_back({x, inf});
}
}
}
inline long long dis(long long i, long long j) {
long long res;
if (i == j)
res = 1;
else if (i > j)
res = min(i - j + 1, j + t - i - 1);
else
res = min(j - i - 1, i + t - j + 1);
return res;
}
inline ll dijkstra() {
priority_queue<node> q;
q.push({0, 0, t, 0});
while (!q.empty()) {
node te = q.top();
q.pop();
if (te.i == s)return te.d;
if (v[te.i][te.j])continue;
v[te.i][te.j] = true;
for (int i = 0; i < List[te.i + 1].size(); i++) {
pil tc = List[te.i + 1][i];
if (tc.second > te.d + dis(te.x, tc.first)) {
List[te.i + 1][i].second = te.d + dis(te.x, tc.first);
q.push({te.i + 1, i, tc.first, List[te.i + 1][i].second});
}
}
}
}
int main() {
read();
cout << dijkstra() << endl;
return 0;
}
最小按钮按压次数
解决一个有趣的问题:如何在播放Anya最喜欢的CD时,通过最少的按钮操作播放她选择的歌曲序列。考虑到CD播放器的特殊操作逻辑,包括向前和向后跳转的功能,以及CD在达到末尾或开始时的循环特性。
274

被折叠的 条评论
为什么被折叠?



