UCF “Practice” Local Contest — Aug 31, 2019 Anya’s Favorite CD

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<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<time.h>
using namespace std;
typedef long long ll;
const int MOD=1e9+7;
const ll INF = 0x3f3f3f3f3f3f3f3f;

ll t,a[1020][20],f[1020][20];

ll cc(ll x,ll y)  //求两曲目间距离
{
    if(x==t)x=0;
    x++;
    if(x==y)return 0;
    if(x<y)return min(y-x,x+t-y);
    if(x>y)return min(x-y,y+t-x);
}


int main()
{
freopen("D:input.txt", "r", stdin);
clock_t start_time=clock();
{
    int s,n;
    cin>>t>>s;  
    for(int i=1;i<=s;i++)   //对于第i首曲mu
    {
        cin>>n;      //输入n
        a[i][0]=n;         //a[i][0]存储可能的曲目个数n
        if(i==1)a[0][0]=1;   //对于第一行的上一行假设有1个耗时为的0曲目(类似dp里的dp[0][0]=0);
        for(int j=1;j<=n;j++)
        {
            cin>>a[i][j];  //输入第j个可能曲目
            ll c=INF;
            for(int k=1;k<=a[i-1][0];k++)    //用上一行的曲目(a[i-1][0]个)对当前输入这一个可能曲目进行优化
            {
                c=min(c,f[i-1][k]+cc(a[i-1][k],a[i][j]));  //如果上一行的某一个曲目到这个曲目的距离较小,就对改曲目进行优化
            }
            f[i][j]=c;  //上一次的曲目到这一次的可能曲目最小值是上一行所有可能曲目到当前可能曲目的值里面最小那个
        }
    }
    ll ans=f[s][1]; //找出最后一行的多个曲目里,多个可能曲目经过优化之后的最小值既是答案;
    for(int i=1;i<=a[s][0];i++)ans=min(ans,f[s][i]);
    cout<<ans<<endl;
}
clock_t end_time=clock();
cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//输出运行时间
return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值