cloned from srcbox.
[furry-nemesis.git] / int.pas
blobdbf4a3a828a91ceb20c8af2ce60a9458f2313aa9
1 var
2 a:array [0..10000] of longint;
3 data:array [0..1000,1..2] of longint;
4 f:array [0..1000] of longint;
5 n,i,j,max1,maxk1,max2,maxk2,ans,k:longint;
7 procedure qsort(l,r:longint);
8 var
9 i,j,m:longint;
11 begin
12 i:=l; j:=r;
13 m:=data[(l+r) div 2,1];
14 repeat
15 while data[i,1]<m do inc(i);
16 while data[j,1]>m do dec(j);
17 if i<=j then
18 begin
19 data[0]:=data[i]; data[i]:=data[j]; data[j]:=data[0];
20 inc(i); dec(j);
21 end;
22 until i>j;
23 if l<j then qsort(l,j);
24 if r>i then qsort(i,r);
25 end;
27 begin
28 readln(n);
29 for i:=1 to n do readln(data[i,1],data[i,2]);
30 qsort(1,n);
31 for i:=1 to n-1 do
32 if data[i,1]=data[i+1,1] then
33 begin
34 if data[i,2]<data[i+1,2] then
35 begin
36 data[i+1,1]:=maxint; data[i+1,2]:=maxint;
37 end
38 else
39 begin
40 data[i,1]:=maxint; data[i,2]:=maxint;
41 end;
42 end;
43 qsort(1,n);
44 while data[n,1]=maxint do dec(n);
45 for i:=1 to n do
46 for j:=data[i,1] to data[i,2] do
47 inc(a[j]);
48 fillchar(f,sizeof(f),0);
49 for i:=1 to n do
50 begin
51 if f[i]>=2 then continue;
52 max1:=a[data[i,1]]; maxk1:=data[i,1];
53 max2:=a[data[i,2]]; maxk1:=data[i,2];
54 for k:=data[i,1]+1 to data[i,2]-1 do
55 if a[k]>max1 then begin max2:=max1; maxk2:=maxk1; max1:=a[k]; maxk1:=k; end
56 else if a[k]>max2 then begin max2:=a[k]; maxk2:=k; end;
57 if f[i]=0 then
58 begin
59 inc(ans); a[maxk2]:=0;
60 j:=i+1;
61 while (maxk2>=data[j,1]) and (maxk2<=data[j,2]) do
62 begin
63 inc(f[j]); inc(j);
64 end;
65 end;
66 inc(ans); a[maxk1]:=0;
67 j:=i+1;
68 while (maxk1>=data[j,1]) and (maxk1<=data[j,2]) do
69 begin
70 inc(f[j]); inc(j);
71 end;
72 end;
73 writeln(ans-1);
74 end.