练习赛补题----F - 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≤101≤N≤10 representing the number of stages. The following NN lines describe the shows happening in each stage. The ii-th of which consists of an integer Mi≥1Mi≥1, representing the number of shows scheduled for the ii-th stage followed by MiMi show descriptions. Each show description contains 3 integers ij,fj,ojij,fj,oj (1≤ij<fj≤864001≤ij<fj≤86400; 1≤oj≤10001≤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 MiMi 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个舞台,每个舞台会表演一些节目,每个节目都会有一定的价值。选择一些节目进行观看,要求每个舞台都要至少看了一个节目且两个节目的时间段不能相交,问价值最大是多少
1≤N≤10,节目数量≤1000,1≤时间≤86400

最多有10个舞台,让人想到可能会用状压,那dp含义什么呢
dp[i][j]是i时间内所选舞台的状态为j的最大价值
遍历时间的时候,对每个时刻,对应选与不选
初始化:dp[i][0] = 0;一个都不选必定是0
不选:
dp[i][j] = max(dp[i][j],dp[i - 1][j]);
选舞台编号为k的第num个节目
ed = 该节目结束时间
val = 该节目的价值
状态应该变为 s = j | (1 << (k - 1));
dp[ed][s] = max(dp[ed][s],dp[i][j] + val);
复杂度大概是(86400+节目数)*(1<<n)
具体请见代码注释

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef struct Node{
    int ed,val,id;
}Node;
int dp[86700][1200];
vector<Node>ve[86700];

int main()
{
    int n;
    scanf("%d",&n);
    for(int i = 1;i <= n;++i){
        int k;
        scanf("%d",&k);
        while(k--){
            int a,b,c;
            scanf("%d %d %d",&a,&b,&c);
            ve[a].pb((Node){b,c,i});
        }
    }
    memset(dp,-1,sizeof(dp));
    for(int i = 1;i <= 86400;++i){
        dp[i][0] = 0; //i时间每个舞台都不选,答案为0
        //当前时间可以不选舞台,进行更新
        for(int j = 1;j <= ((1 << n) - 1);++j){
            dp[i][j] = max(dp[i][j],dp[i - 1][j]);
        }
        //当前时间可以选舞台
        int len = ve[i].size();
        for(int k = 0;k < len;++k){
            int ed = ve[i][k].ed;
            int val = ve[i][k].val;
            int id = ve[i][k].id;
            for(int j = 0;j <= ((1 << n) - 1);++j){
                if(dp[i][j] == -1) continue;  //表明当前状态没有更新值,说明这个状态是不可达的
                int s = j | (1 << (id - 1));
                dp[ed][s]= max(dp[ed][s],dp[i][j] + val);
            }
        }
    }
    printf("%d\n",dp[86400][(1 << n) - 1]);
    return 0;
}

发布了269 篇原创文章 · 获赞 33 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_36386435/article/details/88711979