NYOJ 78 圈水池(凸包基础)@

圈水池

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 4
描述
有一个牧场,牧场上有很多个供水装置,现在牧场的主人想要用篱笆把这些供水装置圈起来,以防止不是自己的牲畜来喝水,各个水池都标有各自的坐标,现在要你写一个程序利用最短的篱笆将这些供水装置圈起来!(篱笆足够多,并且长度可变)
输入
第一行输入的是N,代表用N组测试数据(1<=N<=10)
第二行输入的是m,代表本组测试数据共有m个供水装置(3<=m<=100)
接下来m行代表的是各个供水装置的横纵坐标
输出
输出各个篱笆经过各个供水装置的坐标点,并且按照x轴坐标值从小到大输出,如果x轴坐标值相同,再安照y轴坐标值从小到大输出
样例输入
1
4
0 0
1 1
2 3
3 0
样例输出
0 0
2 3
3 0

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const LL mod = 1e9+7;
const int N = 110;
struct node
{
    int x, y;
    node operator -(const node& tmp)const
    {
        node e;
        e.x=x-tmp.x, e.y=y-tmp.y;
        return e;
    }
    int operator *(const node& tmp)const
    {
        return x*tmp.y-y*tmp.x;
    }
    bool operator <(const node& tmp)const
    {
        return x<tmp.x||(x==tmp.x&&y<tmp.y);
    }
}p[N], s[N];

int cmp(node a,node b)
{
    if(a.x!=b.x) return a.x<b.x;
    return a.y<=b.y;
}
int judge(node a,node b,node c)
{
    return (b-a)*(c-a)>0;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n;
        scanf("%d", &n);
        for(int i=0;i<n;i++)
        {
            scanf("%d %d", &p[i].x, &p[i].y);
        }
        sort(p,p+n,cmp);
        int top=0;
        for(int i=0;i<n;i++)
        {
            while(top>=2&& !judge(s[top-2],s[top-1],p[i]))
                top--;
            s[top++]=p[i];
        }
        int t=top-1;
        for(int i=n-1;i>=0;i--)
        {
            while(top>=t+2&& !judge(s[top-2],s[top-1],p[i]))
                top--;
            s[top++]=p[i];
        }
        --top;
        sort(s,s+top);
        for(int i=0;i<top;i++)
            printf("%d %d\n",s[i].x, s[i].y);
    }
    return 0;
}
















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值