GYM 101908F - Music Festival
GYM 101908F - Music Festival
做法:将节目按照右端点排序,\(dp[i][st][0/1]\)表示前i个节目,选择的stage的状态用一个n位二进制数表示为st,第i个节目选或不选的最大值,转移时用线段树优化一下。
#include <bits/stdc++.h>
#define pb push_back
typedef long long ll;
const int N = 20005;
const int inf = 0x3f3f3f3f3f;
using namespace std;
int n, m;
struct node { int l,r,o,id; } a[N];
int cnt = 0;
bool cmp(node a, node b) {
if(a.r != b.r) return a.r < b.r;
if(a.l != b.l) return a.l < b.l;
if(a.o != b.o) return a.o < b.o;
return a.id < b.id;
}
int dp[1002][1124][2];
int tree[11][1003<<2];
void add(int k, int p, int l, int r, int x, int v) {
if(l == r) {
tree[k][p] = v; return;
}
int mid = (l + r) >> 1;
if(x <= mid) add(k,p<<1,l,mid,x,v);
else add(k,p<<1|1,mid+1,r,x,v);
tree[k][p] = min(tree[k][p<<1], tree[k][p<<1|1]);
}
int P(int k, int p, int l, int r, int x) {
if(l == r) return l;
int mid = (l + r) >> 1;
if(tree[k][p<<1|1] <= x) return P(k,p<<1|1,mid+1,r,x);
else if(tree[k][p<<1] <= x) return P(k,p<<1,l,mid,x);
else return 0;
}
int main() {
scanf("%d",&n);
for(int i = 1; i <= n; ++i) {
scanf("%d",&m);
for(int j = 1; j <= m; ++j) {
++cnt; a[cnt].id = i;
scanf("%d %d %d",&a[cnt].l,&a[cnt].r,&a[cnt].o);
}
}
sort(a+1, a+1+cnt, cmp);
memset(tree,inf,sizeof(tree));
memset(dp,-1,sizeof(dp));
dp[0][0][0] = 0;
for(int i = 1; i <= cnt; ++i) {
for(int st = 0; st < (1<<n); ++st) {
if(dp[i-1][st][0] != -1) dp[i][st][0] = dp[i-1][st][0];
if(dp[i-1][st][1] != -1) dp[i][st][0] = max(dp[i][st][0],dp[i-1][st][1]);
if( !(st&(1<<(a[i].id-1))) ) continue;
for(int k = 1; k <= n; ++k) if( st&(1<<(k-1)) ) {
int pk = P(k,1,1,cnt,a[i].l);
int tmp = max(dp[pk][st][1], dp[pk][st^(1<<(a[i].id-1))][1]);
tmp = max(tmp, dp[pk][st][0]);
tmp = max(tmp, dp[pk][st^(1<<(a[i].id-1))][0]);
if(tmp != -1) dp[i][st][1] = max(dp[i][st][1], tmp+a[i].o);
}
}
add(a[i].id, 1, 1, cnt, i, a[i].r);
}
printf("%d\n",max(dp[cnt][(1<<n)-1][0],dp[cnt][(1<<n)-1][1]));
return 0;
}