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

来源:互联网 发布:淘宝快递拒签运费谁负 编辑:程序博客网 时间:2024/04/30 14:06

圈水池

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