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 ...
随机推荐
- ElasticSearch6 影响_score的两种方式
影响score两种方式都要用到 function_score 1. field_value_factor "function_score" => [ "query& ...
- django使用mongodb建表
1.安装mongodb的py模块包 pip install mongoengine 同时安装了mongoengine.pymongo 2.在项目配置文件settings.py中配置 from mong ...
- 【BZOJ 1449】 1449: [JSOI2009]球队收益 (最小费用流)
1449: [JSOI2009]球队收益 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 841 Solved: 483 Description Inpu ...
- 2018BNU校赛总决赛
题解是qls的题解我就懒得写了23333 A塞特斯玛斯塔 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld ...
- MySQL -- 性能优化的最佳20+条经验
FROM:http://www.cnblogs.com/shlhm/p/3235848.html ,学习学习~ 今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库 ...
- php 导出excel
<?phpclass Excel { var $inEncode; var $outEncode; public function _construct() { } public functio ...
- bzoj 2565: 最长双回文串 manacher算法
2565: 最长双回文串 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem. ...
- PAT甲级1012. The Best Rank
PAT甲级1012. The Best Rank 题意: 为了评估我们第一年的CS专业学生的表现,我们只考虑他们的三个课程的成绩:C - C编程语言,M - 数学(微积分或线性代数)和E - 英语.同 ...
- mvc-单例多线程模式
以spring mvc 为例子 spring mvc 的Controller类默认Scope是单例(singleton) 测试结果发现spring3中的controller默认是单例的,若是某个con ...
- OpenVPN配置网桥模式的一些理解
说明: 1.网桥的作用是将所有的网卡都能直连主机所在的路由器,可以把它当做一个独立的PC. 2.OpenVPN设置成网桥之后,IP设置成主机所在的IP网段,这样客户端连接进来之后就是所在主机所在的网络 ...