http://poj.org/problem?id=2318
___________________________________________________
题目大意:一个箱子用木板分成几个区间,给一些玩具的坐标,求各个区间有几个玩具。
对于每一个玩具,因为输入的区间是有序的,所以采用二分木板,判断一个玩具在哪个区间。
___________________________________________________
1 Program Stone; 2 var i,n,m,x1,x2,y1,y2,xi,yi:longint; 3 ui,li:array[0..10000]of longint; 4 ans:array[0..10000]of longint; 5 procedure init; 6 var i,j,k:longint; 7 begin 8 readln(n,m,x1,y1,x2,y2); 9 ui[0]:=x1;li[0]:=x1; 10 ui[n+1]:=x2;li[n+1]:=x2; 11 if n=0 then halt; 12 for i:=1 to n do readln(ui[i],li[i]); 13 end; 14 Function cross(x1,y1,x2,y2,x3,y3,x4,y4:longint):longint; //叉积 15 begin 16 cross:=(x2-x1)*(y4-y3)-(y2-y1)*(x4-x3); 17 end; 18 function ef(x,y:longint):longint; 19 var i,j,k,left,right,mid:longint; 20 begin 21 left:=0;right:=n+1; 22 while left+1<right do 23 begin 24 mid:=(left+right)div 2; 25 k:=cross(li[mid],y2,ui[mid],y1,li[mid],y2,x,y); 26 if k<0 then left:=mid; 27 if k>0 then right:=mid; 28 end; 29 ef:=left; 30 end; 31 Procedure print; 32 var i:longint; 33 begin 34 for i:=0 to n do 35 writeln(i,‘: ‘,ans[i]); 36 writeln; 37 fillchar(ans,sizeof(ans),0); 38 end; 39 Begin 40 assign(input,‘input.in‘);reset(input); 41 while true do 42 begin 43 init; 44 for i:=1 to m do 45 begin 46 readln(xi,yi); 47 inc(ans[ef(xi,yi)]); 48 end; 49 print; 50 end; 51 end. 52 53
时间: 2024-10-05 14:54:40