codeforces #308 D.Vanya and Triangles(枚举+斜率分块)

题目链接:

题目

题目大意:

给出2000个点,问能组成多少个面积不为1的三角形

题目分析:

只要三个点不共线,就能组成一个三角形,那么n个点在不考虑面积为0的情况的时候能得到C(n,3)个三角形,

那么考虑三点共线的情况,首先枚举一个点,那么对于一个任意两个与它构成的直线斜率相同的点会导致三点共线,那么我们看当前枚举的点每个斜率的点的个数,对于每个斜率,会贡献出C(m,2)种情况导致三点共线,那么利用这个n^2的复杂度可以解决这个问题

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#define eps 1e-8
#define MAX 2007

using namespace std;

typedef long long LL;

LL n;
int x[MAX],y[MAX];
vector<double> gradient;

int main ( )
{
    while ( ~scanf ( "%lld" , &n ) )
    {
        for ( int i = 0 ; i < n ; i++ )
            scanf ( "%d%d" , &x[i] , &y[i] );
        LL ans = n*(n-1LL)*(n-2LL)/6LL;
        for ( int i = 0 ; i < n ; i++ )
        {
            gradient.clear();
            for ( int j = i+1 ; j < n ; j++ )
            {       
                double k = (y[j]-y[i])*1.0/(x[j]-x[i]);
                if ( x[j] == x[i] ) gradient.push_back(10000);
                else gradient.push_back ( k );
            }
            sort ( gradient.begin(), gradient.end() );
            int len = gradient.size();
            LL cnt = 1;
            for ( int j = 1 ; j < len ; j++ )
            {
                if ( fabs(gradient[j]-gradient[j-1])<eps ) cnt++;
                else 
                {
                    ans -= cnt*(cnt-1LL)/2LL;   
                    cnt = 1;
                }
            }
            if ( cnt > 1 )
                ans -= cnt*(cnt-1LL)/2LL;
        }
        printf ( "%lld\n" , ans );
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值