uva11529 Strange Tax Calculation

题目要求是给你n个点,求n个点形成的所有三角形中包含的点的总数的平均值。
题目可以理解为求每个点能在多少三角形内。我们枚举一个点x,然后将其他点按照极角排序,那么每一个点i到与点i成pi的范围内的所有点组成的三角形都不包含点x,然后我们就可以求出所有的解。
里面有一些小的需要注意的问题,就是有的点形成的极角会在(0,-pi),我们求解的时候会有一点小小的麻烦,我们可以将其+2pi来解决。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
typedef long long LL;
using namespace std;
int N;
const double pi = acos(-1.0);
const int maxn = 1500*2;
double X[maxn],Y[maxn],K[maxn];
LL solve(int n)
{
    int cnt = 0;
    for(int i=0; i<N; i++)
    {
        if(i==n)continue;
        K[cnt++] = atan2(Y[i]-Y[n],X[i]-X[n]);
        K[cnt++] = atan2(Y[i]-Y[n],X[i]-X[n])+2*pi;
    }
    sort(K,K+cnt);
//    int _count = 0;
    int pos = 0;
    LL num =1;
    LL res = 0;
    for(int i=0; i<N-1; i++)
    {
        num--;
        while(K[pos]<K[i]+pi)
        {
            pos++;
            num++;
        }
        res+=(num-1)*(num-2)/2;
    }
    LL sum = (LL)(N-1)*(LL)(N-2)*(LL)(N-3)/(LL)6;
    return  sum - res;
}
int main()
{
    int kas = 1;
    while(~scanf("%d",&N)&&N)
    {
        LL res = 0;
        for(int i=0; i<N; i++)
        {
            scanf("%lf%lf",&X[i],&Y[i]);
        }
        for(int i=0; i<N; i++)
        {
            res += solve(i);
        }
        LL sum = (LL)(N)*(LL)(N-1)*(LL)(N-2)/(LL)6;
        printf("City %d: %.2lf\n",kas++,(double)res/(double)sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值