UVA 270 Lining Up (几何 判断共线点)
| 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 (几何 判断共线点)的更多相关文章
- UVA 270 Lining Up 共线点 暴力
题意:给出几个点的位置,问一条直线最多能连过几个点. 只要枚举每两个点组成的直线,然后找直线上的点数,更新最大值即可. 我这样做过于暴力,2.7s让人心惊肉跳...应该还能继续剪枝的,同一直线找过之后 ...
- poj 2031Building a Space Station(几何判断+Kruskal最小生成树)
/* 最小生成树 + 几何判断 Kruskal 球心之间的距离 - 两个球的半径 < 0 则说明是覆盖的!此时的距离按照0计算 */ #include<iostream> #incl ...
- UVa 270 & POJ 1118 - Lining Up
题目大意:给一些点,找出一条直线使尽可能多的点在这条直线上,求这条直线上点的个数. 以每一个点为原点进行枚举,求其它点的斜率,斜率相同则说明在一条直线上.对斜率排序,找出斜率连续相等的最大长度. #i ...
- 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 ...
- ArcEngine中合并断开的线要素(根据几何判断)
在上一篇ArcEngine环境下合并断开的线要素(根据属性)随笔中介绍了如何通过shp文件属性表中相同的属性字段进行线要素的合并.今天刚把通过几何条件判断的方式连接断开的线要素的ArcGIS 插件完成 ...
- 简单几何(判断矩形的位置) UVALive 7070 The E-pang Palace(14广州B)
题目传送门 题意:给了一些点,问组成两个不相交的矩形的面积和最大 分析:暴力枚举,先找出可以组成矩形的两点并保存起来(vis数组很好),然后写个函数判断四个点是否在另一个矩形内部.当时没有保存矩形,用 ...
- uva 558 - Wormholes(Bellman Ford判断负环)
题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...
- UVa 10256 - The Great Divide 判断凸包相交
模板敲错了于是WA了好几遍…… 判断由红点和蓝点分别组成的两个凸包是否相离,是输出Yes,否输出No. 训练指南上的分析: 1.任取红凸包上的一条线段和蓝凸包上的一条线段,判断二者是否相交.如果相交( ...
- UVA 11796 Dog Distance(几何)
Dog Distance [题目链接]Dog Distance [题目类型]几何 &题解: 蓝书的题,刘汝佳的代码,学习一下 &代码: // UVa11796 Dog Distance ...
随机推荐
- FastReport.Net使用:[29]调用存储过程1
1.创建存储过程sp_querycourse,用于查询学生成绩. 2.在FastReport.Net报表设计器中,通过 数据-->添加数据源 来打开数据向导. 选择数据源,添加数据连接. 3.在 ...
- 51nod1437 迈克步 单调栈
考虑一个点作为最小值的区间$[L[i], R[i]]$ 那么这个区间的所有含$i$的子区间最小值都是$v[i]$ 因此,用单调栈求出$L[i], R[i]$后,对$R[i] - L[i] + 1$这个 ...
- div块元素垂直水平居中方法总结
1.已知块级元素的宽和高,使用绝对定位+外边距设定水平垂直居中. 父元素position:relative,子元素position:absolute;top:50%;left:50%;margin-t ...
- 27.prim算法 最优布线问题(wire.cpp)
[例4-10].最优布线问题(wire.cpp) [问题描述] 学校有n台计算机,为了方便数据传输,现要将它们用数据线连接起来.两台计算机被连接是指它们间有数据线连接.由于计算机所处的位置不同,因此不 ...
- ThinkPHP -- 基础入门
ThinkPHP文件结构说明: |——ThinkPHP.php 框架入口文件 |——Common 框架公共文件目录 |——Conf ...
- 读书笔记_Effective_C++_条款三十:了解inline的里里外外
学过基本程序课的同学都知道,inline是内联的关键字,它可以建议编译器将函数的每一个调用都用函数本体替换.这是一种以空间换时间的做法.把每一次调用都用本体替换,无疑会使代码膨胀,但可以节省函数调用的 ...
- Ubuntu14.04环境下配置TFTP服务器
<<<<<<<<<<<<<<<<<<<<<<<<< ...
- 树莓派(Debian)系统设置了静态IP之后还会获取动态IP的问题解决(scope global secondary eth0)
解决方法: 1.配置好静态IP在/etc/network/interface 2.关闭dhcp服务(不知道这个服务是干嘛的,明明是客户端还需要这个) sudo systemctl stop dhcpc ...
- CSDN 四川大学线下编程比赛第一题:数字填充
题目意思: http://student.csdn.net/mcs/programming_challenges peter喜欢玩数字游戏,但数独这种游戏对他来说太简单了,于是他准备玩一个难的游戏. ...
- Windows Server 2008 Standard Enterprise Datacenter各个版本区别
-- Windows Server 2008 Standard 包含1个虚拟实例许可,5个客户端访问授权,售价999美元. -- Windows Server 2008 Enterprise 包含4个 ...