BZOJ1610: [Usaco2008 Feb]Line连线游戏

1610: [Usaco2008 Feb]Line连线游戏

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 1301  Solved: 571
[Submit][Status]

Description

Farmer John最近发明了一个游戏,来考验自命不凡的贝茜。游戏开始的时 候,FJ会给贝茜一块画着N (2 <= N <= 200)个不重合的点的木板,其中第i个点 的横、纵坐标分别为X_i和Y_i (-1,000 <= X_i <=1,000; -1,000 <= Y_i <= 1,000)。 贝茜可以选两个点画一条过它们的直线,当且仅当平面上不存在与画出直线 平行的直线。游戏结束时贝茜的得分,就是她画出的直线的总条数。为了在游戏 中胜出,贝茜找到了你,希望你帮她计算一下最大可能得分。

Input

* 第1行: 输入1个正整数:N

 * 第2..N+1行: 第i+1行用2个用空格隔开的整数X_i、Y_i,描述了点i的坐标

Output

第1行: 输出1个整数,表示贝茜的最大得分,即她能画出的互不平行的直线数

Sample Input

4
-1 1
-2 0
0 0
1 1

Sample Output

* 第1行: 输出1个整数,表示贝茜的最大得分,即她能画出的互不平行的直线数

HINT

 

4 输出说明: 贝茜能画出以下4种斜率的直线:-1,0,1/3以及1。

 

Source

题解:
早上起来又水了一发。。。
枚举所有可能的二元组,把它们的斜率放进数组,拍个序,扫一遍找答案即可
代码:
 1 const eps=1e-7;
 2 var n,i,j,tot,ans:longint;
 3     a:array[0..100000] of double;
 4     x,y:array[0..500] of longint;
 5     function cmp(x,y:double):boolean;
 6      begin
 7      if abs(x-y)<eps then exit(false);
 8      if x<y then exit(true) else exit(false);
 9      end;
10 
11 procedure sort(l,r:longint);
12  var i,j:longint;x,y:double;
13  begin
14  i:=l;j:=r;x:=a[(i+j)>>1];
15  repeat
16   while cmp(a[i],x) do inc(i);
17   while cmp(x,a[j]) do dec(j);
18   if i<=j then
19    begin
20    y:=a[i];a[i]:=a[j];a[j]:=y;
21    inc(i);dec(j);
22    end;
23  until i>j;
24  if i<r then sort(i,r);
25  if j>l then sort(l,j);
26  end;
27 begin
28   assign(input,'input.txt');assign(output,'output.txt');
29   reset(input);rewrite(output);
30   readln(n);tot:=0;
31   for i:=1 to n do readln(x[i],y[i]);
32   for i:=1 to n-1 do
33    for j:=i+1 to n do
34     begin
35       inc(tot);
36       if x[j]-x[i]<>0 then a[tot]:=(y[j]-y[i])/(x[j]-x[i])
37       else a[tot]:=maxlongint;
38     end;
39   sort(1,tot);
40   for i:=1 to tot do
41    if (i=1) or (cmp(a[i-1],a[i])) then inc(ans);
42   writeln(ans);
43   close(input);close(output);
44 end.   
View Code

 

posted @ 2014-08-12 07:26  ZYF-ZYF  Views(227)  Comments(0Edit  收藏  举报