UVa 270 / POJ 1118 Lining Up (计算几何)

2024-03-05 20:32
文章标签 计算 poj 几何 uva 270 1118 lining

本文主要是介绍UVa 270 / POJ 1118 Lining Up (计算几何),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

270 - Lining Up

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=206

http://poj.org/problem?id=1118

``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

11 1
2 2
3 3
9 10
10 11

Sample Output

3

思路:

任取一点,计算出其他点到该点斜率后排序,斜率相同的点必然在一条直线上。 
复杂度:O(N^2*log N) 


UVa的代码:

/*0.129s*/#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF = 1 << 30;char str[20];
double x[705], y[705], d[705];int main(void)
{int t, n, i, j, k;int ans, count;scanf("%d\n", &t);///多读一个换行~while (t--){for (n = 0; gets(str); ++n){if (str[0] == '\0') break;sscanf(str, "%lf%lf", &x[n], &y[n]);}//ans = 1;for (i = 0; i < n - 1; ++i){for (j = i + 1, k = 0; j < n; ++j, ++k)d[k] = (x[j] == x[i] ? INF : (y[j] - y[i]) / (x[j] - x[i]));sort(d, d + k);count = 1;for (j = 1; j < k; ++j){if (fabs(d[j] - d[j - 1]) < 1e-9){++count;ans = max(ans, count);}else count = 1;}}printf("%d\n", ans + 1);if (t) putchar('\n');}return 0;
}


POJ的代码:

/*125ms,204KB*/#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF = 1 << 30;char str[20];
double x[705], y[705], d[705];int main(void)
{int n, i, j, k;int ans, count;while (scanf("%d", &n), n){for (i = 0; i < n; ++i)scanf("%lf%lf", &x[i], &y[i]);ans = 1;for (i = 0; i < n - 1; ++i){for (j = i + 1, k = 0; j < n; ++j, ++k)d[k] = (x[j] == x[i] ? INF : (y[j] - y[i]) / (x[j] - x[i]));sort(d, d + k);count = 1;for (j = 1; j < k; ++j){if (fabs(d[j] - d[j - 1]) < 1e-9){++count;ans = max(ans, count);}else count = 1;}}printf("%d\n", ans + 1);}return 0;
}

Source

East Central North America 1994

这篇关于UVa 270 / POJ 1118 Lining Up (计算几何)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/777727

相关文章

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Java计算经纬度距离的示例代码

《Java计算经纬度距离的示例代码》在Java中计算两个经纬度之间的距离,可以使用多种方法(代码示例均返回米为单位),文中整理了常用的5种方法,感兴趣的小伙伴可以了解一下... 目录1. Haversine公式(中等精度,推荐通用场景)2. 球面余弦定理(简单但精度较低)3. Vincenty公式(高精度,

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如

如何用Java结合经纬度位置计算目标点的日出日落时间详解

《如何用Java结合经纬度位置计算目标点的日出日落时间详解》这篇文章主详细讲解了如何基于目标点的经纬度计算日出日落时间,提供了在线API和Java库两种计算方法,并通过实际案例展示了其应用,需要的朋友... 目录前言一、应用示例1、天安门升旗时间2、湖南省日出日落信息二、Java日出日落计算1、在线API2

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

uva 10055 uva 10071 uva 10300(水题两三道)

情歌两三首,水题两三道。 好久没敲代码了为暑假大作战热热身。 uva 10055 Hashmat the Brave Warrior 求俩数相减。 两个debug的地方,一个是longlong,一个是输入顺序。 代码: #include<stdio.h>int main(){long long a, b;//debugwhile(scanf("%lld%lld", &

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M