崩溃了,板子有问题?
查看原帖
崩溃了,板子有问题?
833124
BIOS楼主2023/10/23 20:57
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 105;
#define x first
#define y second
typedef pair<double, double> PDD;
const double eps = 1e-8, pi = acos(-1);
struct Line
{
    PDD st, ed;
} line[N];
PDD p[N], v[N], ans[N];
int n, q[N];
int sign(double x)
{
    if (fabs(x) < eps)
        return 0;
    if (x < 0)
        return -1;
    return 1;
}

int dcmp(double x, double y)
{
    if (fabs(x - y) < eps)
        return 0;
    if (x < y)
        return -1;
    return 1;
}

double get_angle(const Line &a)
{
    return atan2(a.ed.y - a.st.y, a.ed.x - a.st.x);
}

PDD operator-(PDD a, PDD b)
{
    return {a.x - b.x, a.y - b.y};
}

double cross(PDD a, PDD b)
{
    return a.x * b.y - a.y * b.x;
}

double area(PDD a, PDD b, PDD c)
{
    return cross(b - a, c - a);
}

bool cmp(const Line &a, const Line &b)
{
    double A = get_angle(a), B = get_angle(b);
    if (!dcmp(A, B))
        return area(a.st, a.ed, b.ed) < 0;
    return A < B;
}

PDD gli(PDD p, PDD v, PDD q, PDD w)
{
    auto u = p - q;
    double t = cross(w, u) / cross(v, w);
    return {p.x + v.x * t, p.y + v.y * t};
}

PDD gli(Line a, Line b)
{
    return gli(a.st, a.ed - a.st, b.st, b.ed - b.st);
}

bool on_right(Line &a, Line &b, Line &c)
{
    auto o = gli(b, c);
    return sign(area(a.st, a.ed, o)) <= 0;
}
int half()
{
    sort(line, line + n, cmp);
    int hh = 0, tt = -1, k = 0;
    for (int i = 0; i < n; i++)
    {
        if (i && !dcmp(get_angle(line[i]), get_angle(line[i - 1])))
            continue;
        while (hh < tt && on_right(line[i], line[q[tt - 1]], line[q[tt]]))
            tt--;
        while (hh < tt && on_right(line[i], line[q[hh]], line[q[hh + 1]]))
            hh++;
        q[++tt] = i;
    }
    while (hh < tt && on_right(line[q[hh]], line[q[tt - 1]], line[q[tt]]))
        tt--;
    while (hh < tt && on_right(line[q[tt]], line[q[hh]], line[q[hh + 1]]))
        hh++;
    q[++tt] = q[hh];
    for (int i = hh; i < tt; i++)
        ans[k++] = gli(line[q[i]], line[q[i + 1]]);
    double res = 0;
    for (int i = 1; i + 1 < k; i++)
        res += area(ans[0], ans[i], ans[i + 1]);
    if (fabs(res) < eps)
        return false;
    return true;
}
bool check(int mid)
{
    for (int pos = 0; pos < n; pos++)
        line[pos] = {p[pos], p[(pos + mid + 1) % n]};
    return half();
}
int main()
{
    while (cin >> n)
    {
        for (int i = 0; i < n; i++)
            cin >> p[i].x >> p[i].y;
        int l = 0, r = n;
        while (l < r)
        {
            int mid = l + r + 1 >> 1;
            if (check(mid))
                l = mid;
            else
                r = mid - 1;
        }
        printf("%d\n", l);
    }
}

调了一小时了,一开始以为板子写错了,直到我把所有代码全粘贴过来,样例还是输出1 3

2023/10/23 20:57
加载中...