记录编号 48779 评测结果 AAAAAAAA
题目名称 过河 最终得分 100
用户昵称 Gravatarduwei 是否通过 通过
代码语言 Pascal 运行时间 0.114 s
提交时间 2012-11-06 15:50:16 内存使用 0.18 MiB
显示代码纯文本
var n,i,j,k,l:longint; 
    a,b:array[1..1000]of longint; 
    q:array[0..1,0..1010]of longint; 
    p:array[0..1]of longint; 
    v:Array[0..1010]of boolean;
function fit(x,y:longint):boolean;
  var i,j:longint;
  begin
    if y=0 then exit(true);
    if y=n+1 then exit(true); 
    i:=x mod(a[y]+b[y]);
    if i=0 then exit(false); 
    if i<=a[y] then exit(true) else exit(false);
  end;
begin
  assign(input,'rivera.in'); assign(output,'rivera.out');
  reset(input); rewrite(output); 
  readln(n);
  for i:=1 to n do readln(a[i],b[i]); 
  q[0,0]:=0; p[0]:=1;
  for i:=1 to 10000 do 
    begin   
      fillchar(v,sizeof(v),false);  
      for j:=1 to p[(i+1) and 1] do    
         begin     
            for k:=q[i and 1,j]-5 to q[i and 1,j]+5 do
              if k=n+1 then    
                 begin 
                   write(i);
                   close(input); close(output);
                   halt;
                 end 
                 else      
                   if(k>=0)and(k<=n+1)and(fit(i,k))and not(v[k])then 
                     begin    
                       inc(p[i and 1]);
                       q[(i+1)and 1,p[i and 1]]:=k;
                       v[k]:=true; 
                     end;    
            p[(i+1)and 1]:=0; 
         end;  
    end;
  write('NO');
  close(input); close(output);
end.