求周长的话,就是两点之间距离相加,注意吧p[0]和p[n-1]单独算就行,放在for外边。而面积的话就是一个凸包,从一个点出发,到其他点的连线把凸包分成好多个三角形这样就可以求面积了,s=1/2|a||b|sin。二叉乘的数值就是|a||b|sin,所以除以二就是面积。
HDU-1392
Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11344 Accepted Submission(s): 4399
Problem Description
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.
Input
The 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.
Output
The minimal length of the rope. The precision should be 10^-2.
Sample Input
9
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0
Sample Output
243.06
Source
Asia 1997, Shanghai (Mainland China)
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,top;
struct node
{
int x,y;
}a[50000],p[50000];
double dis(node a,node b)
{
return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
}
double cross(node p0,node p1,node p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
bool cmp(node p1,node p2)
{
double z=cross(a[0],p1,p2);
if(z>0||z==0&&(dis(a[0],p1)<dis(a[0],p2)))
return 1;
return 0;
}
void Graham()
{
int k=0;
for(int i=0;i<n;i++)
{
if(a[i].y<a[k].y||(a[i].y==a[k].y&&a[i].x<a[k].x))
k=i;
}
swap(a[0],a[k]);
sort(a+1,a+n,cmp);
top=1;
p[0]=a[0],p[1]=a[1];
for(int i=2;i<n;i++)
{
while(top&&cross(p[top-1],p[top],a[i])<0)
top--;
top++;
p[top]=a[i];
}
}
int main()
{
while(~scanf("%d",&n))
{
if(n==0)
break;
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
if(n==1)
printf("0.00\n");
else if(n==2)
printf("%.2lf\n",dis(a[0],a[1]));
else
{
Graham();
double sum=0;
for(int i=0; i<top; i++)
{
sum+=dis(p[i],p[i+1]);
}
sum+=dis(p[top],p[0]);
printf("%.2lf\n",sum);
}
}
}
POJ-3348
Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9665 Accepted: 4238
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
CCC 2007
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,top;
struct node
{
int x,y;
}a[10005],p[10005];
double dis(node p1,node p2)
{
return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
double cross(node p0,node p1,node p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
bool cmp(node p1,node p2)
{
double z=cross(a[0],p1,p2);
if(z>0||z==0&&(dis(a[0],p1)<dis(a[0],p2)))
return 1;
return 0;
}
void Graham()
{
int k=0;
for(int i=0;i<n;i++)
{
if(a[i].y<a[k].y||a[i].y==a[k].y&&a[i].x<a[k].x)
k=i;
}
swap(a[k],a[0]);
sort(a+1,a+n,cmp);
p[0]=a[0];
p[1]=a[1];
top=1;
for(int i=2;i<n;i++)
{
while(top&&cross(p[top-1],p[top],a[i])<0)
top--;
top++;
p[top]=a[i];
}
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
Graham();
double sum=0;
for(int i=1;i<top;i++)
{
sum+=cross(p[0],p[i],p[i+1]);
}
int ans=(int)sum/100.0;
printf("%d\n",ans);
}
}