• POJ 3348 Cows(凸包+多边形面积)


    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.

    题目大意:给n个点,求凸包,然后求这个凸包的面积。

    思路:跟题目大意一样……

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <iostream>
      4 #include <algorithm>
      5 #include <cmath>
      6 using namespace std;
      7 
      8 const int MAXN = 10010;
      9 const double EPS = 1e-8;
     10 const double PI = acos(-1.0);//3.14159265358979323846
     11 
     12 inline int sgn(double x) {
     13     return (x > EPS) - (x < -EPS);
     14 }
     15 
     16 struct Point {
     17     double x, y;
     18     Point() {}
     19     Point(double x, double y): x(x), y(y) {}
     20     void read() {
     21         scanf("%lf%lf", &x, &y);
     22     }
     23     bool operator == (const Point &rhs) const {
     24         return sgn(x - rhs.x) == 0 && sgn(y - rhs.y) == 0;
     25     }
     26     bool operator < (const Point &rhs) const {
     27         if(y != rhs.y) return y < rhs.y;
     28         return x < rhs.x;
     29     }
     30     Point operator + (const Point &rhs) const {
     31         return Point(x + rhs.x, y + rhs.y);
     32     }
     33     Point operator - (const Point &rhs) const {
     34         return Point(x - rhs.x, y - rhs.y);
     35     }
     36     Point operator * (const int &b) const {
     37         return Point(x * b, y * b);
     38     }
     39     Point operator / (const int &b) const {
     40         return Point(x / b, y / b);
     41     }
     42     double length() const {
     43         return sqrt(x * x + y * y);
     44     }
     45     Point unit() const {
     46         return *this / length();
     47     }
     48 };
     49 typedef Point Vector;
     50 
     51 double dist(const Point &a, const Point &b) {
     52     return (a - b).length();
     53 }
     54 
     55 double cross(const Point &a, const Point &b) {
     56     return a.x * b.y - a.y * b.x;
     57 }
     58 //ret >= 0 means turn left
     59 double cross(const Point &sp, const Point &ed, const Point &op) {
     60     return sgn(cross(sp - op, ed - op));
     61 }
     62 
     63 double area(const Point& a, const Point &b, const Point &c) {
     64     return fabs(cross(a - c, b - c)) / 2;
     65 }
     66 
     67 struct Seg {
     68     Point st, ed;
     69     Seg() {}
     70     Seg(Point st, Point ed): st(st), ed(ed) {}
     71     void read() {
     72         st.read(); ed.read();
     73     }
     74 };
     75 typedef Seg Line;
     76 
     77 bool isOnSeg(const Seg &s, const Point &p) {
     78     return (p == s.st || p == s.ed) ||
     79         (((p.x - s.st.x) * (p.x - s.ed.x) < 0 ||
     80           (p.y - s.st.y) * (p.y - s.ed.y) < 0) &&
     81          sgn(cross(s.ed, p, s.st) == 0));
     82 }
     83 
     84 bool isIntersected(const Point &s1, const Point &e1, const Point &s2, const Point &e2) {
     85     return (max(s1.x, e1.x) >= min(s2.x, e2.x)) &&
     86         (max(s2.x, e2.x) >= min(s1.x, e1.x)) &&
     87         (max(s1.y, e1.y) >= min(s2.y, e2.y)) &&
     88         (max(s2.y, e2.y) >= min(s1.y, e1.y)) &&
     89         (cross(s2, e1, s1) * cross(e1, e2, s1) >= 0) &&
     90         (cross(s1, e2, s2) * cross(e2, e1, s2) >= 0);
     91 }
     92 
     93 bool isIntersected(const Seg &a, const Seg &b) {
     94     return isIntersected(a.st, a.ed, b.st, b.ed);
     95 }
     96 
     97 bool isParallel(const Seg &a, const Seg &b) {
     98     return sgn(cross(a.ed - a.st, b.ed - b.st)) == 0;
     99 }
    100 
    101 //return Ax + By + C =0 's A, B, C
    102 void Coefficient(const Line &L, double &A, double &B, double &C) {
    103     A = L.ed.y - L.st.y;
    104     B = L.st.x - L.ed.x;
    105     C = L.ed.x * L.st.y - L.st.x * L.ed.y;
    106 }
    107 
    108 Point intersection(const Line &a, const Line &b) {
    109     double A1, B1, C1;
    110     double A2, B2, C2;
    111     Coefficient(a, A1, B1, C1);
    112     Coefficient(b, A2, B2, C2);
    113     Point I;
    114     I.x = - (B2 * C1 - B1 * C2) / (A1 * B2 - A2 * B1);
    115     I.y =   (A2 * C1 - A1 * C2) / (A1 * B2 - A2 * B1);
    116     return I;
    117 }
    118 
    119 bool isEqual(const Line &a, const Line &b) {
    120     double A1, B1, C1;
    121     double A2, B2, C2;
    122     Coefficient(a, A1, B1, C1);
    123     Coefficient(b, A2, B2, C2);
    124     return sgn(A1 * B2 - A2 * B1) == 0 && sgn(A1 * C2 - A2 * C1) == 0 && sgn(B1 * C2 - B2 * C1) == 0;
    125 }
    126 
    127 struct Poly {
    128     int n;
    129     Point p[MAXN];//p[n] = p[0]
    130     void init(Point *pp, int nn) {
    131         n = nn;
    132         for(int i = 0; i < n; ++i) p[i] = pp[i];
    133         p[n] = p[0];
    134     }
    135     double area() {
    136         if(n < 3) return 0;
    137         double s = p[0].y * (p[n - 1].x - p[1].x);
    138         for(int i = 1; i < n; ++i)
    139             s += p[i].y * (p[i - 1].x - p[i + 1].x);
    140         return s / 2;
    141     }
    142 };
    143 
    144 void Graham_scan(Point *p, int n, int *stk, int &top) {//stk[0] = stk[top]
    145     sort(p, p + n);
    146     top = 1;
    147     stk[0] = 0; stk[1] = 1;
    148     for(int i = 2; i < n; ++i) {
    149         while(top && cross(p[i], p[stk[top]], p[stk[top - 1]]) >= 0) --top;
    150         stk[++top] = i;
    151     }
    152     int len = top;
    153     stk[++top] = n - 2;
    154     for(int i = n - 3; i >= 0; --i) {
    155         while(top != len && cross(p[i], p[stk[top]], p[stk[top - 1]]) >= 0) --top;
    156         stk[++top] = i;
    157     }
    158 }
    159 
    160 /*******************************************************************************************/
    161 
    162 Point p[MAXN];
    163 Poly poly;
    164 int stk[MAXN], top;
    165 int n, T;
    166 
    167 int solve() {
    168     poly.n = top;
    169     for(int i = 0; i <= top; ++i) poly.p[i] = p[stk[i]];
    170     double ret = poly.area() + EPS;
    171     return int(ret / 50);
    172 }
    173 
    174 int main() {
    175     scanf("%d", &n);
    176     for(int i = 0; i < n; ++i) p[i].read();
    177     Graham_scan(p, n, stk, top);
    178     printf("%d
    ", solve());
    179 }
    View Code
  • 相关阅读:
    Leetcode题解(4):L216/Combination Sum III
    零基础学python-3.1 python基本规则和语句
    3D打印技术之切片引擎(4)
    自己定义struts2中action类型转换器
    Android开发之实现锁屏功能
    用户上传本地磁盘中的一个图片文件作为头像
    微博达人硅谷之歌:Testin云測移动搜索性能測试非常是让人信服
    读《疯狂Java讲义》笔记总结三
    这才是爱情最好的样子
    数据挖掘 与 Web开发何去何从
  • 原文地址:https://www.cnblogs.com/oyking/p/3416079.html
Copyright © 2020-2023  润新知