码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-11-19 01:09:33      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:math   ios   while   printf   四边形   pac   mes   count   内存   

链接:

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;
}

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

标签:math   ios   while   printf   四边形   pac   mes   count   内存   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!