FZUOJ 2231 平行四边形数

来源:互联网 发布:浦口区网络问政 编辑:程序博客网 时间:2024/05/22 02:13

题目链接~~

题解:(1)可以计算每条线段的斜率和每条线段的长度,如果斜率相同且长度相同则能组成平行四边形(任意三个点不在一条直线上)。

             (2)可以计算每条线段的中点,然后中点相同的两条线段则可以组成平行四边形(任意三点不在一条直线上)。

解法2代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <vector>#include <algorithm>using namespace std ;const int INF = 0x3f3f3f3f ;const int MX = 500 + 10 ;#define INT __int64struct Edge{    INT x ,y ;}E1[MX] ,E2[MX*MX] ;bool cmp(const Edge &a ,const Edge &b){    if(a.x == b.x)        return a.y > b.y ;    else        return a.x > b.x ;}int main(){    //freopen("input.txt" ,"r" ,stdin) ;    int n ;    while(~scanf("%d" ,&n)){        for(int i = 0 ;i < n ; ++i){            scanf("%I64d%I64d" ,&E1[i].x ,&E1[i].y) ;        }        int num = 0 ;        for(int i = 0 ;i < n ; ++i)           for(int j = i+1 ;j < n ; ++j){              E2[num].x = E1[i].x + E1[j].x ;              E2[num].y = E1[i].y + E1[j].y ;              num++ ;           }        sort(E2 ,E2+num ,cmp) ;        INT x = E2[0].x ,y = E2[0].y ,ans = 1 ,sum = 0 ;        for(int i = 1;i < num ; ++i)           if(x == E2[i].x && y == E2[i].y){               ans++ ;           }else{               sum = sum + ans*(ans-1)/2 ;               ans = 1 ;               x = E2[i].x ;               y = E2[i].y ;           }        cout<<sum<<endl ;    }    return 0 ;}


0 0
原创粉丝点击