hdu 5020 排列组合+stl map (或者离散化)

首先为了重复计算,先对点进行排序,然后选定一个点,找到这个点斜率相同的点,然后利用排列组合知识就能知道点对有c(2,n)个,因此只需枚举每个点,找到它序位后面的点中到它斜率相同的点为集合,然后每个集合中取c(2,n),就能得到答案,利用map可以省去离散化Hash的过程

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <map>
#define MAX 1007

using namespace std;

typedef long long LL;
typedef pair<long long , long long> PLL;

map<PLL,LL> mp;

struct Point
{
    LL x,y;
    bool operator < ( const Point& a ) const
    {
        if ( x == a.x ) return y < a.y;
        return x < a.x;
    }
}p[MAX];

LL gcd ( LL a , LL b )
{
    return b == 0 ? a : gcd ( b , a%b );
}

int t,n;

int main ( )
{
    scanf ( "%d" , &t );
    while ( t-- )
    {
        scanf ( "%d" , &n );
        for ( int i = 1 ; i <= n ; i++ )
            scanf ( "%lld%lld" , &p[i].x , &p[i].y );
        sort ( p+1 , p+n+1 );
        LL ans = 0;
        for ( int i = 1 ; i < n ; i++ )
        {
            mp.clear();
            for ( int j = i+1 ; j <= n ; j++ )
            {
                LL x = p[j].x - p[i].x;
                LL y = p[j].y - p[i].y;
                LL g = gcd ( x , y );
                x /= g , y /= g;
                mp[make_pair( x , y )]++;
            }
            map<PLL,LL>::iterator it;
            for ( it= mp.begin(); it != mp.end() ; it++ )
            {
                LL temp = it->second;
                ans += temp*(temp-1)/2;
            }
        }
        printf ( "%lld\n" , ans );
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值