Codeforces 660D Number of Parallelograms

题目链接:

http://codeforces.com/contest/660/problem/D

题意:

给定若干点,判断能组成多少个平行四边形。

分析:

暴力枚举点,算出向量,如果两个向量相等,则可以构成一组平行四边形。然后最后不要忘记除掉重复计算。

代码:

#include<cstdio>
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
typedef pair<int, int >pii;
const int maxn = 2000 + 5;
pii p[maxn], a[maxn];
map<pii, int>cnt;
#define x first
#define y second
bool cmp(pii a, pii b)
{
    if(a.x == b.x) return a.y > b.y;
    else return a.x > b.x;
}
int main (void)
{
    int n;cin>>n;
    for(int i = 0; i < n; i++)
        cin>>p[i].x>>p[i].y;
    if(n < 4) return cout<<0<<endl, 0;
    sort(p, p + n, cmp);
    int res = 0;
    for(int i = 0; i < n; i++){
        for(int j = i + 1; j < n; j++){
            a[i] = pii(p[i].x - p[j].x, p[i].y - p[j].y) ;
            res += cnt[a[i]];
            cnt[a[i]]++;
        }
    }
    cout<<res / 2<<endl;
}
原文地址:https://www.cnblogs.com/Tuesdayzz/p/5758666.html