F - Music Festival Gym - 101908F

传送门:QAQ

题意:给你n组区间,每个居间都带有权值,让你选取一些区间使得区间不想交并且权值最大(要每组都至少有一个区间被选到)。

思路:n最多十组,我们就可以用二进制来记录当前唱歌的状态,然后离散化区间点(这样我们就可以用连续的点去表示这些区间),这样就有dp【i】【j】,i表示当前的区间点,j表示当前的状态,dp【i1】【j1】=dp【i】【j】+num【k】。每次寻找当前点往右 能更新的最近的一个区间(一切切的一切都是膜拜大佬的博客来的,离散化真是个好东西)

附上代码:

#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<iostream>
using namespace std;
struct inst {
	int x, y;
	int sud;
	int id;
};
inst ax[1100];
int dp[2100][1100];
int bn[2100];
int maxn[1100];
int tot, cnt;
map<int, int>q;
int cmp(inst a, inst b) {
	if (a.x!=b.x)return a.x < b.x;
	else return a.y < b.y;
}
int binary_s(int x) {
	int l = 0;
	int r = tot-1;
	int ans = tot-1;
	while (l <= r) {
		int mid = (l + r) / 2;
		if (ax[mid].x >= x) {
			ans = mid;
			r = mid - 1;
		}
		else l = mid + 1;
	}
	if (x > ax[ans].x) ans++;
	return ans;
}
int main(void) {
	int n;
	scanf("%d", &n);
	tot = 0;
	cnt = 0;
	q.clear();
	memset(maxn, 0x8f, sizeof(maxn));
	memset(dp, 0x8f, sizeof(dp));
	for (int i = 1; i <= n; i++) {
		int m;
		scanf("%d", &m);
		for (int z = 0; z < m; z++) {
			scanf("%d %d %d", &ax[tot].x, &ax[tot].y,&ax[tot].sud);
			ax[tot].id = i-1;
			bn[cnt++] = ax[tot].x;
			bn[cnt++] = ax[tot].y;
			tot++;
		}
	}
	sort(bn, bn + cnt);
	cnt = unique(bn,bn + cnt) - bn;
	sort(ax, ax + tot, cmp);
	for (int i = 0; i < cnt; i++) {
		q[bn[i]] = i;
	}
	for (int i = 0; i < tot; i++) {
		ax[i].x = q[ax[i].x];
		ax[i].y = q[ax[i].y];
	}
	dp[0][0] = maxn[0] = 0;
	for (int i = 0; i < cnt; i++) {
		for (int j = 0; j < (1 << n); j++) {
			dp[i][j] = maxn[j] = max(dp[i][j], maxn[j]);
			if (dp[i][j] < 0) continue;
			int pos=binary_s(i);
			int pre = -1;
			for (int z = pos; z < tot; z++) {
				if (pre == -1 || pre == ax[z].x) pre = ax[z].x;
				else break;
				int x1 = ax[z].y;
				int jj = j | (1 << ax[z].id);
				int sum = dp[i][j] + ax[z].sud;
				dp[x1][jj] = max(dp[x1][jj], sum);
			}
		}
	}
	if (maxn[(1 << n) - 1] >= 0) printf("%d\n", maxn[(1 << n) - 1]);
	else printf("-1\n");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/liexss/article/details/83511980