您的位置:首页 > 其它

BZOJ1562: [NOI2009]变换序列

2014-08-14 00:10 295 查看

1562: [NOI2009]变换序列

Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 1148 Solved: 599
[Submit][Status]

Description

const maxn=10000+100;maxm=20000+100;
type node=record
go,next:longint;
end;
var e:array[0..maxm] of node;
head,a,b,px,py,c:array[0..maxn] of longint;
v:array[0..maxn] of boolean;
cur,i,n,tot,ans,tmp:longint;
procedure swap(var x,y:longint);
var t:longint;
begin
t:=x;x:=y;y:=t;
end;

procedure insert(x,y:longint);
begin
inc(tot);
e[tot].go:=y;e[tot].next:=head[x];head[x]:=tot;
end;
function find(x:longint):boolean;
var i,y:longint;
begin
if x<=cur then exit(false);
i:=head[x];
while i<>0 do
begin
y:=e[i].go;
if not(v[y]) then
begin
v[y]:=true;
if (py[y]=-1) or (find(py[y])) then
begin
py[y]:=x;
px[x]:=y;
exit(true);
end;
end;
i:=e[i].next;
end;
exit(false);
end;
procedure init;
begin
readln(n);
for i:=0 to n-1 do
begin
read(c[i]);
a[i]:=(i+c[i]) mod n;
if n-abs(a[i]-i)<c[i] then a[i]:=-1;
b[i]:=(i+n-c[i]) mod n;
if abs(b[i]-i)<c[i] then b[i]:=-1;
if a[i]>b[i] then swap(a[i],b[i]);
if a[i]<>-1 then insert(i,a[i]);
if b[i]<>-1 then insert(i,b[i]);
writeln(a[i],' ',b[i]);
end;
end;
procedure main;
begin
for i:=0 to n-1 do py[i]:=-1;
cur:=-1;
for i:=0 to n-1 do
begin
fillchar(v,sizeof(v),false);
if find(i) then inc(ans);
end;
if ans<>n then begin writeln('No Answer');exit;end;
fillchar(v,sizeof(v),false);
for i:=0 to n-1 do
begin
if px[i]<>a[i] then
begin
cur:=i;
fillchar(v,sizeof(v),false);
tmp:=py[a[i]];
py[a[i]]:=i;
py[b[i]]:=-1;
v[a[i]]:=true;
if find(tmp) then
begin
px[i]:=a[i]
end
else
begin
py[a[i]]:=tmp;
py[b[i]]:=i;
end;
end;
end;
write(px[0]);for i:=1 to n-1 do write(' ',px[i]);
end;
begin
assign(input,'input.txt');assign(output,'output.txt');
reset(input);rewrite(output);
init;
main;
close(input);close(output);
end.


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: