Lining Up

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1274    Accepted Submission(s): 366

Problem Description
``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 consists of multiple test cases, and each case begins with a single positive integer on a line by itself indicating the number of points, followed by N pairs of integers, where 1 < N < 700. Each pair of integers is separated by one blank and ended by a new-line character. No pair will occur twice in one test case. 
 
Output
For each test case, the output consists of one integer representing the largest number of points that all lie on one line, one line per case.
 
Sample Input
5
1 1
2 2
3 3
9 10
10 11
 
Sample Output
3
 

题解:错了好一会儿,发现是排序那里写错了,多此一举。。。都怪以前的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(在一条直线上的最大点数目,暴力)的更多相关文章

  1. lintcode 中等题:Max Points on a Line 最多有多少个点在一条直线上

    题目 最多有多少个点在一条直线上 给出二维平面上的n个点,求最多有多少点在同一条直线上. 样例 给出4个点:(1, 2), (3, 6), (0, 0), (1, 3). 一条直线上的点最多有3个. ...

  2. 一条直线上N个线段所覆盖的总长度

    原文:http://blog.csdn.net/bxyill/article/details/8962832 问题描述: 现有一直线,从原点到无穷大. 这条直线上有N个线段.线段可能相交. 问,N个线 ...

  3. 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 ...

  4. lintcode-186-最多有多少个点在一条直线上

    186-最多有多少个点在一条直线上 给出二维平面上的n个点,求最多有多少点在同一条直线上. 样例 给出4个点:(1, 2), (3, 6), (0, 0), (1, 3). 一条直线上的点最多有3个. ...

  5. 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. ...

  6. [LintCode] 最多有多少个点在一条直线上

    /** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(i ...

  7. objectarx之判断三点是否在一条直线上

    bool CCommonFuntion::IsOnLine(AcGePoint2d& pt1, AcGePoint2d& pt2, AcGePoint2d& pt3){ AcG ...

  8. 两条直线(蓝桥杯)二分枚举+RMQ

    算法提高 两条直线   时间限制:1.0s   内存限制:256.0MB        问题描述 给定平面上n个点. 求两条直线,这两条直线互相垂直,而且它们与x轴的夹角为45度,并且n个点中离这两条 ...

  9. 判断两条直线的位置关系 POJ 1269 Intersecting Lines

    两条直线可能有三种关系:1.共线     2.平行(不包括共线)    3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, ...

随机推荐

  1. JavaScript 输入验证器工具

    前注:在数据添加的时候很多地方都会涉及到数据的合法性验证,所以有必要提炼成为一个工具.今天偶然间点错网页,弹出一个游戏界面,本来是想看怎么实现的背景音乐的加载的,结果看到一个注册页面的验证JS,所以这 ...

  2. UESTC_邱老师选妹子 2015 UESTC Training for Dynamic Programming<Problem H>

    H - 邱老师选妹子 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  3. java连接oracle数据库详细代码

    详细代码: import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;imp ...

  4. 移除UIView上面的所有控件

    ;i<[view.subviews count];i++){ [ [ view.subviews objectAtindex:i] removeFromsuperview]; }

  5. github Permission denied (publickey)解决办法

    想要玩玩git,参考了网友懒惰之计的一篇Blog<github:如何获取项目源代码 >,按部就班完成了所有的步骤的, 可在测试的时候,遇到了问题,总是报错”github Permissio ...

  6. [jquery] 图片热区随图片大小自由缩放

    在图片上直接画出带超级链接热区元素map和area相信大家并不陌生,Dreamweaver等网页制作软件都有直接在图片上绘制带超级链接的热区工具,但是直接绘制的热区是不能随着图片自适应放大和缩小的,现 ...

  7. POJ 2446 Chessboard

    要求用占两格的长方形铺满平面上除去指定点 二分图匹配 #include <iostream> #include <cstdio> #include <cstring> ...

  8. Lua编程入门-学习笔记2

    第6章 深入函数 函数是一种“第一类值(First-Class Value)”,他们具有特定的词法域(lexical scoping) 将表达式“function(x) <body> en ...

  9. IE的documentMode属性

    参看下面链接:<IE的documentModeshuxing>

  10. .Net Service开发(一)

    一, 新增一个服务项目