讨论 / AC 100 pascal答案0ms 不超时 各位大牛们,假如有更简短的程序,请第一时间把程序经过qq邮箱发给我 qq邮箱:2637453212@qq.com
g++ 2016-03-23 19:43:12
点我顶贴 收藏 删除
var

n,m,i,j,t:longint;

a:array[-1..50500,1..2] of longint;

b:array[-1..50500] of longint;

c:array[-50500..50500] of longint;

procedure pretreatment;

var i,j:longint;

begin

b[2]:=1; b[1]:=a[1,1]; b[3]:=a[1,2];

for i:=3 to n-1 do begin

if a[b[i],1]=b[i-1] then begin

b[i+1]:=a[b[i],2];

end else begin

if a[b[i],2]=b[i-1] then b[i+1]:=a[b[i],1] else begin

writeln ('-1');

halt;

end;

end;

end;

if not((b[1]=a[b[n],1]) or (b[1]=a[b[n],2])) then begin

writeln ('-1');

halt;

end;

end;

procedure work;

var

i,t:longint;

begin

for i:=1-n to n-1 do c[i]:=0;

for i:=1 to n do begin

t:=b[i]-i;

if t<0 then t:=t+n;

inc(c[t]);

end;

for i:=0 to n-1 do if (n-c[i])<m then m:=n-c[i];

end;

begin

assign (input,'gin.txt');assign (output,'gout.txt');

reset(input);rewrite(output);

readln (n);

for i:=1 to n do readln (a[i,1],a[i,2]);

pretreatment;

m:=maxlongint;

work;

for i:=1 to n div 2 do begin

t:=b[i]; b[i]:=b[n-i+1]; b[n-i+1]:=t;

end;

work;

writeln (m);

end.

查看更多回复
提交回复