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

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

题目分析

很显然状压DP
f[s]表示选用状态s电影 能度过的最长时间
更新的时候用二分贪心一下就好了

#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
int n,m;
int len[50],a[50][1010],p[50];
int f[1<<22];
int find(int sum,int id)
{
    int l=1,r=p[id],mid,ans=-1;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(a[id][mid]<=sum) l=mid+1,ans=mid;
        else r=mid-1;
    } 
    return ans;
}
int ans;
int main()
{   
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&len[i],&p[i]);
        for(int j=1;j<=p[i];j++) scanf("%d",&a[i][j]);
    }
    memset(f,-1,sizeof f);
    f[0]=0,ans=0x3f3f3f3f;
    for(int i=0;i<(1<<n);i++)
    {
        if(f[i]==-1) continue;
        if(f[i]>=m)
        {
            int cnt=0;
            for(int j=i;j;j-=j&-j) cnt++;
            ans=min(ans,cnt);
            continue;
        }
        for(int j=1;j<=n;j++)
        {
            if(i&(1<<(j-1))) continue;
            int nm=find(f[i],j);
            if(nm==-1) continue;
            f[i|(1<<(j-1))]=max(f[i|(1<<(j-1))],a[j][nm]+len[j]);
        }
    }
    if(ans==0x3f3f3f3f) printf("-1");
    else printf("%d",ans);
    return 0;
}


发表评论

电子邮件地址不会被公开。 必填项已用*标注