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

  • 相关阅读:
    蛙蛙推荐:简单介绍一下托管容器持久性(CMP),顺便征集一下.NET CMP2.0的改进方案
    15分钟内快速构建数据访问层(翻译)
    【蛙蛙推荐】.NET 2.0里使用强类型数据创建多层应用
    蛙蛙推荐:迎接web2.0:写一个RSS.HTC组件
    蛙蛙推荐:web下的授权简单解决方案
    J2me访问c# Web Services
    2006年3月份技术随笔
    声讨vs.net,讨论用户控件,编码等问题
    Hadoop中mapred包和mapreduce包的区别
    hbase MapReduce程序样例入门
  • 原文地址:https://www.cnblogs.com/dll6/p/7168832.html
Copyright © 2011-2022 走看看