HihoCoder ~ 1687 ~ 寻找切线 (数学)

本文介绍了一种通过寻找最左侧点并结合斜率比较来确定极值点的算法实现。该算法首先找到所有点中最左侧的点,接着计算其余各点与最左侧点之间的斜率,选取斜率绝对值最大的点作为另一个极值点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:我们找到最左边(或上,下,右)的点,然后找一条斜率最大或者最小的点就是答案。

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
int n;
struct Node
{
    double x, y, id;
}a[MAXN];
bool cmp1(Node A, Node B)
{
    return A.x < B.x;
}
bool cmp2(Node A, Node B)
{
    return abs(A.y - a[0].y) / abs(A.x - a[0].x) > abs(B.y - a[0].y) / abs(B.x - a[0].x);
}
int main()
{
    ios::sync_with_stdio(false);
    while (~scanf("%d", &n))
    {
        for (int i = 0; i < n; i++)
        {
            scanf("%lf%lf", &a[i].x, &a[i].y);
            a[i].id = i + 1;
        }
        sort(a, a + n, cmp1);//找到最左边的点
        sort(a + 1, a + n, cmp2);//找到和最左边点斜率最大的那个点
        cout << a[0].id << " " << a[1].id << endl;
    }
    return 0;
}
/*
6
0 10
7 0
8 8
10 18
15 13
20 4
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值