凸包详解地址:
https://www.cnblogs.com/wpbing/p/9456240.html
A - Surround the Trees
There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him?
The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line.
There are no more than 100 trees.
InputThe input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair is separated by blank.
Zero at line for number of trees terminates the input for your program.
OutputThe minimal length of the rope. The precision should be 10^-2.
Sample Input9
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0
Sample Output243.06
典型的凸包问题。
代码及备注解释如下:
#include <stdio.h> #include <string.h> #include <iostream> #include <string> #include <math.h> #include <stdlib.h> #include <algorithm> using namespace std; int top,s[10024];//s用来当作边界点的栈 struct data { double x,y; } p[10024],temp; double corss(data a,data b,data c)//计算叉积向量ab和向量ac { return ((b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y)); } double dis(data a,data b)//计算距离 { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } //除第一个点外按极角大小排列,小的在前面,不会用sort来写…… int cmp(const void *a,const void *b) { double m; data *p1,*p2; p1=(data *)a; p2=(data *)b; m=corss(p[0],*p1,*p2); if(m<0) return 1; else if(m==0&&(dis(p[0],*p1)<dis(p[0],*p2))) return 1; else return -1; } int main() { int n,i,j,u; double sum; while(~scanf("%d",&n)&&n) { sum=0; for(i=0; i<n; i++) scanf("%lf%lf",&p[i].x,&p[i].y); if(n==1) { printf("0.00\n"); continue; } else if(n==2) { printf("%.2f\n",2*dis(p[0],p[1])); //hdu 1392 不需要乘两倍 zoj1453需要这样写 continue; } u=0; //找出最左边的点,即y坐标最小,y相同时x最小 for(i=0; i<n; i++) { if(p[i].y<p[u].y||((p[i].y==p[u].y)&&p[i].x<p[u].x)) u=i; } temp=p[u]; p[u]=p[0]; p[0]=temp; qsort(&p[1],n-1,sizeof(double)*2,cmp); for(i=0; i<=2; i++) s[i]=i;//将前面三点入栈 top=2; for(i=3; i<n; i++) { while(corss(p[s[top-1]],p[s[top]],p[i])<=0) top--;//右旋顺时针,将top的点出栈 s[++top]=i; } for(i=1; i<=top; i++) sum+=dis(p[s[i]],p[s[i-1]]); sum+=dis(p[s[0]],p[s[top]]); printf("%.2f\n",sum); } return 0; }
Cows
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 101Sample Output
151题目大意:
草地上有些树,用树做篱笆围一块最大的面积来养牛,每头牛要50平方米才能养活,问最多能养多少只牛。解析:
凸包求面积,分解成三角形用茶几求面积。
代码如下:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct node { int x,y; } e[10010],result[10010]; int dist(node a,node b) { return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } int cp(node p1,node p2,node p3) { return (p3.x-p1.x)*(p2.y-p1.y)-(p3.y-p1.y)*(p2.x-p1.x); } bool cmp(node a,node b) { int ans=cp(e[0],a,b); if(ans==0) return dist(e[0],a)-dist(e[0],b)<=0; else return ans>0; } int main() { int n,i,j; while(scanf("%d",&n)!=EOF) { int pos=0; for(i=0; i<n; ++i) { scanf("%d%d",&e[i].x,&e[i].y); if(e[pos].y>=e[i].y) { if(e[pos].y==e[i].y) { if(e[pos].x>e[i].x) pos=i; } else pos=i; } } if(n<3) { printf("0\n"); continue; } node temp; int top=1; temp=e[0]; e[0]=e[pos]; e[pos]=temp; sort(e+1,e+n,cmp); result[0]=e[0]; result[1]=e[1]; for(i=2; i<n; ++i) { while(cp(result[top-1],result[top],e[i])<0) top--; result[++top]=e[i]; } double s=0; for(i=0; i<=top; ++i) { double area=(result[(i+1)%(top+1)].x*result[i].y-result[(i+1)%(top+1)].y*result[i].x)/2.0; s+=area; } printf("%d\n",(int)(s/50.0)); } return 0; }z