10927 - Bright Lights
Posted: Sun Oct 09, 2005 5:27 pm
Anyone could tell me how to solve this problem?
Code: Select all
Const
error= 10e-7;
Type
punto= record
m: extended;
r,x,y,z: longint;
end;
Var
c,i,n,t: longint;
p: array [1..100000] of punto;
Function menor_m(a,b: punto): boolean;
begin
if (a.m + error< b.m) or
((abs(a.m -b.m)< error) and (a.z< b.z)) or
((abs(a.m -b.m)< error) and (a.z= b.z) and (sqr(a.x) + sqr(a.y) < sqr(b.x) + sqr(b.y)))
then menor_m:= true
else menor_m:= false;
end;
Function menor_x(a,b: punto): boolean;
begin
if (a.r < b.r) or
((a.r= b.r) and (a.x< b.x)) or
((a.r= b.r) and (a.x= b.x) and (a.y < b.y))
then menor_x:= true
else menor_x:= false;
end;
Procedure Qsort(l,r,m: longint);
var x,t: punto;
i,j: longint;
begin
i:= l; j:= r; x:= p[(l+r) shr 1];
repeat
if (m=0) then while menor_m(p[i], x) do Inc(i)
else while menor_x(p[i], x) do Inc(i);
if (m=0) then while menor_m(x, p[j]) do Dec(j)
else while menor_x(x, p[j]) do Dec(j);
if (i<=j) then
begin
t:= p[i]; p[i]:= p[j]; p[j]:= t;
Inc(i); Dec(j);
end;
until (i>j);
if (l<j) then Qsort(l,j,m);
if (i<r) then Qsort(i,r,m);
end;
Begin
c:= 0;
readln(input,n);
while (n<>0) do
begin
Inc(c);
for i:= 1 to n do
with (p[i]) do
begin
r:= 1;
readln(input,x,y,z);
if (x<>0) then m:= y/x
else m:= 1e100;
if (y=0) and (x<0) then m:= -0.1234567;
end;
Qsort(1,n,0);
t:= 0;
for i:= 1 to n-1 do
if (p[i].m= p[i+1].m) and (p[i].z= p[i+1].z) then
begin
Inc(t);
p[i+1].r:= 0;
end;
writeln(output,'Data set ',c,':');
if (t= 0) then writeln(output,'All the lights are visible.')
else begin
writeln(output,'Some lights are not visible:');
Qsort(1,n,1);
for i:= 1 to t-1 do
writeln(output,'x = ',p[i].x,', y = ',p[i].y,';');
writeln(output,'x = ',p[t].x,', y = ',p[t].y,'.');
end;
readln(input,n);
end;
End.
I think precision is not the case here. Why didn't you put comments to your code, explaining your idea?Pier wrote:I keep getting WA. I think it might be because of precision errors (I'm not very used to geometric problems). Any help appreciated.
Here I define a point, x,y,z are coordinates, m is the slope (using 0,0 as the other point) and r is used to indicate if it's visible (1= visible, 0= not visible).Const
error= 10e-7;
I declare variables, c is for counting the number of tests, i is for loops, n is the number of points, t is the total of not visible points and p is where I store the points.Type
punto= record
m: extended;
r,x,y,z: longint;
end;
This funtion sorts the points by slope, then by z and then by distance.Var
c,i,n,t: longint;
p: array [1..100000] of punto;
This funtion sorts the points by slope, then by z and then by distance.Function menor_m(a,b: punto): boolean;
begin
if (a.m + error< b.m) or
((abs(a.m -b.m)< error) and (a.z< b.z)) or
((abs(a.m -b.m)< error) and (a.z= b.z) and (sqr(a.x) + sqr(a.y) < sqr(b.x) + sqr(b.y)))
then menor_m:= true
else menor_m:= false;
end;
The sorting funtion.Function menor_x(a,b: punto): boolean;
begin
if (a.r < b.r) or
((a.r= b.r) and (a.x< b.x)) or
((a.r= b.r) and (a.x= b.x) and (a.y < b.y))
then menor_x:= true
else menor_x:= false;
end;
Procedure Qsort(l,r,m: longint);
var x,t: punto;
i,j: longint;
begin
i:= l; j:= r; x:= p[(l+r) shr 1];
repeat
if (m=0) then while menor_m(p, x) do Inc(i)
else while menor_x(p, x) do Inc(i);
if (m=0) then while menor_m(x, p[j]) do Dec(j)
else while menor_x(x, p[j]) do Dec(j);
if (i<=j) then
begin
t:= p; p:= p[j]; p[j]:= t;
Inc(i); Dec(j);
end;
until (i>j);
if (l<j) then Qsort(l,j,m);
if (i<r) then Qsort(i,r,m);
end;
[/code]
Begin
c:= 0;
readln(input,n);
while (n<>0) do
begin
Inc(c);
for i:= 1 to n do
with (p) do
begin
r:= 1;
readln(input,x,y,z);
if (x<>0) then m:= y/x
else m:= 1e100;
if (y=0) and (x<0) then m:= -0.1234567;
end;
Qsort(1,n,0);
t:= 0;
for i:= 1 to n-1 do
if (p.m= p[i+1].m) and (p.z= p[i+1].z) then
begin
Inc(t);
p[i+1].r:= 0;
end;
writeln(output,'Data set ',c,':');
if (t= 0) then writeln(output,'All the lights are visible.')
else begin
writeln(output,'Some lights are not visible:');
Qsort(1,n,1);
for i:= 1 to t-1 do
writeln(output,'x = ',p.x,', y = ',p.y,';');
writeln(output,'x = ',p[t].x,', y = ',p[t].y,'.');
end;
readln(input,n);
end;
End.
Code: Select all
AC