POJ1696凸包变形

9 篇文章 0 订阅
2 篇文章 0 订阅

题目

Space Ant

平面上有若干个点,一只蚂蚁走路不能向右转,问最多能经过多少个点。

蚂蚁的起点为 ( 0 , m i n ( y i ) ) (0,min(y_i)) (0,min(yi))

求解

凸包变形,路径一定是下凸壳、上凸壳、下凸壳、上凸壳…组成的,循环找凸壳即可。

代码

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int N = 2e5 + 5;

typedef struct Point
{
    int id;
    double x, y;
    Point operator-(const Point &temp) const
    {
        Point T;
        T.x = x - temp.x;
        T.y = y - temp.y;
        return T;
        // return Point{1, x - temp.x, y - temp.y};
    }
    bool operator<(const Point &temp) const
    {
        if (x == temp.x)
            return y > temp.y;
        return x < temp.x;
    }
} Vector;

Point p[N];

int stk[N];
bool vis[N];

double cross(Vector A, Vector B)
{
    return A.x * B.y - A.y * B.x;
}

void solve()
{
    int n;
    scanf("%d", &n);
    p[0].x = 0;
    p[0].y = 0x3f3f3f3f;
    for (int i = 1; i <= n; i++)
    {
        cin >> p[i].id;
        scanf("%lf %lf", &p[i].x, &p[i].y);
        p[0].y = min(p[0].y, p[i].y);
    }
    sort(p + 1, p + 1 + n);
    int top = 0, op = 1;
    memset(vis, 0, sizeof vis);
    bool flag = 1;
    stk[++top] = 0; //先压入起点
    while (flag)
    {
        flag = 0;
        if (op)
            for (int i = 1; i <= n; i++)
            {
                if (vis[i])
                    continue;
                while (top >= 2 && cross(p[stk[top]] - p[stk[top - 1]], p[i] - p[stk[top - 1]]) <= 0)
                    vis[stk[top--]] = 0;
                flag = 1;
                stk[++top] = i;
                vis[i] = 1;
            }
        else
            for (int i = n; i >= 1; i--)
            {
                if (vis[i])
                    continue;
                while (top >= 2 && cross(p[stk[top]] - p[stk[top - 1]], p[i] - p[stk[top - 1]]) <= 0)
                    vis[stk[top--]] = 0;
                flag = 1;
                stk[++top] = i;
                vis[i] = 1;
            }
        op ^= 1;
    }
    cout << top - 1 << ' ';
    for (int i = 2; i <= top; i++)
        printf("%d ", p[stk[i]].id);
    puts("");
}
int main()
{
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hesorchen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值