【POJ SOJ HDOJ】POJ 2187 Beauty Contest

题目描述:

Beauty Contest

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 39669 Accepted: 12305
Description

Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates.

Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms.

Input

* Line 1: A single integer, N

* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
Output

* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other.

题目价值:3(1-5), 凸包入门

复制代码
#include<iostream>
#include<vector>
#include<stdio.h>
#include<time.h>
#include<algorithm>
using namespace std;
struct P {
    int x, y;
    P() {}
    P(int _x, int _y) {
        x = _x, y = _y;
    }
    int dot(const P& ot) {
        return this->x * ot.x + this->y * ot.y;
    }
    P operator-(const P& ot)const {
        return P(this->x - ot.x, this->y - ot.y);
    }
    P operator+(const P& ot)const {
        return P(this->x + ot.x, this->y + ot.y);
    }
    int det(const P& ot)const {
        return this->x * ot.y - this->y * ot.x;
    }
};
int N;
vector<P> ps;

////////////////////////////////////
bool cmp(const P& a, const P& b) {
    if (a.x == b.x) return a.y < b.y;
    return a.x < b.x;
}

vector<P> convex_pack() {
    vector<P> qs(2 * N);
    int k = 0;
    int t;
    sort(ps.begin(), ps.end(), cmp);
    for (int i = 0; i < N; i++) {
        // 下凸包, note that the operation is det(外积)
        while (k > 1 && (ps[i] - qs[k - 1]).det(qs[k - 1] - qs[k - 2]) <= 0) k--;
        qs[k++] = ps[i];
    }
    for (int i = N - 2, t = k; i >= 0; i--) {
        while (k > t && (ps[i] - qs[k - 1]).det(qs[k - 1] - qs[k - 2]) <= 0) k--;
        qs[k++] = ps[i];
    }
    qs.resize(k - 1);
    return qs;
}
int dist(const P& a, const P& b) {
    P c = a - b;
    return c.dot(c);
}
int max_dist() {
    vector<P> qs = convex_pack();
    int res = 0;
    for (int i = 0; i < qs.size(); i++) {
        for (int j = 0; j < i; j++) {
            res = max(res, (int)dist(qs[i], qs[j]));
        }
    }
    return (int)res;
}
int main() {
    while (cin >> N) {
        ps.clear();
        ps.resize(N);
        for (int i = 0; i < N; i++) {
            cin >> ps[i].x >> ps[i].y;
        }
        printf("%d\n", max_dist());
    }
    return 0;
}
复制代码

 

裸的凸包, 用格雷厄姆算法就解决了

结果:

 

posted @   stackupdown  阅读(122)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示