Lining Up 

``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 begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The input consists of N pairs of integers, where 1 < N < 700. Each pair of integers is separated by one blank and ended by a new-line character. The list of pairs is ended with an end-of-file character. No pair will occur twice.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line. 
The output consists of one integer representing the largest number of points that all lie on one line.

Sample Input

1

1 1
2 2
3 3
9 10
10 11

Sample Output

3

题意:给定一些点坐标。求共线点最多的个数。。

思路:我的做法是暴力枚举。每次先枚举出2个点。作为直线。再去枚举第三个点看在不在直线上。 可以推出一个公式

y(x1 - x2) = (y1 - y2) * x + y2 * x1 - y1 * x2的时候。为(x,y)在(x1,y1)和(x2,y2)组成的直线上。。不过这样做的话时间复杂度为O(n^3)..中间有个优化。就是如果两个点之前判断连接过了。之后在遇到就直接跳过。。但是依然跑了快2秒。- -

看网上别人做法有一种时间复杂度为O(n^2logn)的做法。。是每次枚举一个点作为原点。然后把其他点和它的斜率算出来。然后找出这些斜率中相同斜率出现次数最多的作为最大值。感觉不错。

我的代码:

#include <stdio.h>
#include <string.h> int t;
int n, i, j, k, l;
int max, ans;
int vis[705][705];
int mark[705];
char sb[30]; struct Point {
int x, y;
} p[705]; int main() {
scanf("%d%*c%*c", &t);
while (t --) {
n = 0; max = 0;
memset(vis, 0, sizeof(vis));
while (gets(sb) && sb[0] != '\0') {
sscanf(sb, "%d%d", &p[n].x, &p[n].y);
n ++;
}
for (i = 0; i < n; i ++)
for (j = i + 1; j < n; j ++) {
if (vis[i][j]) continue;
ans = 0;
for (k = 0; k < n; k ++) {
if (p[k].y * (p[i].x - p[j].x) == (p[i].y - p[j].y) * p[k].x + p[j].y * p[i].x - p[i].y * p[j].x) {
mark[ans ++] = k;
for (l = 0; l < ans - 1; l ++)
vis[k][mark[l]] = vis[mark[l]][k] = 1;
}
}
if (max < ans)
max = ans;
}
printf("%d\n", max);
if (t) printf("\n");
}
return 0;
}

UVA 270 Lining Up (几何 判断共线点)的更多相关文章

  1. UVA 270 Lining Up 共线点 暴力

    题意:给出几个点的位置,问一条直线最多能连过几个点. 只要枚举每两个点组成的直线,然后找直线上的点数,更新最大值即可. 我这样做过于暴力,2.7s让人心惊肉跳...应该还能继续剪枝的,同一直线找过之后 ...

  2. poj 2031Building a Space Station(几何判断+Kruskal最小生成树)

    /* 最小生成树 + 几何判断 Kruskal 球心之间的距离 - 两个球的半径 < 0 则说明是覆盖的!此时的距离按照0计算 */ #include<iostream> #incl ...

  3. UVa 270 & POJ 1118 - Lining Up

    题目大意:给一些点,找出一条直线使尽可能多的点在这条直线上,求这条直线上点的个数. 以每一个点为原点进行枚举,求其它点的斜率,斜率相同则说明在一条直线上.对斜率排序,找出斜率连续相等的最大长度. #i ...

  4. Uva 12124 Uva Live 3971 - Assemble 二分, 判断器, g++不用map.size() 难度:0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  5. ArcEngine中合并断开的线要素(根据几何判断)

    在上一篇ArcEngine环境下合并断开的线要素(根据属性)随笔中介绍了如何通过shp文件属性表中相同的属性字段进行线要素的合并.今天刚把通过几何条件判断的方式连接断开的线要素的ArcGIS 插件完成 ...

  6. 简单几何(判断矩形的位置) UVALive 7070 The E-pang Palace(14广州B)

    题目传送门 题意:给了一些点,问组成两个不相交的矩形的面积和最大 分析:暴力枚举,先找出可以组成矩形的两点并保存起来(vis数组很好),然后写个函数判断四个点是否在另一个矩形内部.当时没有保存矩形,用 ...

  7. uva 558 - Wormholes(Bellman Ford判断负环)

    题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...

  8. UVa 10256 - The Great Divide 判断凸包相交

    模板敲错了于是WA了好几遍…… 判断由红点和蓝点分别组成的两个凸包是否相离,是输出Yes,否输出No. 训练指南上的分析: 1.任取红凸包上的一条线段和蓝凸包上的一条线段,判断二者是否相交.如果相交( ...

  9. UVA 11796 Dog Distance(几何)

    Dog Distance [题目链接]Dog Distance [题目类型]几何 &题解: 蓝书的题,刘汝佳的代码,学习一下 &代码: // UVa11796 Dog Distance ...

随机推荐

  1. 趴一趴京东的Ajax动态价格页面

    AJAX,异步加载技术!!! 之前在网上看过很多朋友有一种疑问,为什么在看京东网页的源代码里面看不到价格或则折扣一类的数据,而在网页上正常显示却能看到?...之前我也没有想到是AJAX,因为我写写爬虫 ...

  2. 【BZOJ 3443】 3443: 装备合成 (离线+线段树)

    3443: 装备合成 Time Limit: 15 Sec  Memory Limit: 128 MBSubmit: 63  Solved: 31 Description [背景]     lll69 ...

  3. 矩阵乘法<简单总结>

    原理:矩阵相乘最重要的方法是一般矩阵乘积.它只有在第一个矩阵的 行数 和第二个矩阵的 列数 相同时才可进行.若A为m×n矩阵,B为n×p矩阵,则他们的乘积AB会是一个m×p矩阵. 若A=    a   ...

  4. ARC 101 C - Candles

    题面在这里! 显然直接枚举左端点(右端点)就OK啦. #include<cstdio> #include<cstdlib> #include<algorithm> ...

  5. PHP生成短网地址

    思路: 1)将长网址md5生成32位签名串,分为4段, 每段8个字节; 2)对这四段循环处理, 取8个字节, 将他看成16进制串与0x3fffffff(30位1)与操作, 即超过30位的忽略处理; 3 ...

  6. 权限验证AuthorizeAttribute

    /// <summary> /// 权限验证属性. /// </summary> public class AuthorizeExAttribute : AuthorizeAt ...

  7. BZOJ 2243: [SDOI2011]染色 树链剖分 倍增lca 线段树

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  8. ROS知识(1)----ROS Jade安装

    ROS入门难,进去之后会是很简单,这是很多人的经验.但是今天安装ROS就吃了闭门羹,安装成功后,回顾发现,关键是操作系统Ubantu14.04没有安装好,一些系统包没有及时更新导致的.这里总结下ROS ...

  9. iOS开发经验总结——基础工程

    iOS开发经验总结--依赖库 这篇博客,我想说一下开发中经常遇到的一个问题,虚拟个场景描述一下的话,应该是这样的. 项目经理:今天我们正式开始一个新项目,iOSer你负责把苹果端的APP完成,有没有问 ...

  10. 微信小程序的坑

    虽然官方文档,可以在.json中给页面设置背景颜色,用backgroundColor,但是实际上并不好使,所以设置背景颜色只能在wxss中设置 <import src="../comm ...