Gym - 101908F Music Festival —— dp,二进制枚举

Music festivals should be fun, but some of them become so big that cause headache for the goers. The problem is that there are so many good attractions playing in so many stages that the simple task of choosing which shows to watch becomes too complex.

To help goers of such festivals, Fulano decided to create an app that, after evaluating the songs heard on the users’ favorite streaming services, suggests which shows to watch so that there is no other combination of shows that is better according to the criteria described below:

To enjoy the experience as much as possible it is important to watch each of the selected shows from start to end;
Going to the festival and not seeing one of the stages is out of the question;
To ensure that the selection of artists is compatible with the user, the app counts how many songs from each artist the user had previously listened to on streaming services. The total number of known songs from chosen artists should be the largest possible.
Unfortunately the beta version of app received criticism, because users were able to think of better selections than those suggested. Your task in this problem is to help Fulano and write a program that, given the descriptions of the shows happening in each stage, calculates the ideal list of artists to the user.

The displacement time between the stages is ignored; therefore, as long as there is no intersection between the time ranges of any two chosen shows, it is considered that it is possible to watch both of them. In particular, if a show ends exactly when another one begins, you can watch both of them.

Input
The first line contains an integer 1≤N≤10 representing the number of stages. The following N lines describe the shows happening in each stage. The i-th of which consists of an integer Mi≥1, representing the number of shows scheduled for the i-th stage followed by Mi show descriptions. Each show description contains 3 integers ij,fj,oj (1≤ij<fj≤86400; 1≤oj≤1000), representing respectively the start and end time of the show and the number of songs of the singer performing that were previously heard by the user. The sum of the Mi shall not exceed 1000.

Output
Your program must produce a single line with an integer representing the total number of songs previously heard from the chosen artist, or −1 if there is no valid solution.

Examples
Input
3
4 1 10 100 20 30 90 40 50 95 80 100 90
1 40 50 13
2 9 29 231 30 40 525
Output
859
Input
3
2 13 17 99 18 19 99
2 13 14 99 15 20 99
2 13 15 99 18 20 99
Output
-1

题意:

有n个舞台,每个舞台有m个剧,每个剧有起始结束时间和观看人数,找出一种方法使得每个舞台都有人看并且观看人数最多,不存在则输出-1

题解:

枚举开始时间,往后转移,因为只有10个数,所以可以用二进制枚举那些舞台,首先把dp数组赋成-1,因为可能有状态并不存在,比如有第一个舞台的m=0,那111就不可能从110转过来。之后就是背包一样的东西。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll dp[86405][(1<<10)+5];
struct node
{
    int fin,num;
    ll val;
    node(){}
    node(int fin,int num,ll val):fin(fin),num(num),val(val){}
};
vector<node>vec[86405];
int main()
{
    memset(dp,-1,sizeof(dp));
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        int t;
        scanf("%d",&t);
        int sta,fin;
        ll val;
        while(t--)
        scanf("%d%d%lld",&sta,&fin,&val),vec[sta].push_back(node(fin,i,val));
    }
    int t=1<<n;
    dp[0][0]=0;
    for(int i=0;i<=86400;i++)
    {
        for(int j=0;j<t;j++)
            dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
        for(int j=0;j<vec[i].size();j++)
            for(int k=0;k<t;k++)
                if(dp[i][k]!=-1)
                    dp[vec[i][j].fin][k|(1<<vec[i][j].num)]=max(dp[vec[i][j].fin][k|(1<<vec[i][j].num)],dp[i][k]+vec[i][j].val);
    }
    printf("%lld\n",dp[86400][t-1]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/83541615