Hdoj 1432 Lining Up 【叉积】

Lining Up

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1094    Accepted Submission(s): 307

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

题意:给出一些坐标,问有多少各点在同一条直线上

策略:共线向量叉积为0; 叉积公式 例如 a (x1, y1), b(x2, y2)  a*b = x1*y2-x2*y1

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
const int M = 750;
using namespace std;

struct node{
    int x, y;
}s[M];

bool f(node a, node b, node c){
    return ((a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x) == 0); //
}

int main(){
    int n;
    while(scanf("%d", &n) == 1){
        for(int i = 0; i < n; ++ i){
            scanf("%d%d", &s[i].x, &s[i].y);
        }
        int ans = 0, cou = 0;
        for(int i = 0; i < n; ++ i){
            for(int j = i+1; j < n; ++ j){
                cou = 0;
                for(int k = j+1; k < n; ++ k){
                    if(f(s[i], s[j], s[k])) ++cou;
                }
                ans = max(ans, cou);
            }
        }
        printf("%d\n", ans+2);
    }
    return 0;
}
时间: 2024-12-29 15:38:36

Hdoj 1432 Lining Up 【叉积】的相关文章

HDU 1432 Lining Up(几何)

http://acm.hdu.edu.cn/showproblem.php?pid=1432 题目大意: 2维平面上给定n个点,求一条直线能够穿过点数最多是多少. 解题思路: 因为题目给定的n(1~700),所以枚举,时间复杂度是O(n^3),不会超时. 枚举两个点,然后判断剩下的点是否在这条直线. AC代码: 1 #include<cstdio> 2 3 struct Point{ 4 int x, y; 5 6 Point(int x = 0, int y = 0): x(x), y(y)

hdoj 2036 (计算几何,叉积求面积)

改革春风吹满地 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24179    Accepted Submission(s): 12504 Problem Description “ 改革春风吹满地,不会AC没关系;实在不行回老家,还有一亩三分地.谢谢!(乐队奏乐)” 话说部分学生心态极好,每天就知道游戏,这次考试如此简单的题目,也是云

POJ 1018 &amp; HDU 1432 Lining Up 【简单几何】

Lining Up Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24786   Accepted: 7767 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 spe

HDU 1432 Lining Up (POJ 1118)

枚举,枚举点 复杂度为n^3. 还可以枚举边的,n*n*log(n). POJ 1118 要判断0退出. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostream> #include<list&

hdoj 1086 You can Solve a Geometry Problem too 【计算几何】

题意:就是判断各线段之间有没有交点. 判断两线段相交,要运用到叉积.两个线段相交肯定相互跨越,假设一个条线段(p1p2),另一条是(q1q2),那么p1p2肯定在q1q2线段的两侧,那么运用叉积如果p1p2跨越q1q2的话(q1p1)x(q2p2)<= 0.同样也要验证 q1q2是不是也跨越p1p2,注意:p1p2跨越q1q2,不代两个线段相交,可能是p1p2跨越直线q1q2,所以说还是要再次判断q1q2是不是跨越p1p2 还有另外一种比较容易理解的解法: 就是如果两个线段相交,那么两线段两端端

【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. 1 /* 4328 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector>

【POJ 1408】 Fishnet (叉积求面积)

[POJ 1408] Fishnet (叉积求面积) 一个1*1㎡的池塘 有2*n条线代表渔网 问这些网中围出来的最大面积 一个有效面积是相邻两行和相邻两列中间夹的四边形 Input为n 后面跟着四行 每行n个浮点数 每一行分别代表a,b,c,d 如图 并且保证a(i) > a(i-1) b(i) > b(i-1) c(i) > c(i-1) d(i) > d(i-1) n(n <= 30)*2+4(四个岸)条边 枚举点数就行 相邻的四个四个点枚举 找出围出的最大面积 找点用

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1