HDU-1392,POJ-3348(凸包周长,面积)

本文介绍了一种计算由点构成的最大围栏周长和面积的方法,通过使用Graham扫描算法找出凸包,进而计算出由这些点构成的最大的封闭区域的周长和面积。

求周长的话,就是两点之间距离相加,注意吧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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值