原题网址:http://www.lydsy.com/JudgeOnline/problem.php?id=3523
很显然的贪心,每次找不和前面元素重复的最多的,尾部颜色在数量相同时优先级更高。WA了好多发因为姿势丑,如果堆顶元素和前面元素一样只要先把堆顶拿出来就好了。。
type
rec=record
c,cnt:longint;
end;
const
MAXN=1000050;
var
heap:array[0..MAXN] of rec;
a,c,cp:array[0..MAXN] of longint;
n,m,st,nd,i,cnt:longint;
t:rec;
procedure swp(var a,b:longint);
var t:longint;
begin t:=a;a:=b;b:=t; end;
procedure swap(a,b:longint);
begin
swp(heap[a].c,heap[b].c);
swp(heap[a].cnt,heap[b].cnt);
end;
operator >(a,b:rec)c:boolean;
begin exit((a.cnt>b.cnt)or(a.cnt=b.cnt)and(a.c=nd));end;
procedure heapup(x:longint);
begin
while ((x>1)and(heap[x]>heap[x>>1])) do
begin
swap(x,x>>1);
x:=x>>1;
end;
end;
procedure heapdown(x:longint);
var
e:longint;
begin
while ((x<<1<=cnt)and(heap[x<<1]>heap[x])or(x<<1+1<=cnt)and(heap[x<<1+1]>heap[x])) do
begin
e:=x<<1+ord((x<<1+1<=cnt)and(heap[x<<1+1].cnt>heap[x<<1].cnt));
swap(x,e);
x:=e;
end;
end;
procedure push(x:rec);
begin
inc(cnt);
heap[cnt]:=x;
heapup(cnt);
end;
procedure poop;
begin
heap[1]:=heap[cnt];
dec(cnt);
heapdown(1);
end;
procedure no_solution;
begin
writeln(0);
halt;
end;
begin
read(n,st,nd);m:=0;
for i:=1 to n do read(a[i]);
cp:=a;
for i:=1 to n do inc(m,a[i]);
dec(a[st]);if (a[st]<0) then no_solution;
dec(a[nd]);if (a[nd]<0) then no_solution;
for i:=1 to n do
if (a[i]>0) then
begin
t.c:=i;
t.cnt:=a[i];
push(t);
end;
c[1]:=st;c[m]:=nd;
for i:=2 to m-1 do
if (heap[1].c<>c[i-1])
then
begin
c[i]:=heap[1].c;
dec(heap[1].cnt);
if (heap[1].cnt=0)
then poop
else heapdown(1);
end
else
begin
if (cnt=1) then no_solution;
t:=heap[1];poop;
c[i]:=heap[1].c;
dec(heap[1].cnt);
if (heap[1].cnt=0)
then poop
else heapdown(1);
push(t);
end;
if (c[m]=c[m-1]) then no_solution;
for i:=1 to m-1 do
write(c[i],' ');
writeln(c[m]);
end.