求助万能的谷民
  • 板块学术版
  • 楼主FiraCode
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/10/6 07:52
  • 上次更新2023/10/27 08:35:00
查看原帖
求助万能的谷民
528430
FiraCode楼主2022/10/6 07:52

用凸包做的,只得了20分。

CODE:

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
struct lzf{
    double x, y;
}a[10020];
int n, t, mi, q[10020];
double ans;
double cj(int i, int j, int k){
    return (a[i].x - a[k].x) * (a[j].y - a[k].y) - (a[j].x - a[k].x) * (a[i].y - a[k].y);
}
bool cmp(lzf l, lzf y){
    int z = (l.x - a[1].x) * (y.y - a[1].y) - (y.x - a[1].x) * (l.y - a[1].y);
    if (z > 0) return true;
    if (z < 0) return false;
    if (l.x != y.x) return l.x<y.x;
    return l.y < y.y;
}
int main(){
    scanf("%d", &n);
    a[0] = (lzf){0, 0};
    a[mi = n + 2] = (lzf){1001, 1001};
    for (int i = 1; i <= n; ++i){
        scanf("%lf%lf",&a[i].x, &a[i].y);
        if (a[mi].x > a[i].x || a[mi].x == a[i].x && a[mi].y > a[i].y)
            mi = i;
    }
    swap(a[mi], a[1]);
    sort(a + 2, a + n + 1, cmp);
    q[t = 1] = 1;
    for (int i = 2; i <= n; ++i){
        while (t > 1 && cj(i, q[t], q[t - 1]) >= 0) t--;
        q[++t] = i;
    }
    q[t + 1] = q[1];
    for (int i = 1; i <= t; ++i)
        ans += cj(q[i], q[i + 1], 0);
    printf("%.1lf", abs(ans) / 2 * (n - 2));
    return 0;
}
2022/10/6 07:52
加载中...