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, ...
随机推荐
- 【转】【漫画解读】HDFS存储原理
根据Maneesh Varshney的漫画改编,以简洁易懂的漫画形式讲解HDFS存储机制与运行原理. 一.角色出演 如上图所示,HDFS存储相关角色与功能如下: Client:客户端,系统使用者,调用 ...
- 【转】android 电池(三):android电池系统
关键词:android电池系统电池系统架构 uevent power_supply驱动 平台信息: 内核:linux2.6/linux3.0系统:android/android4.0 平台:S5PV3 ...
- android的init过程分析
前言 Android系统是运作在linux kernal上的,因此它的启动过程也遵循linux的启动过程,当linux内核启动之后,运行的第一个进程是init,这个进程是一个守护进程,它的生命周期贯穿 ...
- HDU 4508 湫湫系列故事——减肥记I(全然背包)
HDU 4508 湫湫系列故事--减肥记I(全然背包) http://acm.hdu.edu.cn/showproblem.php?pid=4508 题意: 有n种食物, 每种食物吃了能获得val[i ...
- Bestcoder HDU5059 Help him 字符串处理
Help him Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- 【类似N^N做法的斐波那契数列】【HDU1568】 Fibonacci
Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- kvm虚拟化之克隆篇
注意:在克隆虚拟机的时候,该虚拟机必须处于关闭状态. 1,查看目前有哪些子机并选择要克隆的子机,我选择关闭test,说明我要克隆的就是它了. 2,查看虚拟机是否关闭. virsh list --al ...
- OSError: [Errno 13] Permission denied: '/etc/cron.d/1sandbox_registration'
使用Hortonworks 的twitter tutorial: http://hortonworks.com/hadoop-tutorial/how-to-refine-and-visualize- ...
- 【转载】Xcode6中添加pch文件
//原文地址:http://www.cnblogs.com/YouXianMing/p/3989155.html 1. 新建工程: 2. 创建pch文件:cmd+n->other->PCH ...
- vim自动补全文章搜集
引用文章A:http://blog.csdn.net/wendy260310/article/details/18035555 文章介绍:添加C++标准库的tags文件方法.(中文版) 引用文章B:h ...