【Usaco2015 JAN】Moovie Mooving 题解

【Usaco2015 JAN】Moovie Mooving

首先,题意:

Description

Bessie is out at the movies. Being mischievous as always, she has decided to hide from Farmer John for L (1 <= L <= 100,000,000) minutes, during which time she wants to watch movies continuously. She has N (1 <= N <= 20) movies to choose from, each of which has a certain duration and a set of showtimes during the day. Bessie may enter and exit a movie at any time during one if its showtimes, but she does not want to ever visit the same movie twice, and she cannot switch to another showtime of the same movie that overlaps the current showtime. Help Bessie by determining if it is possible for her to achieve her goal of watching movies continuously from time 0 through time L. If it is, determine the minimum number of movies she needs to see to achieve this goal (Bessie gets confused with plot lines if she watches too many movies).

Input

The first line of input contains N and L. The next N lines each describe a movie. They begin with its integer duration, D (1 <= D <= L) and the number of showtimes, C (1 <= C <= 1000). The remaining C integers on the same line are each in the range 0..L, and give the starting time of one of the showings of the movie. Showtimes are distinct, in the range 0..L, and given in increasing order.

Output

A single integer indicating the minimum number of movies that Bessie
needs to see to achieve her goal. If this is impossible output -1
instead.

样例

Sample Input

4 100
50 3 15 30 55
40 2 0 65
30 2 20 90
20 1 0

Sample Output

3

大意

Bessie要在电影院里呆L分钟,这段时间他要看小型电影度过。电影一共N部,每部都播放于若干段可能重叠的区间,Bessie决不会看同一部电影两次。现在问他要看最少几部电影才能度过这段时间? 注:必须看电影才能在电影院里呆着,同时一场电影可以在其播放区间内任意时间入场出场。

分析

考到的是dp。
设f[i]表示状态为i(二进制)时,最多能看到的时间。
(什么意思呢?)
eg:f[10]对应的二进制值为1010,表示Bessie看第2,4个电影。
这样我们就可以很方便的dp,也可以说是递推吧。
f[i|1<<j]=max(f[i|1<<j],c[j][k]+len[j]);
(c[j][k]表示第j个电影的第j次放映,len[j]是第j次电影的时间长)。
注意,因为这个k的规模有1000,所以我们二分求出离当前最近的k。
然后时间复杂度O(2^N×N×xxxx) 
其中xxxx是求能看片场中最靠后一个的时间复杂度。
差不多就是这样了,具体看代码。

代码

//match:【Usaco2015 JAN】
//problem:Moovie Mooving
//signature:weiqiang he
#include<cstdio>
#include<iostream>
#define lowbit(a) (a)&(-a)
#define S 1<<N
#define inf 2000000001
using namespace std;
const int maxn=20+2;
const int maxm=1000+10; 
int N,L,f[1<<maxn],ans=inf;  //f[i]表示状态为i(二进制)时,最多能看到的时间 
int len[maxn],p[maxn],c[maxn][maxm];
int find(int x,int i)
{
    int l=-1,r=p[i]-1,ans,mid;
    while(l<r)
    {
        mid=(l+r+1)>>1;
        if(c[i][mid]<=x) l=mid;
        else r=mid-1;
    }
    return l;
}
int main()
{
    cin>>N>>L;
    for(int i=0;i<N;i++)
    {
        scanf("%d%d",&len[i],&p[i]);
        for(int j=0;j<p[i];j++)
            scanf("%d",&c[i][j]);
    }
    f[0]=0;
    for(int i=1;i<S;i++)
      f[i]=-1;
    for(int i=0;i<S;i++)
    {
        if(f[i]==-1) continue;
        if(f[i]>=L){
            int j,k=0;
            for(j=i;j>0;j-=lowbit(j))  //看二进制中有几个1
              k++;
            ans=min(ans,k);
        }
        for(int j=0;j<N;j++)
        {
            if(i&(1<<j)) continue;
            int k=find(f[i],j);
            if(k==-1) continue;
            f[i|1<<j]=max(f[i|1<<j],c[j][k]+len[j]); 
        }
    }
    if(ans==inf) cout<<-1;
    else cout<<ans;
    return 0;
}

还有另一份稍微丑一点的代码:
点这里

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值