码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3348(凸包面积)

时间:2016-05-01 21:42:46      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8063   Accepted: 3651

Description

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

题意:在一个树林里面养牛,每头牛需要50平方米的空间,问你总共可以养多少牛?
题解:凸包面积/50
#include <iostream>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
const int N = 10005;
const double eps = 1e-8;
struct Point {
    int x,y;
}p[N],Stack[N];
int n;
int cross(Point a,Point b,Point c){
    return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
double getarea(Point a,Point b,Point c){
    return cross(a,b,c)/2.0;
}
double dis(Point a,Point b){
    return  sqrt((double)((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)));
}
int cmp(Point a,Point b){
    if(cross(a,b,p[0])>0) return 1;
    if(cross(a,b,p[0])==0&&dis(b,p[0])-dis(a,p[0])>eps) return 1;
    return 0;
}
int Graham(){
    int k=0;
    for(int i=1;i<n;i++){
        if(p[i].y<p[k].y||(p[i].y==p[k].y)&&(p[i].x<p[k].x)) k=i;
    }
    swap(p[0],p[k]);
    sort(p+1,p+n,cmp);
    int top =2;
    Stack[0] = p[0];
    Stack[1] = p[1];
    Stack[2] = p[2];
    for(int i=3;i<n;i++){
        while(top>=1&&cross(p[i],Stack[top],Stack[top-1])>=0) top--;
        Stack[++top] = p[i];
    }
    double area = 0;
    for(int i=1;i<top;i++){
        area+=getarea(Stack[i],Stack[i+1],Stack[0]); ///这里打成p..WA了几次..
    }
    return floor(area/50);
}
int main()
{
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++){
            scanf("%d%d",&p[i].x,&p[i].y);
        }
        if(n==1||n==2) {
            printf("0\n");
            continue;
        }
        int ans =  Graham();
        printf("%d\n",ans);
    }
    return 0;
}

 

poj 3348(凸包面积)

标签:

原文地址:http://www.cnblogs.com/liyinggang/p/5451115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!