您的位置:首页 > 其它

UVA-11529(极角排序)

2016-07-05 23:21 218 查看
题意:

n个不共线的点,任意三个都可以形成一个三角形,平均每个三角形内部包含多少个点;

思路:

反方向考虑,每个点被多少个三角形包含,

枚举每个点,以这个点为原点,再枚举剩下的点,在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;

double x
,y
,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;
}
double solve()
{
LL ans=0,sum,num=cal(n,3);
for(int i=1;i<=n;i++)
{
sum=0;
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 pos=1;
for(int j=1;j<n;j++)
{
while(temp[pos]-temp[j]<=PI&&pos<cnt)pos++;//[i+1,pos-1]
LL len=(LL)(pos-1-j);
if(len>=2)sum=sum+cal(len,2);
}
ans=cal(n-1,3)-sum+ans;
}
return ans*1.0/(num*1.0);
}

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("City %d: %.2lf\n",++Case,solve());
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UVA