Hdu 5784 How Many Triangles 计算几何,极角排序

题目;http://acm.hdu.edu.cn/showproblem.php?pid=5784
求锐角三角形个数,直接暴力n3肯定超时;
要将所有的锐角个数和直角钝角个数统计出来;
设有x个锐角,y个直角加钝角,总共就有(x+y)/3个三角形,就有(x+y)/3-y个锐角三角形,化简一下就是(x-2*y)/3;
对每一个点,求一次以这个点为顶点的角,将这个点和所有其他点的向量求出来,排序后对每一个向量求以这个向量为一边的各种角的个数,用两个标记来标记第一个非共线的位置,小于直角的位置和小于平角的位置,因为遍历到第四象限的时候晾一边可能在第一象限,所以向量开两倍,保证不会越界;
atan2可以求出一个向量的角度,在第三四象限会变成负的所以加一下

#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debug
using namespace std;
const double eps=1e-11;
const double PI=acos(-1.0);
const int maxn=4000+50;
int n;
struct Point
{
    double x,y;
    Point() {}
    Point(double x,double y):x(x),y(y) {}
    void read()
    {
        scanf("%lf%lf",&x,&y);
    }
};
 
typedef Point Vector;
 
Vector operator - (Vector A,Vector B)
{
    return Vector(A.x-B.x,A.y-B.y);
}
 
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    else return x<0?-1:1;
}
 
int cnt;
Point p[maxn];
double a[maxn];
 
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        int acute=0,notacute=0;
        for(int i=1; i<=n; i++) p[i].read();
        for(int i=1; i<=n; i++)//以i为原点
        {
            cnt=0;
            for(int j=1; j<=n; j++)
            {
                if(i==j)continue;
                Vector tmp=p[j]-p[i];
                double ang=atan2(tmp.y,tmp.x);
                if(ang<0) ang+=2*PI;
                a[++cnt]=ang;
            }
 
            sort(a+1,a+cnt+1);
            for(int j=1;j<=cnt;j++) a[cnt+j]=a[j]+2*PI;
            int l=1,r=1,equ=1;
            for(int j=1; j<=cnt; j++)
            {
                while(l<=2*cnt&&dcmp(a[l]-a[j]-PI/2)<0) l++;//锐角
                while(r<=2*cnt&&dcmp(a[r]-a[j]-PI)<0) r++;//钝角
                while(equ<=2*cnt&&dcmp(a[equ]-a[j])==0) equ++;//非三角形
 
                acute+=l-equ;//锐角个数
                notacute+=r-l;//钝角
            }
        }
        printf("%d\n",(acute-2*notacute)/3);
    }
    return 0;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值