LA-4064(极角排序)

来源:互联网 发布:js方法自动执行 编辑:程序博客网 时间:2024/05/20 19:31

题意:

给定平面上n(3<=n<=1200)个无三点共线的点,问这些点组成了多少个锐角或直角三角形;

思路:

任意三点可组成一个三角形,C(n,3);减去钝角三角形的个数就是锐角或直角三角形的个数了;

计算钝角三角行的个数时枚举每个点,以这个点为原点,极角排序后计算角度在(PI/2,PI)的个数,最后一减就是答案了;

AC代码:

//#include <bits/stdc++.h>#include <vector>#include <iostream>#include <queue>#include <cmath>#include <map>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;#define Riep(n) for(int i=1;i<=n;i++)#define Riop(n) for(int i=0;i<n;i++)#define Rjep(n) for(int j=1;j<=n;j++)#define Rjop(n) for(int j=0;j<n;j++)#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('\n');}const LL mod=1e9+7;const double PI=acos(-1.0);const LL inf=1e18;const int N=1203;const int maxn=1005;const double eps=1e-10;double x[N],y[N],temp[2*N];LL n;LL cal(LL x,LL y){    LL ans=1;    for(int i=0;i<y;i++)        ans=ans*(x-i);    for(LL i=2;i<=y;i++)        ans/=i;    return ans;}LL getans(){    LL num=cal(n,3),sum=0;    for(int i=1;i<=n;i++)    {        int cnt=1;        for(int j=1;j<=n;j++)        {            if(i==j)continue;            temp[cnt]=atan2(y[j]-y[i],x[j]-x[i]);            if(temp[cnt]<0)temp[cnt]+=2*PI;            cnt++;            temp[cnt]=temp[cnt-1]+2*PI;            cnt++;        }        sort(temp+1,temp+cnt);        int l=1,r=1;        for(int j=1;j<n;j++)        {            while(temp[r]-temp[j]<PI&&r<cnt)r++;            while(temp[l]-temp[j]<=0.5*PI-eps&&l<cnt)l++;//[l,r-1];            LL len=LL(r-l);           // cout<<len<<"###"<<endl;            sum=sum+len;        }    }    return num-sum;}int main(){        int Case=0;        while(1)        {            read(n);            if(!n)break;            for(int i=1;i<=n;i++)                scanf("%lf%lf",&x[i],&y[i]);            printf("Scenario %d:\nThere are %lld sites for making valid tracks\n",++Case,getans());        }        return 0;}


0 0
原创粉丝点击