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 button presses Arupcan use to play the desired sequence of songs from the CD.can use to play the desired sequence of songs from the CD.

样例输入1

15 2
4 1 3 8 9
2 12 2

样例输出1

0

样例输入2

12 5
2 5 7
2 5 7
3 12 2 4
1 9
2 4 5

样例输出2

16

问题重述:

有一个 CD 播放器,CD 里有 t 首歌(序号 1~t),CD 播放器有前进和后退两
个按键,若当前第 x 首歌刚刚播放完毕,若不按键,则自动播放第 x+1 首,若按
前进键,则播放第 x+2 首,若按后退键,则再播放第 x 首,按两次则播放第 x-1
首(若即将播放的序号不存在,则自动循环列表,例如:t + 1 不存在,则回到播
放列表开头的第 1 首歌)。现在 B 给出 s 个需求序列,每个序列包含若干个在该
时间点,B 希望听到的歌曲序号。现在 CD 播放器从第一首歌开始播,求出 A 为
了满足 B 的需求,至少需要按几次按键。

问题分析:

本问题具有“最优子结构”以及“无后效性”,显然是一个线性动态规划问题。

#include 
#include 
#include 
#include
using namespace std;
const int Max=1e3+10;
typedef long long ll;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll dp[2][13];
ll a[2][13];
int main()
{
    clock_t start_time=clock();
    {
        ll n,test,t,t1,mins;
        int q=1;
        cin >> n >> test;
        a[0][1]=n,a[0][0]=1;
        for(int i=1; i<=test; i++)
        {
            cin >> a[q][0];
            for(int j=1; j<=a[q][0]; j++)
            {
                cin >> a[q][j];
                mins=INF;
                for(int k=1; k<=a[!q][0]; k++)
                {
                    if(a[q][j]>a[!q][k])
                        t=a[q][j]-a[!q][k]-1,t1=(a[!q][k]-a[q][j]+n)%n+1;
                    else if(a[q][j]<a[!q][k])
                        t=a[!q][k]-a[q][j]+1,t1=(a[q][j]-a[!q][k]+n)%n-1;
                    else t=t1=1;
                    mins=min(mins,dp[!q][k]+min(t,t1));
                }
                dp[q][j]=mins;
            }
            q=!q;
        }
        ll ans=dp[!q][1];
        for(int j=2; j<=a[!q][0]; j++)
            ans=min(ans,dp[!q][j]);
        cout << ans << endl;
    }
    clock_t end_time=clock();
    //cout<< "Running time is: "<(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<
    return 0;
}

你可能感兴趣的:(刷题笔记)