poj3348 Cows

传送门

凸包板子

写凸包的要点:

极角排序就直接叉积 逆正顺负

算面积的时候三角形要/2.0

还有这题用整数就行了qwq

Code:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cmath>
 4 using namespace std;
 5 typedef double D;
 6 #define read(x) scanf("%lf",&x)
 7 #define inf 2147483647
 8 #define eps 1e-8
 9 const int N = 100005;
10 #define rep(i,a,n) for(int i = a;i <= n;++i)
11 struct point {
12     D x,y;
13     point(){}
14     point(int X,int Y):x(X),y(Y){}
15     D len() {return sqrt(x*x+y*y);}
16     point operator - (point o) {
17         return (point){x-o.x,y-o.y};
18     }
19     D operator * (point o) {
20         return x * o.y - y * o.x;
21     }
22 }p[N],stk[N];
23 int top,n;
24 
25 inline bool cmp(point a,point b) {
26     D Cmp = (a - p[1]) * (b - p[1]);
27     if(Cmp != 0.0) return Cmp > 0.0;
28     return (a - p[1]).len() < (b - p[1]).len();
29 }
30 
31 void Graham() {
32     int id = 1;
33     rep(i,2,n) if(p[i].x < p[id].x || (p[i].x == p[id].x && p[i].y < p[id].y)) id = i;
34     swap(p[1],p[id]),sort(p+2,p+n+1,cmp);
35     top = 0;
36     stk[++top] = p[1];
37     rep(i,2,n) {
38         while(top >= 2 && (p[i] - stk[top-1]) * (stk[top] - stk[top-1]) >= 0.0) top--;
39         stk[++top] = p[i];
40     }
41 }
42 
43 D S() {
44     D res = 0.0;
45     stk[top+1] = stk[1];
46     rep(i,1,top) res += stk[i] * stk[i+1];
47     return res / 2.0;
48 }
49 
50 int main() {
51     scanf("%d",&n);
52     rep(i,1,n) scanf("%lf%lf",&p[i].x,&p[i].y);
53     Graham();
54     int ans = S() / 50;
55     printf("%d\n",ans);
56     return 0;
57 }

 

posted @ 2018-11-22 16:38  白怀潇  阅读(147)  评论(0编辑  收藏  举报