zoukankan      html  css  js  c++  java
  • poj 3348--Cows(凸包求面积)

    链接:http://poj.org/problem?id=3348

    Cows
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 6677   Accepted: 3020

    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

    Source

     

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    凸包求面积,先用Graham求出所有凸包的点,然后用叉乘积借助三角形求面积

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 #include <iostream>
     5 #include <algorithm>
     6 #include <math.h>
     7 #define MAXX 10010
     8 
     9 using namespace std;
    10 
    11 typedef struct point
    12 {
    13     int x;
    14     int y;
    15 }point;
    16 
    17 double crossProduct(point a,point b,point c)
    18 {
    19     return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
    20 }
    21 
    22 double dist(point a,point b)
    23 {
    24     return sqrt((double)(a.x-b.x)*(a.x-b.x)+(double)(a.y-b.y)*(a.y-b.y));
    25 }
    26 
    27 point c[MAXX];
    28 int top;
    29 point stk[MAXX];
    30 
    31 bool cmp(point a,point b)
    32 {
    33     double len=crossProduct(c[0],a,b);
    34     if(len == 0)
    35     {
    36         return dist(c[0],a)<dist(c[0],b);
    37     }
    38     return len < 0;
    39 }
    40 
    41 void Graham(int n)
    42 {
    43     int tmp=0;
    44     for(int i=1;i<n;i++)
    45     {
    46         if((c[i].x<c[tmp].x)||((c[i].x == c[tmp].x)&&(c[i].y<c[tmp].y)))
    47               tmp=i;
    48     }
    49     swap(c[0],c[tmp]);
    50     sort(c+1,c+n,cmp);
    51     stk[0]=c[0];
    52     stk[1]=c[1];
    53     stk[2]=c[2];
    54     top=2;
    55     for(int i=3; i<n; i++)
    56     {
    57         while(1)
    58         {
    59             point a,b;
    60             a=stk[top];
    61             b=stk[top-1];
    62             if(crossProduct(a,b,c[i])<=0)
    63             {
    64                 top--;
    65             }
    66             else break;
    67         }
    68         stk[++top]=c[i];//printf("%d %d^^",stk[top].x,stk[top].y);
    69     }
    70 }
    71 
    72 double Area(int n)
    73 {
    74     if(n<3)return 0;
    75     int i;
    76     double ret=0.0;
    77     for(i=2; i<n; i++)
    78     {
    79         ret+=fabs(crossProduct(stk[0],stk[i-1],stk[i])/2.0);//printf("%lf---",ret);
    80     }
    81     return ret;
    82 }
    83 
    84 int main()
    85 {
    86     int i,j,n,m;
    87     scanf("%d",&n);
    88     for(i=0; i<n; i++)
    89     {
    90         scanf("%d%d",&c[i].x,&c[i].y);
    91     }
    92     Graham(n);//printf("%d**",top);
    93     double ans=Area(top+1);
    94     printf("%d
    ",(int)ans/50);
    95     return 0;
    96 }
    View Code
  • 相关阅读:
    包含停用词的词频统计(map,set非class版本)<< 0919
    pair,map,set<<0924
    答题程序中用户登录状态的保存<<0924
    时间戳<<0923
    list,vector相关函数与区别<<0922
    类里面的赋值和拷贝函数<<0922
    vector的初始化补充,list,find函数,指针和迭代器等<<0922
    redhat 6上nis配置
    如何查看cache信息
    oprofile 安装使用
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3902394.html
Copyright © 2011-2022 走看看