P8893 「UOI-R1」智能推荐

本来以为出了个板子,但好像没有被喷太惨。

题目出来后,出题人其实给了一个三次方的做法,而枚举天数暴力算也是三次方的。

而在公开赛几天前,这玩意被加强了,原因是出题人突然发现可以拓扑排序。

然后暴力被卡成 9090 分。但是我们可以考虑 bitset 优化。显然每天肯定能把现在能做的全部做完,然后枚举每一条规则,看能否解锁新的题目。理论上这一部分需要 O(n)O(n) 判断,但是可以考虑 bitset 优化,复杂度变为 O(nw)O(\dfrac{n}{w}),即可通过。

于是 bitset 过了,复杂度 O(n3w)O(\dfrac{n^3}{w}),拓扑排序应该是 O(n2)O(n^2) 的。

代码实现:

#include <bits/stdc++.h>
using namespace std;

#define ll long long

const int N = 5e3 + 5, INF = 2e9, MOD = 1e9 + 7;

inline int read()
{
	int op = 1, x = 0;
	char ch = getchar();
	while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
	while (ch == '-')
	{
		op = -op;
		ch = getchar();
	}
	while (ch >= '0' and ch <= '9')
	{
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * op;
}

inline void write(int x)
{
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}

int n, k, p, g[N], r, f[N];
bitset<N> v, vv; 
bitset<N> q[N];

int main()
{
	// freopen("*.in", "r", stdin);
	// freopen("*.out", "w", stdout);
	int ans = 0;
	scanf("%d%d%d", &n, &k, &p);
	for (int i = 1; i <= p; i++)
	{
		int x;
		scanf("%d", &x);
		v[x] = 1;
	}
	scanf("%d", &r);
	if (r > n) return -1;
	for (int i = 1; i <= r; i++)
	{
		scanf("%d%d", &g[i], &f[i]);
		if (g[i] > n) return -1;
		int dd;
		for (int j = 1; j <= f[i]; j++)
		{
			scanf("%d", &dd);
			q[i][dd] = 1;
		}
	}
	int now = 0;
	while (now < 10000)
	{
		if (v[k])
		{
			printf("%d\n", now);
			return 0;
		}
		vv = v;
		for (int i = 1; i <= r; i++)
		{
			if (v[g[i]]) continue;
			bitset<N> kkk = q[i] & vv;
			if (kkk == q[i])
			{
				v[g[i]] = 1;
			}
		}
		now++;
	}
	printf("-1\n");
	return 0;
}
posted @   HappyBobb  阅读(24)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示