P5583 题解
Melo.VVちゃん · · 题解
我们亲爱的 bitset 还是太神了。
我究竟是如何拿到最优解的。
考虑枚举区间右端点
- 如果当前区间
[L,R] 还没有包含所有喜欢的数字,必须往左扩展。 - 否则,如果当前集合有不喜欢的数字,这个集合一定不被加入我们枚举的区间里。
- 如果不符合上面两种情况,为了使魔力之和最大,这个集合一定是要加进来的。
那么就做完了,每个集合不喜欢数字的个数可以在输入的时候顺便算出来,喜欢的数字是否包含可以用 bitset 维护,总体复杂度
跑不满,但是看起来比较极限,不过很快就跑过去了,而且好像比正解还快,bitset 还是太神了。
Code
#include <bits/stdc++.h>
#define int long long
#define f(i ,m ,n ,x) for (int i = (m) ; i <= (n) ; i += (x))
template < typename T > inline void read ( T &x ) {
x = 0 ;
bool flag (0) ; char ch = getchar () ;
while (! isdigit (ch)) {
flag = ch == '-' ;
ch = getchar () ;
} while (isdigit (ch)) {
x = (x << 1) + (x << 3) + (ch ^ 48) ;
ch = getchar () ;
} flag ? x = - x : 0 ;
} template < typename T ,typename ...Args >
inline void read ( T &x ,Args &...tmp ) {
read (x) ,read (tmp...) ;
}
const int N = 3007 ;
int n ,m ,d ,no[N] ,t[N] ,l ,r ,min = 0x3f3f3f3f ,max = 0 ;
bool yes[N] ;
std :: bitset < N > have[N] ,zero ,cur ;
signed main () {
read (n ,m ,d) ;
f (i ,1 ,d ,1) {
int p ; read (p) ;
yes[p] = true ;
}
f (i ,1 ,n ,1) {
int num ; read (t[i] ,num) ;
f (j ,1 ,num ,1) {
int c ; read (c) ;
if (yes[c]) have[i][c] = true ;
else no[i] ++ ;
}
}
f (ed ,1 ,n ,1) {
bool ok (0) ;
cur &= zero ;
int cant = 0 ,mo = 0 ,bef = 0 ;
for (int st = ed ; st ; st --) {
if (ok && no[st] == 0) mo += t[st] ;
if (ok && no[st]) break ;
if (! ok) {
cur |= have[st] ;
cant += no[st] ;
mo += t[st] ;
if (static_cast < int > (cur.count ()) == d) ok = true ;
} bef = st ;
}
if (ok) {
if (cant < min) {
l = bef ,r = ed ;
min = cant ;
max = mo ;
} else if (cant == min && max < mo) {
max = mo ;
l = bef ,r = ed ;
}
}
} if (! l) return puts ("-1") ,0 ;
std :: cout << l << ' ' << r << '\n' ;
return 0 ;
}