Gym101908F. Music Festival (DP)

本文介绍了一种解决音乐节选场问题的算法,通过离散化和动态规划技术,实现从多个舞台上选择不冲突的表演,以最大化用户已听歌曲数量的目标。使用二进制状态表示选择状态,优化了在有限舞台上的选择过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                       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();
}

 

### 使用 `gym.spaces.Box` 定义动作空间 在OpenAI Gym环境中定义连续的动作空间通常会使用到 `gym.spaces.Box` 类。此类允许创建一个多维的盒子形状的空间,其边界由低限(low)和高限(high)参数指定[^1]。 对于给定的例子,在类 `ActionSpace` 中静态方法 `from_type` 返回了一个基于输入类型的行动空间实例: 当 `space_type` 是 `Continuous` 时,返回的是一个三维向量形式的动作空间对象,该对象表示三个维度上的取值范围分别为 `[0.0, 1.0]`, `[0.0, 1.0]`, 和 `[-1.0, 1.0]` 的实数集合,并且数据类型被设定为了 `np.float32`: ```python import numpy as np import gym class ActionSpace: @staticmethod def from_type(action_type: int): space_type = ActionSpaceType(action_type) if space_type == ActionSpaceType.Continuous: return gym.spaces.Box( low=np.array([0.0, 0.0, -1.0]), high=np.array([1.0, 1.0, 1.0]), dtype=np.float32, ) ``` 此段代码展示了如何通过传递最低限度(`low`)数组以及最高限度(`high`)数组来初始化一个新的Box实例,从而构建出一个具有特定界限的多维连续数值区间作为环境可能采取的一系列合法行为的选择集的一部分。 另外值得注意的是,每个环境都应当具备属性 `action_space` 和 `observation_space` ,这两个属性应该是继承自 `Space` 类的对象实例;Gymnasium库支持大多数用户可能会需要用到的不同种类的空间实现方式[^2]。 #### 创建并测试 Box 动作空间的一个简单例子 下面是一个简单的Python脚本片段用于展示怎样创建和验证一个基本的 `Box` 空间成员资格的方法: ```python def check_box_space(): box_space = gym.spaces.Box(low=-1.0, high=1.0, shape=(2,), dtype=np.float32) sample_action = box_space.sample() # 获取随机样本 is_valid = box_space.contains(sample_action) # 检查合法性 print(f"Sampled action {sample_action} within bounds? {'Yes' if is_valid else 'No'}") check_box_space() ``` 上述函数首先建立了一个二维的 `-1.0` 到 `1.0` 范围内的浮点型 `Box` 空间,接着从中抽取了一组随机样本来检验它确实位于所规定的范围内。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值