Lining Up(在一条直线上的最大点数目,暴力)
Lining Up
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1274 Accepted Submission(s): 366
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!
1 1
2 2
3 3
9 10
10 11
题解:错了好一会儿,发现是排序那里写错了,多此一举。。。都怪以前的qsort,使我现在都快不敢直接判断了。。。
思路是先找出所有点,求出相同直线的个数sum,根据n*(n - 1)/2=sum,求出n;借助队友的思路;
ac代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
int tp;
struct Point{
double x, y;
Point(){ }
Point(double x, double y){
this->x = x;
this->y = y;
}
};
Point point[];
struct Node{
double k, b;
Node(double k,double b){
this->k = k;
this->b = b;
}
Node(){ }
bool operator < (const Node &a) const{
if(k != a.k){//直接比就可以。。。
return k < a.k;
}
else//
return b < a.b;
}
};
Node dt[];
Node operator + (Point a,Point b){
double k, t;
k = (a.y - b.y) / (a.x - b.x);
t = a.y - k * a.x;
return Node(k,t);
}
bool operator == (Node a, Node b){
if(abs(a.k - b.k) < 1e-){
if(abs(a.b - b.b) < 1e-){
return true;
}
}
return false;
}
int getn(int a, int b, int c){
double t = b * b - * a * c;
double x = ( -b + sqrt(t) ) / (2.0 * a);
return (int)x;
}
int main(){
int N;
while(~scanf("%d",&N)){
double x, y;
tp = ;
for(int i = ; i < N; i++){
scanf("%lf%lf",&x,&y);
point[i] = Point(x, y);
for(int j = ; j < i; j++){
dt[tp++] = point[i] + point[j];
}
}
if(N == ){
puts("");continue;
}
sort(dt, dt + tp);
int ans = , temp = ;
for(int i = ; i < tp; i++){
if(dt[i] == dt[i - ]){
temp++;
ans = max(ans,temp);
}
else temp = ;
}
ans++;
printf("%d\n", getn(, -, - * ans) );
}
return ;
}
java:
package com.lanqiao.week1; import java.util.Arrays;
import java.util.Scanner; public class poj1118 {
private static Scanner cin;
private static int MOD = 1000000007;
static{
cin = new Scanner(System.in);
}
static int getN(double a, double b, double c){
double ans = (-b + Math.sqrt(b * b - 4 * a * c)) / (2.0 * a);
return (int)ans;
}
static class Point{
int x, y;
public static Node getNode(Point a, Point b) {
int x = a.x - b.x;
int y = a.y - b.y;
double k = 1.0*y/x;
return new Node(k, a.y - a.x * k);
}
}
static class Node implements Comparable<Node>{
double k, t; public Node(double k, double t) {
super();
this.k = k;
this.t = t;
} public static boolean isEqual(Node a, Node b){
if(Math.abs(a.k - b.k) <= 1e-15 &&
Math.abs(a.t - b.t) <= 1e-15){
return true;
}else
return false;
}
@Override
public int compareTo(Node o) {
if(Math.abs(o.k - k) <= 1e-15){
if(o.t < t){
return 1;
}else{
return -1;
}
}else{
if(o.k < k){
return 1;
}else{
return -1;
}
}
} }
static Point[] points = new Point[710];
static Node[] nodes = new Node[250000];
public static void main(String[] args) {
int N;
N = cin.nextInt();
while(N > 0){ int k = 0;
for(int i = 0; i < N; i++){
points[i] = new Point();
points[i].x = cin.nextInt();
points[i].y = cin.nextInt();
for(int j = 0; j < i; j++){
nodes[k++] = Point.getNode(points[i], points[j]);
}
}
Arrays.sort(nodes, 0, k);
// for(int i = 0; i < k; i++){
// System.out.println((i + 1) + " : " + "k-->" + nodes[i].k + "t-->" + nodes[i].t);
// }
int ans = 1, cnt = 1;
for(int i = 1; i < k; i++){
if(Node.isEqual(nodes[i], nodes[i - 1])){
cnt ++;
ans = Math.max(ans, cnt);
}else{
cnt = 1;
}
}
System.out.println(getN(1, -1, -2*ans));
N = cin.nextInt();
}
}
}
Lining Up(在一条直线上的最大点数目,暴力)的更多相关文章
- lintcode 中等题:Max Points on a Line 最多有多少个点在一条直线上
题目 最多有多少个点在一条直线上 给出二维平面上的n个点,求最多有多少点在同一条直线上. 样例 给出4个点:(1, 2), (3, 6), (0, 0), (1, 3). 一条直线上的点最多有3个. ...
- 一条直线上N个线段所覆盖的总长度
原文:http://blog.csdn.net/bxyill/article/details/8962832 问题描述: 现有一直线,从原点到无穷大. 这条直线上有N个线段.线段可能相交. 问,N个线 ...
- LeetCode:149_Max Points on a line | 寻找一条直线上最多点的数量 | Hard
题目:Max Points on a line Given n points on a 2D plane, find the maximum number of points that lie on ...
- lintcode-186-最多有多少个点在一条直线上
186-最多有多少个点在一条直线上 给出二维平面上的n个点,求最多有多少点在同一条直线上. 样例 给出4个点:(1, 2), (3, 6), (0, 0), (1, 3). 一条直线上的点最多有3个. ...
- 149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- [LintCode] 最多有多少个点在一条直线上
/** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(i ...
- objectarx之判断三点是否在一条直线上
bool CCommonFuntion::IsOnLine(AcGePoint2d& pt1, AcGePoint2d& pt2, AcGePoint2d& pt3){ AcG ...
- 两条直线(蓝桥杯)二分枚举+RMQ
算法提高 两条直线 时间限制:1.0s 内存限制:256.0MB 问题描述 给定平面上n个点. 求两条直线,这两条直线互相垂直,而且它们与x轴的夹角为45度,并且n个点中离这两条 ...
- 判断两条直线的位置关系 POJ 1269 Intersecting Lines
两条直线可能有三种关系:1.共线 2.平行(不包括共线) 3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, ...
随机推荐
- 完全跨站点跨域名单点(SSO)同步登录和注销
先来说说什么是单点登录(SSO).来自百科的介绍:SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这次主 ...
- UESTC_Ferris Wheel String 2015 UESTC Training for Search Algorithm & String<Problem L>
L - Ferris Wheel String Time Limit: 3000/1000MS (Java/Others) Memory Limit: 43000/43000KB (Java/ ...
- html&CSS初学
<link href="https://fonts.gdgdocs.org/css?family=Lobster" rel="stylesheet" ty ...
- Scala--样例类(case)详解
概述: case类在模式匹配和actor中经常使用到,当一个类被定义成为case类后,Scala会自动帮你创建一个伴生对象并帮你实现了一系列方法且带来了不少好处,如下: 1.实现了apply方法,意味 ...
- AudioManager详解(结合源代码)
AudioManager:用来对音量大小,声音模式(静音,震动,震动加声音等模式)的管理, 还有用它来注册“插入耳机”时的广播接收者(Action: android.intent.action.MED ...
- Codeforces 474D Flowers dp(水
题目链接:点击打开链接 思路: 给定T k表示T组測试数据 每组case [l,r] 有2种物品a b.b物品必须k个连续出现 问摆成一排后物品长度在[l,r]之间的方法数 思路: dp[i] = d ...
- 工作记录8:iOS 传值问题总结(7种传值完美介绍)
1.属性传值 前向后传值. 记住: /* 1: 属性传值第一步需要用到什么类型就定义什么样的属性 2: 从上一个页面到一个页面的选中方法里面将要传的值传到来(上一个页面)备注:这种方法只适用于上一个页 ...
- [Python笔记][第二章Python序列-复杂的数据结构]
2016/1/27学习内容 第二章 Python序列-复杂的数据结构 堆 import heapq #添加元素进堆 heapq.heappush(heap,n) #小根堆堆顶 heapq.heappo ...
- [Python笔记][第一章Python基础]
2016/1/27学习内容 第一章 Python基础 Python内置函数 见Python内置函数.md del命令 显式删除操作,列表中也可以使用. 基本输入输出 input() 读入进来永远是字符 ...
- 【计算几何初步-代码好看了点线段相交】【HDU2150】Pipe
题目没什么 只是线段相交稍微写的好看了点 Pipe Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...