Jzoj P3493 三角形___容斥+枚举

题目大意:

平面上有 n n 个点,求出用这些点可以构成的三角形数。

n<=3000,坐标的绝对值不超过 104 10 4 ,保证没有重合的点。

分析:

N N 个点中选3个点构成图形的方案为 C3n C n 3
枚举第一个点,对于剩下的点按照和这个点连线的斜率排序一下,把相同斜率的线段放在一起,可以算出选到两条相同斜率的线段方案数,即不能构成三角形的方案数,然后减去,注意处理斜率不存在的情况。

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define N 3005
#define eps 1e-9

using namespace std;

typedef long long ll;

struct Node {int x, y; }a[N];
double c[N];
ll n, ans;
int cnt;

bool cmp(Node aa, Node bb)
{
    if (aa.y == bb.y) return aa.x > bb.x;
    return aa.y > bb.y;
}

int main()
{
    scanf("%lld", &n);
    if (n == 1 || n == 2) 
    {
        printf("0\n");
        return 0;
    }
    ans = n * (n - 1) * (n - 2) / 6;
    for (int i = 1; i <= n; i++)
         scanf("%d %d", &a[i].x, &a[i].y);
    sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i < n; i++)
    {
         cnt = 0;
         for (int j = i + 1; j <= n; j++)
         {
              if (a[j].x == a[i].x) c[++cnt] = -10005;
              else if (a[j].y == a[i].y) c[++cnt] = -10006;
                   else c[++cnt] = (double)(a[i].y - a[j].y) / (a[i].x - a[j].x);         
         }
         sort(c + 1, c + cnt + 1);
         ll tot = 1;
         c[cnt + 1] = 10005;
         for (int i = 2; i <= cnt + 1; i++)
              if (c[i] <= c[i - 1] + eps) tot++;
              else
              {
                  if (tot != 1) ans = ans - (tot) * (tot - 1) / 2;
                  tot = 1;  
              } 
    }
    printf("%lld\n", ans);  
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值