维护一个小根堆,先把每种珠子的出现第一个位置放入堆中,每次取出彩带最左边的珠子
放入取出同类珠子的下一颗同种类的珠子,并且更新彩带最优解。
直到取出的珠子是这类珠子的最右边的一颗时候结束
1 type link=^node; 2 node=record 3 loc:longint; 4 next:link; 5 end; 6 point=record 7 loc,num:longint; 8 end; 9 10 var w,b:array[0..70] of link; 11 heap:array[0..10010] of point; 12 n,k,s,x,i,j,y,t,ans:longint; 13 p,q:link; 14 15 function min(a,b:longint):longint; 16 begin 17 if a>b then exit(b) else exit(a); 18 end; 19 20 procedure swap(var a,b:point); 21 var c:point; 22 begin 23 c:=a; 24 a:=b; 25 b:=c; 26 end; 27 28 procedure up(i:longint); 29 var j:longint; 30 begin 31 j:=i shr 1; 32 while j>0 do 33 begin 34 if heap[i].loc<heap[j].loc then 35 begin 36 swap(heap[i],heap[j]); 37 i:=j; 38 j:=i shr 1; 39 end 40 else break; 41 end; 42 end; 43 44 procedure sift(i:longint); 45 var j:longint; 46 begin 47 j:=i shl 1; 48 while j<=k do 49 begin 50 if (j+1<=k) and (heap[j].loc>heap[j+1].loc) then inc(j); 51 if heap[i].loc>heap[j].loc then 52 begin 53 swap(heap[i],heap[j]); 54 i:=j; 55 j:=i shl 1; 56 end 57 else break; 58 end; 59 end; 60 61 begin 62 readln(n,k); 63 for i:=1 to k do 64 begin 65 read(s); 66 w[i]:=nil; 67 for j:=1 to s do 68 begin 69 read(x); 70 new(p); 71 p^.next:=nil; 72 p^.loc:=x; 73 if w[i]=nil then 74 begin 75 w[i]:=p; 76 q:=p; 77 end 78 else begin 79 q^.next:=p; 80 q:=p; 81 end; 82 end; 83 b[i]:=w[i]; 84 readln; 85 end; 86 y:=0; 87 for i:=1 to k do 88 begin 89 t:=t+1; 90 heap[t].loc:=b[i]^.loc; 91 heap[t].num:=i; 92 up(t); 93 if heap[t].loc>y then y:=heap[t].loc; 94 b[i]:=b[i]^.next; 95 end; 96 ans:=y-heap[1].loc; 97 while true do 98 begin 99 x:=heap[1].num; 100 if b[x]=nil then break 101 else begin 102 heap[1].loc:=b[x]^.loc; 103 if y<b[x]^.loc then y:=b[x]^.loc; //维护彩带最右端 104 b[x]:=b[x]^.next; 105 sift(1); 106 ans:=min(ans,y-heap[1].loc); 107 end; 108 end; 109 writeln(ans); 110 end.
type link=^node;
node=record
loc:longint;
next:link;
end;
point=record
loc,num:longint;
end;
var w,b:array[0..70] of link;
heap:array[0..10010] of point;
n,k,s,x,i,j,y,t,ans:longint;
p,q:link;
function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end;
procedure swap(var a,b:point);
var c:point;
begin
c:=a;
a:=b;
b:=c;
end;
procedure up(i:longint);
var j:longint;
begin
j:=i shr 1;
while j>0 do
begin
if heap[i].loc<heap[j].loc then
begin
swap(heap[i],heap[j]);
i:=j;
j:=i shr 1;
end
else break;
end;
end;
procedure sift(i:longint);
var j:longint;
begin
j:=i shl 1;
while j<=k do
begin
if (j+1<=k) and (heap[j].loc>heap[j+1].loc) then inc(j);
if heap[i].loc>heap[j].loc then
begin
swap(heap[i],heap[j]);
i:=j;
j:=i shl 1;
end
else break;
end;
end;
begin
readln(n,k);
for i:=1 to k do
begin
read(s);
w[i]:=nil;
for j:=1 to s do
begin
read(x);
new(p);
p^.next:=nil;
p^.loc:=x;
if w[i]=nil then
begin
w[i]:=p;
q:=p;
end
else begin
q^.next:=p;
q:=p;
end;
end;
b[i]:=w[i];
readln;
end;
y:=0;
for i:=1 to k do
begin
t:=t+1;
heap[t].loc:=b[i]^.loc;
heap[t].num:=i;
up(t);
if heap[t].loc>y then y:=heap[t].loc;
b[i]:=b[i]^.next;
end;
ans:=y-heap[1].loc;
while true do
begin
x:=heap[1].num;
if b[x]=nil then break
else begin
heap[1].loc:=b[x]^.loc;
if y<b[x]^.loc then y:=b[x]^.loc; //维护彩带最右端
b[x]:=b[x]^.next;
sift(1);
ans:=min(ans,y-heap[1].loc);
end;
end;
writeln(ans);
end.