Gym101908F. Music Festival (DP)

                                       F. Music Festival

time limit per test

1.0 s

memory limit per test

1024 MB

input

standard input

output

standard output

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

Copy

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

Copy

859

input

Copy

3
2 13 17 99 18 19 99
2 13 14 99 15 20 99
2 13 15 99 18 20 99

output

Copy

-1

 

一、原题地址

点我传送

 

二、大致题意

有n个舞台,每个舞台上有m场表演,每场表演有开始时间si 结束时间ei 以及一个权值wi,现在要求每场舞台的表演至少都要选择一场,其他随意选择,要观看的保证时间不重叠。(题目保证同一舞台的表演不会有重叠)。

询问最大能得到的权值是多少。

 

三、大致思路

由于n的范围只有10,所以想到用二进制来表达选择表演的状态,1表示在这个舞台至少选择了一场,0则表示没有选择。

开始时间和结束时间范围太大,首先需要离散化,这样就可以处理出多个小区间,选择用vector来记录该区间达到的右端r 、该区间属于的舞台fr 、该区间的权值val 。

用dp[ i ][ j ] 表示选择到 i 位置处状态为 j 时的最大权值。

状态转移:

dp[ now.r ][ k |(1<<(now.fr-1))]=max(dp[ now.r ][ k |(1<<(now.fr-1))],mic[ i ] [ j ].val+dp[ i ][ k ]) 。

其中k表示当前枚举到的状态。如果 k 不等于0的时候,表示之前一定选择过了区间,所以如果此时dp值为零,表示之前的状态还没有出现过,就不能进行更新。

 

当然为了保证中间状态不丢失,还需要

dp[ i ] [ k ]=max(dp[ i ][ k ],dp[ i-1 ][ k ])   来记录之前的状态。

 

四、代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int INF = 0x3f3f3f3f;
#define LL long long int
long long  gcd(long long  a, long long  b) { return a == 0 ? b : gcd(b % a, a); }



int n;
struct Node
{
    int l,r,val;
}a[15][1005];
int h[2005],num[15];
int tot,Size;
int dp[2005][(1<<10)+5];
struct Music
{
    int r,fr,val;
    Music(){}
    Music(int _r,int _fr,int _val)
    {
        r=_r;fr=_fr;val=_val;
    }
};
vector<Music>mic[2005];

void init()
{
    scanf("%d",&n);
    tot=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&num[i]);
        for(int j=1;j<=num[i];j++)
        {
            scanf("%d %d %d",&a[i][j].l,&a[i][j].r,&a[i][j].val);
            h[++tot]=a[i][j].l;
            h[++tot]=a[i][j].r;
        }
    }
    sort(h+1,h+1+tot);
    Size=unique(h+1,h+1+tot)-h-1;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=num[i];j++)
        {
            int lp=lower_bound(h+1,h+1+Size,a[i][j].l)-h;
            int rp=lower_bound(h+1,h+1+Size,a[i][j].r)-h;
            mic[lp].push_back(Music(rp,i,a[i][j].val));
        }
    }
}

void work()
{
    int ans=0;
    for(int i=1;i<=Size;i++)
    {
        for(int k=0;k<(1<<n);k++)
        {
            dp[i][k]=max(dp[i][k],dp[i-1][k]);
        }
        for(int j=0;j<mic[i].size();j++)
        {
            Music now=mic[i][j];
            for(int k=0;k<(1<<n);k++)
            {
                if(k==0)dp[now.r][(1<<(now.fr-1))]=max(dp[now.r][(1<<(now.fr-1))],mic[i][j].val);
                else
                {
                    if(dp[i][k]==0)continue;
                    else
                    {
                        dp[now.r][k|(1<<(now.fr-1))]=max(dp[now.r][k|(1<<(now.fr-1))],mic[i][j].val+dp[i][k]);
                    }
                }
            }
        }
        ans=max(ans,dp[i][(1<<n)-1]);
    }
    printf("%d\n",(ans?ans:-1));
}

int main()
{
    init();
    work();
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值