NOI2002 荒岛野人

这题其实黑书上有,只是我脑残的没想起来……

其实就是拓展欧几里得算法

我参看的题解:http://www.cnblogs.com/Rinyo/archive/2012/11/25/2788373.html

还有一个讲解的很清楚的exgcd:http://www.cnblogs.com/Rinyo/archive/2012/11/25/2787419.html

代码:

 1 var c,p,l:array[0..50] of longint;
 2     ans,now,n,i,j,d,x,y,pp,cc:longint;
 3     flag:boolean;
 4 function min(x,y:longint):longint;
 5  begin
 6  if x<y then exit(x) else exit(y);
 7  end;
 8 function max(x,y:longint):longint;
 9  begin
10  if x>y then exit(x) else exit(y);
11  end;
12 function exgcd(a,b:longint;var x,y:longint):longint;
13  var t,d:longint;
14  begin
15  if b=0 then
16   begin
17   x:=1;y:=0;exit(a);
18   end;
19  d:=exgcd(b,a mod b,x,y);
20  t:=x;x:=y;y:=t-(a div b)*y;
21  exit(d);
22  end;
23 procedure main;
24  begin
25  readln(n);
26  for i:=1 to n do
27   begin
28   readln(c[i],p[i],l[i]);
29   ans:=max(ans,c[i]);
30   end;
31  while true do
32   begin
33   flag:=true;
34   for i:=1 to n-1 do
35    begin
36     for j:=i+1 to n do
37      begin
38      pp:=p[i]-p[j];cc:=c[j]-c[i];
39      d:=exgcd(pp,ans,x,y);
40      if cc mod d<>0 then continue;
41      now:=x*cc div d;
42      now:=now mod (ans div d);
43      if now<0 then inc(now,abs(ans div d));
44      if now<=min(l[i],l[j]) then begin flag:=false;break;end;
45      end;
46     if not(flag) then break;
47     end;
48   if flag then break;
49   inc(ans);
50   end;
51  writeln(ans);
52  end;
53 begin
54  main;
55 end.                         
View Code

我要开始刷noi历年题了!我相信我一定能够坚持下来!

posted @ 2014-06-11 12:51  ZYF-ZYF  Views(364)  Comments(0Edit  收藏  举报