题目链接

题意

给定\(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. NSString 使用 copy、strong

    // 首先定义2个属性 @property (nonatomic, strong) NSString *stStr; @property (nonatomic, copy) NSString *coS ...

  2. Django2.0里urls.py里配置的改变

    从Django2.0开始,urls.py配置方法有很大改变. 1.把url函数换成path 2.不在使用^.$作为路由 3.其他地方以后再进一步研究 下面看一个列子: from django.cont ...

  3. Re:从零开始的Linux之路(目录配置)

    基于 Red Hat Enterprise Linux 7.5 或者 CentOS 7.4 FHS协议(Filesystem Hierarchy Standard)——文件系统层次化标准 该标准定义了 ...

  4. paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 standard verilog FSM conding styles(三段式)

    Three always block style with registered outputs(Good style)

  5. 多线程之volatile关键字(五)

    开始全文之前,先铺垫一下jvm基础知识以及线程栈: JVM栈是线程私有的,每个线程创建的同时都会创建JVM栈,JVM栈中存放的为当前线程中局部基本类型的变量(java中定义的八种基本类型:boolea ...

  6. pandas处理较大数据量级的方法 - chunk,hdf,pkl

    前情提要: 工作原因需要处理一批约30G左右的CSV数据,数据量级不需要hadoop的使用,同时由于办公的本本内存较低的缘故,需要解读取数据时内存不足的原因. 操作流程: 方法与方式:首先是读取数据, ...

  7. 转:跟我一起写Makefile (PDF重制版)

    原文地址:http://seisman.info/how-to-write-makefile.html 其它一些问题  不妨看一下:http://blog.csdn.net/huyansoft/art ...

  8. LeetCode(162) Find Peak Element

    题目 A peak element is an element that is greater than its neighbors. Given an input array where num[i ...

  9. Docker容器技术的核心原理

    目录 1 前言 2 docker容器技术 2.1 隔离:Namespace 2.2 限制:Cgroup 2.3 rootfs 2.4 镜像分层 3 docker容器与虚拟机的对比 1 前言 上图是百度 ...

  10. JQuery根据字母检索元素并导航到指定位置

    介绍:类似于实现ios通讯录中右侧字母,快速导航到联系人的效果,如图: Html代码如下,分别是字母和港星名字的排序: <input type="text" id=" ...