您的位置:首页 > 其它

【杭电oj】1432 - Lining Up(数论)

2016-04-12 17:26 417 查看


Lining Up

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1328    Accepted Submission(s): 377


Problem Description

``How am I ever going to solve this problem?" said the pilot. 

Indeed, the pilot was not facing an easy task. She had to drop packages at specific points scattered in a dangerous area. Furthermore, the pilot could only fly over the area once in a straight line, and she had to fly over as many points as possible. All points
were given by means of integer coordinates in a two-dimensional space. The pilot wanted to know the largest number of points from the given set that all lie on one line. Can you write a program that calculates this number? 

Your program has to be efficient! 

 

Input

The input consists of multiple test cases, and each case begins with a single positive integer on a line by itself indicating the number of points, followed by N pairs of integers, where 1 < N < 700. Each pair of integers is separated by one blank and ended
by a new-line character. No pair will occur twice in one test case. 

 

Output

For each test case, the output consists of one integer representing the largest number of points that all lie on one line, one line per case.

 

Sample Input

5
1 1
2 2
3 3
9 10
10 11

 

Sample Output

3

 

Source

ACM暑期集训队练习赛(四)

利用三点共线的性质暴力求解就行了。

题中第三重for循环从 j + 1 开始个人觉得思路上挺好的,毕竟我的第一想法是从头搜索,这样三重 for 循环浪费的时间不少呢。

代码如下:

#include <cstdio>
struct node
{
int x,y;
}data[711];
bool check (node a,node b,node c)
{
if ((a.x - b.x) * (a.y - c.y) - (a.x - c.x) * (a.y - b.y) == 0)
return true;
return false;
}
int max (int a,int b)
{
if (a > b)
return a;
return b;
}
int main()
{
int n;
int ans;
int amt;
while (~scanf ("%d",&n))
{
for (int i = 1 ; i <= n ; i++)
scanf ("%d %d",&data[i].x,&data[i].y);
ans = -1;
for (int i = 1 ; i < n ; i++)
{
for (int j = i + 1 ; j <= n ; j++)
{
amt = 2;
// for (int k = 1 ; k <= n ; k++) //这样从头搜索还是会超时的
// {
// if (k == j || k == i)
// continue;
// if (check (data[i],data[j],data[k]))
// amt++;
// }
for (int k = j + 1 ; k <= n ; k++) //个人觉得这样从下一个点搜索挺巧的(差距啊)
{
if (check (data[i],data[j],data[k]))
amt++;
}
ans = max (ans,amt);
}
}
printf ("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: