hdoj 1432 Lining Up 【思维】

该博客介绍了HDOJ 1432道编程题,题目要求找出一组给定点中最多能有多少个点位于同一直线上。问题通过输入点的坐标来解决,并强调解决方案需要高效。博主给出了题目的输入格式、输出要求及样例,随后阐述了题意,并提出了通过计算斜率来统计共线点数的思路,最后提供了通过计算斜率实现的AC代码。
摘要由CSDN通过智能技术生成

题目链接:hdoj 1432 Lining Up

Lining Up

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

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

题意:有n个点,问你共线的点最多有多少个。

思路:我们处理出斜率,统计斜率的个数就可以了。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e5 + 10;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
int x[701], y[701];
double k[700*700];
int main() {
    int n; 
    while(scanf("%d", &n) != EOF) {
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &x[i], &y[i]);
        }
        int ans = 0;
        for(int i = 0; i < n; i++) {
            int top = 0;
            for(int j = i+1; j < n; j++) {
                k[top++] = (y[j] - y[i]) * 1.0 / (x[j] - x[i]);
                //cout << k[top-1] << endl;
            }
            sort(k, k+top); int cnt = 1; int ans1 = 2;
            for(int j = 1; j < top; j++) {
                if(k[j] == k[j-1]) cnt++;
                else {
                    ans1 = max(ans1, cnt+1);
                    cnt = 1;
                }
            }
            ans1 = max(ans1, cnt+1);
            ans = max(ans, ans1);
        }
        printf("%d\n", ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值