D. Number of Parallelograms
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given n points on a plane. All the points are distinct and no three of them lie on the same line. Find the number of parallelograms with the vertices at the given points.
Input
The first line of the input contains integer n (1 ≤ n ≤ 2000) — the number of points.
Each of the next n lines contains two integers (xi, yi) (0 ≤ xi, yi ≤ 109) — the coordinates of the i-th point.
Output
Print the only integer c — the number of parallelograms with the vertices at the given points.
Example
input
4 0 1 1 0 1 1 2 0
output
1
题解:因为没有三点共线的情况,所以斜率相同的两条线段肯定平行不共线。那么由平行四边形的定义,一对边平行且相等,称为平行四边形。所以只
需要统计每条线段的距离和斜率就好了。
AC代码
/* ***********************************************
Author :xdlove
Created Time :2016年06月10日 星期五 13时11分56秒
File Name :2016-06-09.cpp
************************************************ */
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
const int N = 4e6 + 10;
struct Node
{
int x,y;
void read()
{
scanf("%d %d",&x,&y);
}
ll mul(const int x) const
{
return 1LL * x * x;
}
ll dist(const Node &a) const
{
return mul(x - a.x) + mul(y - a.y);
}
}node[2005];
struct Interval
{
int x,y;
ll len;
bool operator < (const Interval &a) const
{
if(y != a.y) return y < a.y;
if(x != a.x) return x < a.x;
return len < a.len;
}
bool operator == (const Interval &a) const
{
if(y == a.y && x == a.x && len == a.len)
return true;
return false;
}
}interval[N];
void Gcd(int &x,int &y)
{
if(x == 0 || y == 0)
{
x = abs(x);
y = abs(y);
return;
}
int gcd = __gcd(abs(x),abs(y));
x /= gcd;
y /= gcd;
}
ll cal(int x)
{
return 1LL * x * (x - 1) / 2;
}
void solve()
{
int n,m;
cin >> n;
m = 0;
for(int i = 0; i < n; ++i) node[i].read();
for(int i = 0; i < n; ++i)
{
for(int j = i + 1; j < n; ++j)
{
int kx = node[i].x - node[j].x;
int ky = node[i].y - node[j].y;
if(kx < 0 && ky < 0)
kx = -kx,ky = -ky;
else if(kx < 0 || ky < 0)
kx = abs(kx),ky = -abs(ky);
Gcd(kx,ky);
interval[m].x = kx;
interval[m].y = ky;
interval[m].len = node[i].dist(node[j]);
m++;
}
}
sort(interval,interval + m);
ll ans = 0;
int pos = 0;
//cout << interval[0].x << " " << interval[0].y << " " << interval[0].len << endl;
for(int i = 1; i < m; ++i)
{
//cout << interval[i].x << " " << interval[i].y << " " << interval[i].len << endl;
if(!(interval[i] == interval[pos]))
{
ans += cal(i - pos);
pos = i;
}
}
ans += cal(m - pos);
cout << ans / 2 << endl;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
solve();
return 0;
}