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. Mod_Python中文文档

    Mod_Python中文文档 mod_python中文文档

  2. Tomcat部署项目的几种常见方式

    以前学习的时候只知道在Eclipse或者MyEclipse中发布项目到Tomcat,最近实习时发现不同的项目还有不同的发布方式,所以特地学习了Tomcat发布项目的方式,在此记录下来. 1 直接将we ...

  3. 【点击模型学习笔记】Predicting Clicks_Estimating the Click-Through Rate for New Ads_MS_www2007

    概要: 微软研究院的人写的文章,提出用逻辑回归来解决ctr预估问题,是以后ctr的经典解决方式,经典文章. 详细内容: 名词: CPC -- cost per click CTR -- click t ...

  4. 使用有限状态机(FSM)编写的敌人AI

    using UnityEngine; using System.Collections; public class AttackState : FSMState { public AttackStat ...

  5. Android平台抓取native crash log

    Android开发中,在Java层可以方便的捕获crashlog,但对于 Native 层的 crashlog 通常无法直接获取,只能通过系统的logcat来分析crash日志. 做过 Linux 和 ...

  6. C#的简单的Windows Service 创建与安装

    注意事项: 1. 添加调试代码 入口: 服务: 2. 再服务界面右键添加安装程序 3. 修改安装程序属性(Account) 4. 修改服务安装属性(DelayedAutoStart,ServiceNa ...

  7. XML.ObjTree -- XML source code from/to JavaScript object like E4X

    转载于:http://www.kawa.net/works/js/xml/objtree-try-e.html // ========================================= ...

  8. C#常用的内置委托

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  9. 小项目--反eclass

    前言—— 最近会把前一段时间闲的无聊写的一些很小的项目写一些博客,用来练练手. 引子—— 最近班里有个很讨厌的软件,,,,教育局规定每个学校要上传多媒体使用记录,所以学校就给班里每台电脑上装了一个比较 ...

  10. nginx+webpy 出现 upstream timed out

    关于nginx配置webpy应用出现的错误 upstream timed out (: Connection timed out) while reading response header from ...