LightOJ - 1058 - Parallelogram Counting(数学,计算几何)

链接:

https://vjudge.net/problem/LightOJ-1058

题意:

There are n distinct points in the plane, given by their integer coordinates. Find the number of parallelograms whose vertices lie on these points. In other words, find the number of 4-element subsets of these points that can be written as {A, B, C, D} such that AB || CD, and BC || AD. No four points are in a straight line.

思路:

考虑平行四边形,对角线的中点相交,所以枚举所有中点,相同的点组合数求解。
map爆内存。。。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<utility>

using namespace std;
typedef long long LL;
const int INF = 1e9;

const int MAXN = 1e3+10;
const int MOD = 1e9+7;

struct Node
{
    double x, y;
}node[MAXN*MAXN];

int n;
int x[MAXN], y[MAXN];

Node GetMid(int l, int r)
{
    return Node{double(x[l]+x[r])/2.0, double(y[l]+y[r])/2.0};
}

bool Cmp(Node a, Node b)
{
    if (a.x != b.x)
        return a.x < b.x;
    return a.y < b.y;
}

int main()
{
    int cnt = 0;
    int t;
    scanf("%d", &t);
    while(t--)
    {
        printf("Case %d:", ++cnt);
        scanf("%d", &n);
        for (int i = 1;i <= n;i++)
            scanf("%d%d", &x[i], &y[i]);
        int tot = 0;
        for (int i = 1;i <= n;i++)
        {
            for (int j = i+1;j <= n;j++)
            {
                node[++tot] = GetMid(i, j);
            }
        }
        int res = 0;
        sort(node+1, node+1+tot, Cmp);
        int tmp = 1;
        for (int i = 2;i <= tot;i++)
        {
            if (node[i].x == node[i-1].x && node[i].y == node[i-1].y)
                tmp++;
            else
            {
                if (tmp >= 2)
                    res += 1LL*tmp*(tmp-1)/2;
                tmp = 1;
            }
            if (i == tot && tmp >= 2)
                res += 1LL*tmp*(tmp-1)/2;

        }
        printf(" %d\n", res);
    }

    return 0;
}

原文地址:https://www.cnblogs.com/YDDDD/p/11886514.html

时间: 2024-10-08 00:39:15

LightOJ - 1058 - Parallelogram Counting(数学,计算几何)的相关文章

Light OJ - 1058 Parallelogram Counting

题目链接:http://lightoj.com/volume_showproblem.php?problem=1058

Parallelogram Counting(平行四边形个数,思维转化)

1058 - Parallelogram Counting    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB There are n distinct points in the plane, given by their integer coordinates. Find the number of parallelograms whose vertices lie on these po

计算几何 + 统计 --- Parallelogram Counting

Parallelogram Counting Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5749   Accepted: 1934 Description There are n distinct points in the plane, given by their integer coordinates. Find the number of parallelograms whose vertices lie o

【POJ】Parallelogram Counting(HASH,数学之平行四边形)

Parallelogram Counting 题意:输入t表示有t组数据 每组数据输入一个数n,表示有n个点 然后有n行,每行是这个点的(x,y) 问这些点能组成多少个平行四边形 思路:求中点,中点一样的是一个平行四边形. 记录同一个中点的个数sum(初始为1),平行四边形数是(sum-1) * sum / 2; #include<iostream> #include<algorithm> using namespace std; typedef long long ll; con

LightOJ - 1148 - Mad Counting

先上题目: 1148 - Mad Counting   PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 MB Mob was hijacked by the mayor of the Town "TruthTown". Mayor wants Mob to count the total population of the town. Now the naive approach to

LightOJ 1305 - Area of a Parallelogram(数学啊 )

题目链接:http://lightoj.com/volume_showproblem.php?problem=1305 A parallelogram is a quadrilateral with two pairs of parallel sides. See the picture below: Fig: a parallelogram Now you are given the co ordinates of A, B and C, you have to find the coordi

LightOJ 1062 - Crossed Ladders 基础计算几何

http://www.lightoj.com/volume_showproblem.php?problem=1062 题意:问两条平行边间的距离,给出从同一水平面出发的两条相交线段长,及它们交点到水平面的高. 思路:计算几何怎么可能直接算出答案orz解了好久方程觉得不对,应该是二分枚举平行边的距离,通过相似三角形,算出交点的高,与题目比较,小于误差范围就行了. /** @Date : 2016-12-10-18.18 * @Author : Lweleth ([email protected])

LightOJ - 1148 Mad Counting(坑)

Mad Counting Time Limit: 500MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submit Status Description Mob was hijacked by the mayor of the Town "TruthTown". Mayor wants Mob to count the total population of the town. Now the naive a

17997 Simple Counting 数学

17997 Simple Counting 时间限制:2000MS  内存限制:65535K提交次数:0 通过次数:0 题型: 编程题   语言: 不限定 Description Ly is crazy about counting . Recently , he got a simple problem , but he had to learn Gaoshu these days .So , he turns to you for help . You are given a sequenc