题目链接

题意

给定\(n(n\leq 700)\)个点,问共线的点最多有多少个?

思路

\(O(n^3)\):枚举两个顶点确定一条直线,再看有多少个顶点在这条直线上。讲道理会T.

\(O(n^2logn)\):枚举一个顶点,算其他所有点与它连线的斜率,排个序,斜率相同的(排序后相邻的)就是共线的。

Code

#include <bits/stdc++.h>
#define maxn 710
using namespace std;
typedef long long LL;
int x[maxn], y[maxn];
LL k[maxn];
int gcd(int a, int b) {
return b ? gcd(b, a%b) : a;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d%d", &x[i], &y[i]);
int ans = 0;
for (int i = 0; i < n; ++i) {
int tot = 0;
for (int j = i+1; j < n; ++j) {
int dx = x[j] - x[i], dy = y[j] - y[i];
if (dx < 0) dx = -dx, dy = -dy;
int div = gcd(dx, dy);
dx /= div, dy /= div;
k[tot++] = 1LL * dx + dy * 10000;
}
sort(k, k+tot);
int cnt = 0;
for (int j = 1; j < tot; ++j) {
if (k[j] == k[j-1]) ++cnt;
else ans = max(ans, cnt), cnt = 0;
}
ans = max(ans, cnt);
}
printf("%d\n", ans+2);
return 0;
}

luogu 1142 轰炸 最多共线点数的更多相关文章

  1. Leetcode 149.直线上最多的点数

    直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | |        o ...

  2. Java实现 LeetCode 149 直线上最多的点数

    149. 直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | ...

  3. Lint Code——最多共线的点的个数

    题目链接:http://www.lintcode.com/zh-cn/problem/max-points-on-a-line/# 条件:给一个点数组 目标:求出共线的点的最多个数 实现:时间复杂度- ...

  4. Max Points on a Line(直线上最多的点数)

    给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | |        o |     o | ...

  5. ZOJ 3597 Hit the Target! (线段树扫描线 -- 矩形所能覆盖的最多的点数)

    ZOJ 3597 题意是说有n把枪,有m个靶子,每把枪只有一发子弹(也就是说一把枪最多只能打一个靶子), 告诉你第 i 把枪可以打到第j个靶, 现在等概率的出现一个连续的P把枪,在知道这P把枪之后,你 ...

  6. [Swift]LeetCode149. 直线上最多的点数 | Max Points on a Line

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  7. leetcode 149. 直线上最多的点数 解题报告

    给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | o +------- ...

  8. 149 Max Points on a Line 直线上最多的点数

    给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...

  9. POJ 1118 Lining Up 直线穿过最多的点数

    http://poj.org/problem?id=1118 直接枚举O(n^3) 1500ms能过...数据太水了...这个代码就不贴了... 斜率排序O(n^2logn)是更好的做法...枚举斜率 ...

随机推荐

  1. d3.js--03(增删改查)

    选择元素 d3.select():是选择所有指定元素的第一个 d3.selectAll():是选择指定元素的全部 插入元素 append():在选择集末尾插入元素 insert():在选择集前面插入元 ...

  2. 【转】pDc->SelectObject(pOldBrush)恢复画刷

    请看下面的代码:  CDC *pDc=new CClientDC(this); CBrush brush; brush.CreateSolidBrush(RGB(0,255,0)); CBrush * ...

  3. cppoop作业:Inheritance+Composition 關係下的構造和析構

    Inheritance+Composition 關係下的構造和析構 哪个的ctor先被调用. 父类先于组件类调用 构造函数

  4. Flask-基本原理与核心知识

    虚拟环境 使用pipenv创建一个虚拟环境和项目绑定,安装:E:\py\qiyue\flask>python3 -m pip install pipenv 和项目绑定:到项目的目录中pipenv ...

  5. java中常用的集合的一些区别 (2013-10-07-163写的日志迁移

    java中的以下几大集合: List结构的集合类: ArrayListl类,LinkedList类,Vector类,stack类 Map结构的集合类: HashMap类,Hashtable类(此是以k ...

  6. Python基础——字典(dict)

    由键-值对构建的集合. 创建 dic1={} type(dic1) dic2=dict() type(dic2) 初始化 dic2={'hello':123,'world':456,'python': ...

  7. Anaconda安装和环境的搭建

    Anaconda安装 在官网上下载最新的Anaconda https://www.anaconda.com/distribution/ 我使用的是2018.12,Python 3.7这个版本的. 安装 ...

  8. x mod a=r(N对a,r)

    //模数不一定互质,互质才可以用孙子定理. /* https://blog.csdn.net/zmh964685331/article/details/50527894 uu遇到了一个小问题,可是他不 ...

  9. Post页面爬取失败__编码问题

    python3爬取Post页面时, 报以下错误 "POST data should be bytes or an iterable of bytes. It cannot be of typ ...

  10. jmeter中基于oracle的JDBC Request的使用

    前提条件: 1.有数据库:2.数据库中有表,例如testuser(userid,username,usepwd): 设置如下: 参考自:http://www.linuxidc.com/Linux/20 ...