POJ 1696 (凸包变形)

题目: 传送门

题意: 给你 n 个点, 然后, 有一只蚂蚁, 问你蚂蚁只能直走和左转,问蚂蚁要怎么走才能走最多的点。

题解: 其实就是求很多个凸包, 蚂蚁肯定能走完所有点。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
using namespace std;

const int N = 5e4 + 5;

struct Point {
    int id;
    double x, y;
    Point(double x = 0, double y = 0) : x(x), y(y) { } /// 构造函数
};

typedef Point Vector;
/// 向量+向量=向量, 点+向量=向量
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
///点-点=向量
Vector operator - (Point A, Point B) { return Vector(A.x - B.x, A.y - B.y); }
///向量*数=向量
Vector operator * (Vector A, double p) { return Vector(A.x * p, A.y * p); }
///向量/数=向量
Vector operator / (Vector A, double p) { return Vector(A.x / p, A.y / p); }

const double eps = 1e-10;
int dcmp(double x) {
    if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
}

bool operator < (const Point& a, const Point& b) {
    return a.x == b.x ? a.y < b.y : a.x < b.x;
}

bool operator == (const Point& a, const Point &b) {
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}

double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } /// 点积
double Length(Vector A) { return sqrt(Dot(A, A)); } /// 计算向量长度
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } /// 向量A、B夹角
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } /// 叉积


Point P[N], ans[N];
bool vis[N];
void ConvexHull(int st, int n) {
    mem(vis, 0);
    int k = 0, t = 1;
    while(k < n) {
        rep(i, st, n - 1) {
            if(vis[P[i].id]) continue;
            while(k > t && Cross(ans[k - 1] - ans[k - 2], P[i] - ans[k - 1]) <= 0) k--, vis[ans[k].id] = 0;
            ans[k++] = P[i]; vis[P[i].id] = 1;
        }
        t = k;
        dep(i, 0, n - 2) {
            if(vis[P[i].id]) continue;
            while(k > t && Cross(ans[k - 1] - ans[k - 2], P[i] - ans[k - 1]) <= 0) k--, vis[ans[k].id] = 0;
            ans[k++] = P[i]; vis[P[i].id] = 1;
        }
        t = k; st = 0;
    }
}

void solve() {
    int n;
    scanf("%d", &n);
    double mi = 150.0, pos = -1;
    rep(i, 0, n - 1) {
        scanf("%d %lf %lf", &P[i].id, &P[i].x, &P[i].y);
    }
    sort(P, P + n);
    rep(i, 0, n - 1) if(P[i].y < mi) mi = P[i].y, pos = i;
    ConvexHull(pos, n);
    printf("%d ", n);
    rep(i, 0, n - 1) printf("%d ", ans[i].id); puts("");
}

int main() {
    int _; scanf("%d", &_);
    while(_--) solve();
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Willems/p/12346573.html