Codeforces 660D Number of Parallelograms 【思维】

本文解决Codeforces660D题目,介绍如何通过找到点集中的平行四边形来计算其数量。利用平行四边形对角线中点重合的性质,使用C++实现算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:Codeforces 660D Number of Parallelograms

D. Number of Parallelograms
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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

题意:n个点,问你最多可以构成的平行四边形。

老题啦,平行四边形对角线中点重合,所以找中点就好了。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#define ll o<<1
#define rr o<<1|1
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<double, double> pii;
const int INF = 0x3f3f3f3f;
const int MAXN = 3*1e5 + 10;
double x[2010], y[2010];
pii ans[2001*2001];
map<pii, int> fp;
int main()
{
    int n;
    while(scanf("%d", &n) != EOF) {
        for(int i = 1; i <= n; i++) {
            scanf("%lf%lf", &x[i], &y[i]);
        }
        fp.clear(); int top = 0;
        for(int i = 1; i <= n; i++) {
            for(int j = i+1; j <= n; j++) {
                double xx = (x[i] + x[j]) / 2;
                double yy = (y[i] + y[j]) / 2;
                fp[pii(xx, yy)]++;
                ans[top++] = pii(xx, yy);
            }
        }
        sort(ans, ans+top);
        int N = unique(ans, ans+top) - ans;
        LL res = 0;
        for(int i = 0; i < N; i++) {
            int v = fp[ans[i]];
            res += 1LL * v * (v-1) / 2;
        }
        printf("%lld\n", res);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值