凸包基础题

/*
Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are forced to save money on buying fence posts by using trees as fence posts wherever possible. Given the locations of some trees, you are to help farmers try to create the largest pasture that is possible. Not all the trees will need to be used.

However, because you will oversee the construction of the pasture yourself, all the farmers want to know is how many cows they can put in the pasture. It is well known that a cow needs at least 50 square metres of pasture to survive.

Input
The first line of input contains a single integer, n (1 ≤ n ≤ 10000), containing the number of trees that grow on the available land. The next n lines contain the integer coordinates of each tree given as two integers x and y separated by one space (where -1000 ≤ x, y ≤ 1000). The integer coordinates correlate exactly to distance in metres (e.g., the distance between coordinate (10; 11) and (11; 11) is one metre).

Output
You are to output a single integer value, the number of cows that can survive on the largest field you can construct using the available trees.

Sample Input
4
0 0
0 101
75 0
75 101
Sample Output
151
*/ 

/* 
凸包 
基础入门题 

求出凸包  然后 循环 求面积即可; 
*/ 
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct trees{
    int x;
    int y;
}tree[1010],ch[1010];
bool cmp(trees a,trees b)
{
    if(a.x==b.x)    return a.y<b.y;
    return a.x<b.x;
}
int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}
int det(trees a,trees b,trees c,trees d)
{
    int x1,y1,x2,y2;
    x1 = a.x - b.x;
    y1 = a.y - b.y;
    x2 = c.x - d.x;
    y2 = c.y - d.y;
    return x1*y2-y1*x2;
}
double crea(trees a,trees b,trees c,trees d)
{
    int x1,y1,x2,y2;
    x1 = a.x - b.x;
    y1 = a.y - b.y;
    x2 = c.x - d.x;
    y2 = c.y - d.y;
    return abs(x1*y2-y1*x2)*1.0/2;
}
int tubao(trees *tree,int n,trees *ch)
{
    sort(tree,tree+n,cmp);
    int m = 0;
    for(int i=0;i<n;i++)
    {
        while(m>1&&det(ch[m-1],ch[m-2],tree[i],ch[m-2])<=0)
        m--;
        ch[m++] = tree[i];
    }
    int k = m;
    for(int i=n-2;i>=0;i--)
    {
        while(m>k&&det(ch[m-1],ch[m-2],tree[i],ch[m-2])<=0)
        m--;
        ch[m++] = tree[i];
    }
    if(n>1) m--;
    return m;
}
int pick(trees *ch,int m)
{
    int ans=0;
    for(int i=0;i<m;i++)
    {
        ans += gcd(abs(ch[i].x-ch[i+1].x),abs(ch[i].y-ch[i+1].y));
    }
    return ans;
}
int main()
{
    int w;
    scanf("%d",&w);
    for(int k = 0;k<w;k++)
    {
        int n,m;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&tree[i].x,&tree[i].y);
        }
        for(int j=0;j<n-2;j++)
        {
            tree[j+1].x = tree[j].x+tree[j+1].x;
            tree[j+1].y = tree[j].y+tree[j+1].y;
        }
        tree[n-1].x = 0;
        tree[n-1].y = 0;
        double s=0;
        m =tubao(tree,n,ch);
        for(int i=1;i<m-1;i++)
        {
            s += crea(ch[0],ch[i],ch[0],ch[i+1]);
        }
        int lon = pick(ch,m),pointsum;
        pointsum =(int)(s - ((double)lon)/2 +1);
        printf("%d %d %0.1lf\n",pointsum,lon,s);
    }
}

猜你喜欢

转载自blog.csdn.net/lijianzhong1/article/details/81226291