【杭电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;
}
相关文章推荐
- Chrome 的插件JSONView 格式化转换本地的文件
- Unix网络编程一、二章笔记
- C语言之指针
- 数据存储优化
- Android ProGuard代码混淆
- javascript全局变量与局部变量
- Android 开发进阶指南
- 当ListView有Header时,onItemClick里的position不正确。
- 大数据面试题
- Java异常处理-原则
- iOS中 imageNamed方法 非常多图片占用大量内存问题
- C++ 常用容器or数据结构
- 【原创】MVC +WebUploader 实现分片上传大文件
- Unity编辑器扩展-菜单项
- HDFS源码分析之编辑日志编辑相关双缓冲区EditsDoubleBuffer
- 输出顺序串中最长的重复子串
- StarUML使用说明-指导手册
- GET,POST——简述
- 数据库的几种连接查询
- 微信退款和支付宝退款接口调用(java版)