hdu-5784 How Many Triangles(计算几何+极角排序)

题目链接:

How Many Triangles

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 570    Accepted Submission(s): 183


Problem Description
Alice has n points in two-dimensional plane. She wants to know how many different acute triangles they can form. Two triangles are considered different if they differ in at least one point.
 
Input
The input contains multiple test cases.
For each test case, begin with an integer n,
next n lines each contains two integers xi and yi.
3n2000
0xi,yi1e9
Any two points will not coincide.
 
Output
For each test case output a line contains an integer.
 
Sample Input
 
3
1 1
2 2
2 3
3
1 1
2 3
3 2
4
1 1
3 1
4 1
2 3
 
Sample Output
 
0
1
2
 
题意:
 
问有多少个锐角三角形,跟以前的求钝角和直角三角形一样,要用极角排序,不过这道题的点有共线的,所以要结合一下三角形的知识,其实就是锐角三角形三个锐角,直角和钝角三角形有两个锐角,求出锐角的个数,钝角和直角的个数,然后锐角个数减去两倍的直角钝角个数,再除3就是答案了,极角排序的问题就是精度问题了;精度ep开的小一点啦就能过啦;
 
思路:
 
哎哟,上边一不小心就说了;
 
AC代码;
 
/************************************************
┆  ┏┓   ┏┓ ┆   
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆ 
┆┃   ┻   ┃ ┆
┆┗━┓    ┏━┛ ┆
┆  ┃    ┃  ┆      
┆  ┃    ┗━━━┓ ┆
┆  ┃  AC代马   ┣┓┆
┆  ┃           ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆      
************************************************ */ 
 
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
 
using namespace std;
 
#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
 
typedef  long long LL;
 
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}
 
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=5e4+10;
const int maxn=2e3+14;
const double eps=1e-12;

double temp[2*maxn];
int n;
struct node
{
    double x,y;
}po[maxn];

int main()
{      
        while(scanf("%d",&n)!=EOF)
        {
            For(i,1,n)
            {
                scanf("%lf%lf",&po[i].x,&po[i].y);
            }
            LL ans1=0,ans2=0;
            For(i,1,n)
            {
                int cnt=0;
                For(j,1,n)
                {
                    if(i==j)continue;
                    temp[++cnt]=atan2(po[j].y-po[i].y,po[j].x-po[i].x);
                    if(temp[cnt]<0)temp[cnt]+=2*PI;
                }
                sort(temp+1,temp+cnt+1);
                For(j,1,cnt)
                {
                    temp[j+cnt]=temp[j]+2*PI;
                }
                int l=1,r=1,le=1;
                For(j,1,cnt)
                {
                    while(temp[r]-temp[j]<PI&&r<=2*cnt)r++;
                    while(temp[l]-temp[j]<0.5*PI&&l<=2*cnt)l++;
                    while(temp[le]-temp[j]<=eps&&le<=2*cnt)le++;
                    ans1=ans1+r-l;
                    ans2=ans2+l-le;
                }
            }
            cout<<(ans2-2*ans1)/3<<"
";
        }
        return 0;
}

  

原文地址:https://www.cnblogs.com/zhangchengc919/p/5733811.html