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, ...
随机推荐
- UESTC_秋实大哥与家 2015 UESTC Training for Data Structures<Problem E>
E - 秋实大哥与家 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- 搜索和搜索形式(SEARCHING and its forms)
什么是搜索? 在计算机科学中,搜索就是在一个事物的集合中找到具有特定特征的一项的过程.这些集合中的元素可能是排好序的数据库中的记录,简单数组中的数据,文件中的文本,树中的节点,几何图形中的点和边或者是 ...
- 剑指offer-面试题10:二进制中1的个数
题目:请实现一个函数,输入一个函数,输出该数二进制表示中1的个数.例如把9 表示成二进制是1001,有2位是1.因此如果输入9,该函数输出2. 这道题最典型的方法就是用移位统计,就比如统计9的二进制1 ...
- apache FtpServer 整合spring部署
我们在项目中可能会出现这样的需求,使用ftp上传很大的文件后对需要对文件进行相应的逻辑处理,这时我们可以使用apache ftpServer来处理这段逻辑,只要我们做相应的部署和编写我们的逻辑代码,这 ...
- 【bzoj1031】[JSOI2007]字符加密Cipher
题目描述 喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法:把需要加密的信息排成一圈,显然,它们有很多种不同的读法.例如下图,可以读作:JSOI07 ...
- LeetCode——Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- char与byte的差别
非常多刚開始学习的人(包含我,已经学了一年多java了)肯会对char和byte这两种数据类型有所疑惑,相互混淆,今天特地查了好多资料,对byte和char两种数据类型进行了总结和比較,先将结果与大家 ...
- 放弃使用jQuery实现动画
在Web开发的圈子里,开发人员经常觉得CSS动画是一种高性能web动画技术.假设想让网页载入的更快一些,就应该用纯CSS动画.事实上这样的观点是错误的,非常多开发人员早就放弃了javascript的动 ...
- 【分割平面,分割空间类题】【HDU1290 HDU2050】
HDU 2050 折线分割平面 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- PHP中的的一个挺好用的函数 array_chunk