P1689: [Usaco2005 Open] Muddy roads 泥泞的路

水题,模拟就行了,别忘了L>=r的时候直接更新下一个的L然后continue

 1 type
 2   node=record
 3     l,r:longint;
 4   end;
 5 var n,l,i,ans:longint;
 6 k:array[0..10001] of node;
 7 function ma(a,b:longint):longint;
 8 begin
 9   if a>b then exit(a) else exit(b);
10 end;
11 procedure qs(t,w:longint);
12 var mid,l,r:longint;
13 tem:node;
14 begin
15   l:=t; r:=w; mid:=k[(l+r) shr 1].l;
16   repeat
17     begin
18       while k[l].l<mid do inc(l);
19       while k[r].l>mid do dec(r);
20       if l<=r then
21         begin
22           tem:=k[l];
23           k[l]:=k[r];
24           k[r]:=tem;
25           inc(l); dec(r);
26         end;
27     end;
28   until l>r;
29   if t<r then qs(t,r);
30   if l<w then qs(l,w);
31 end;
32 begin
33   readln(n,l);
34   for i:=1 to n do
35     readln(k[i].l,k[i].r);
36   qs(1,n);
37   for i:=1 to n do
38     begin
39       if k[i].l>=k[i].r then
40         begin
41           k[i+1].l:=ma(k[i+1].l,k[i].l);
42           continue;
43         end;
44       inc(ans,(k[i].r-k[i].l) div l);
45       if ((k[i].r-k[i].l) div l)*l<>k[i].r-k[i].l then
46         begin
47           inc(ans);
48           k[i+1].l:=ma(k[i+1].l,(((k[i].r-k[i].l) div l)+1)*l+k[i].l);
49         end;
50     end;
51   writeln(ans);
52 end.
View Code

 (转载请注明出处:http://www.cnblogs.com/Kalenda/)

posted @ 2015-09-22 18:52  LovelyMonster丶  阅读(194)  评论(0编辑  收藏  举报