PAT L3-021 神坛

https://pintia.cn/problem-sets/994805046380707840/problems/994805046577840128

在古老的迈瑞城,巍然屹立着 n 块神石。长老们商议,选取 3 块神石围成一个神坛。因为神坛的能量强度与它的面积成反比,因此神坛的面积越小越好。特殊地,如果有两块神石坐标相同,或者三块神石共线,神坛的面积为 0.000

长老们发现这个问题没有那么简单,于是委托你编程解决这个难题。

输入格式:

输入在第一行给出一个正整数 n(3 ≤ n ≤ 5000)。随后 n 行,每行有两个整数,分别表示神石的横坐标、纵坐标(− 横坐标、纵坐标 <)。

输出格式:

在一行中输出神坛的最小面积,四舍五入保留 3 位小数。

输入样例:

8
3 4
2 4
1 1
4 1
0 3
3 0
1 3
4 2

输出样例:

0.500

代码:

#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int maxn = 5100;
int N;
ll ans = 1e18;

struct Node{
    ll x;
    ll y;
}node[maxn], l[maxn];

bool cmp(const Node &a, const Node &b) {
    return b.y * a.x > b.x * a.y;
}

int main() {
    scanf("%d", &N);
    for(int i = 0; i < N; i ++)
        scanf("%lld%lld", &node[i].x, &node[i].y);

    int cnt = 0;
    for(int i = 0; i < N; i ++) {
        cnt = 0;
        for(int j = 0; j < N; j ++) {
            if(i != j) {
                l[cnt].x = node[j].x - node[i].x;
                l[cnt].y = node[j].y - node[i].y;
                cnt ++;
            }
        }
        sort(l, l + cnt, cmp);

        for(int j = 1; j < cnt; j ++)
            ans = min(ans, abs(l[j].y * l[j - 1].x - l[j].x * l[j - 1].y));
    }

    printf("%.3f
", ans / 2.0);
    return 0;
}

  极角排序 暴力超时 

原文地址:https://www.cnblogs.com/zlrrrr/p/10511085.html