典型的状压DP,第一次我写的太丑了。。。,更坚定的我的信念,一定要把问题分析透彻再code!
代码:
const maxn=10000;
var w,f:array[1..maxn,0..31] of longint;
n,ans:longint;
procedure init;
var c,p,k1,k2,hate,happy,j,d:longint;
begin
fillchar(w,sizeof(w),0);
readln(n,c);
while c>0 do begin dec(c);
read(p,k1,k2);
hate:=0; while k1>0 do begin dec(k1);
read(d); hate:=hate or (1 shl (4-(d+n-p)mod n));
end;
happy:=0; while k2>0 do begin dec(k2);
read(d); happy:=happy or (1 shl (4-(d+n-p)mod n));
end;
for j:=0 to 31 do
if (j and hate<>0)or((not j)and happy<>0) then inc(w[p,j]);
readln;
end;
end;
function max(a,b:longint):longint; begin if a>b then exit(a) else exit(b) end;
procedure work;
var p,j,i:longint;
begin
for j:=0 to 31 do f[1,j]:=-maxlongint;
ans:=-1;
for p:=0 to 31 do begin
f[1,p]:=w[1,p];
for i:=2 to n do
for j:=0 to 31 do
f[i,j]:=w[i,j]+max(f[i-1,j shr 1],f[i-1,(j shr 1)or 16]);
ans:=max(ans,max(f[n,p shr 1],f[n,(p shr 1)or 16]));
f[1,p]:=-maxlongint;
end;
write(ans);
end;
begin
assign(input,'zoo.in'); reset(input);
assign(output,'zoo.out'); rewrite(output);
init;
work;
close(input);
close(output);
end.