zoukankan      html  css  js  c++  java
  • 1058

    1058 - Parallelogram Counting

    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.

    Input

    Input starts with an integer T (≤ 15), denoting the number of test cases.

    The first line of each test case contains an integer n (1 ≤ n ≤ 1000). Each of the next n lines, contains 2 space-separated integers x and y (the coordinates of a point) with magnitude (absolute value) of no more than 1000000000.

    Output

    For each case, print the case number and the number of parallelograms that can be formed.

    Sample Input

    Output for Sample Input

    2

    6

    0 0

    2 0

    4 0

    1 1

    3 1

    5 1

    7

    -2 -1

    8 9

    5 7

    1 1

    4 8

    2 0

    9 8

    Case 1: 5

    Case 2: 6

    分析:学了这么多年数学,然而只知道平行四边形对角线交于一点,却没想到只要存在两点的中点与另两点的中点相同,就能构成平行四边形。

    代码:

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    using namespace std;
    #define N 510000

    struct node
    {
    int x;
    int y;
    }arr[N], mid[N];
    bool cmp(struct node a, struct node b)
    {
    if(a.x != b.x)
    return a.x < b.x;
    return a.y < b.y;
    }
    int main(void)
    {
    int T, cas, n;

    scanf("%d", &T);
    cas = 0;

    while(T--)
    {
    cas++;
    memset(mid, 0, sizeof(mid));

    scanf("%d", &n);

    for(int i = 0; i < n; i++)
    {
    scanf("%d%d", &arr[i].x, &arr[i].y);
    }
    int num = 0;

    for(int i = 0; i < n-1; i++)
    {
    for(int j = i+1; j < n; j++)
    {
    mid[num].x = arr[i].x + arr[j].x;
    mid[num].y = arr[i].y + arr[j].y;
    num++;
    }
    }
    sort(mid, mid+num, cmp);/// 排序是为了方便下面比较
    int cnt = 1;
    int ans = 0;
    int flag = 0;

    for(int i = 1; i < num; i++)
    {
    if(mid[i].x == mid[flag].x && mid[i].y == mid[flag].y)
    {
    cnt++;
    }
    else
    {
    ans += cnt * (cnt - 1) / 2;///计算就是组合数:从 n个数里面取出 2个数 ,就是 C(n,2)
    cnt = 1;
    flag = i;
    }
    }
    if(cnt>1)
    ans += (cnt - 1) * cnt / 2; /// 判断循环的最后一组数据,如果也存在相同的点,就加上
    printf("Case %d: %d ", cas, ans);
    }
    return 0;
    }

  • 相关阅读:
    为什么要持续输出
    从Libra看区块链的机遇
    windows 通过choco 安装 .net core windowshosting
    java 正则表达式空格无法匹配
    docker jenkins 安装
    docker jenkins 前端node项目 自动化部署异常 env: ‘node’: No such file or directory
    jenkin docker node 自动化部署配置
    centos docker redis 安装
    Windows server 2012 出现大量无名已断开连接用户清楚办法
    spring boot 打包jar后访问classes文件夹的文件提示地址不存在
  • 原文地址:https://www.cnblogs.com/dll6/p/7168832.html
Copyright © 2011-2022 走看看