vijos p1091(环球旅行)

模拟,单纯模拟会超时,但是有个定理:若从此地发现没油,则不必从头开始搜,直接从当前地开始就可以了。

 1 program P1091;
 2 var
 3         n,m,i,j,k,l,l2,h,q,x,w:longint;
 4         s,pow,ha:array[1..600000] of longint;
 5         bool:boolean;
 6 begin
 7         read(n,l);
 8         l2:=l;
 9         for i:=1 to n do
10                 begin
11                 read(s[i],pow[i]);
12                 if s[i]<=pow[i-1] then begin inc(h); ha[h]:=i-1; end;
13                 end;
14         if s[1]<=pow[n] then begin inc(h); ha[h]:=n end;
15         for i:=2 to n do
16                 l2:=l2-s[i];
17         s[1]:=l2;
18         i:=0;q:=0;
19         repeat
20                 bool:=true;
21                 inc(q);
22                 i:=q;
23                 x:=pow[i];
24                 while(((x>=s[i+1])and(i<n))or((x>=s[1]) and (i=n)))and(bool)do
25                         begin
26                         inc(i);
27                         if i=n+1 then i:=1;
28                         x:=x-s[i]+pow[i];
29                         if i=q
30                         then begin bool:=false; write(q,' '); w:=1;end;
31                         end;
32                if bool then if i>q then q:=i-1;
33         until (q=n);
34         {if w=0 then write('-1');}
35 end.  
posted @ 2012-10-14 21:22  改名字干什么  阅读(217)  评论(0编辑  收藏  举报