【POJ】Parallelogram Counting (HASH, parallelogram of mathematics)

Parallelogram Counting

Question meaning: input t means that there are t groups of data

   Enter a number n for each set of data, indicating that there are n points

   Then there are n lines, each line is (x, y) for this point

   How many parallelograms can these points form?

Idea: Find the midpoint, the same midpoint is a parallelogram. 

   Record the number sum of the same midpoint (initially 1), the number of parallelograms is (sum-1) * sum / 2;

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
struct zz
{
    double x, y;
};
zz a[1007];
zz b[1007 * 1007];
bool cmp(zz u, zz v)
{
    if(u.x == v.x) return u.y < v.y;
    return u.x < v.x;
}
intmain ()
{
    /*freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);*/

    int n;
    cin >> n;
    while(n--)
    {
        int i, j, m;
        cin >> m;
        for(i = 0; i < m; i++)
        {
            cin >> a[i].x >> a[i].y;
        }
        int k = 0;
        for(i = 0; i < m - 1; i++)
        {
            for(j = i + 1; j < m; j++)
            {
                if(a[i].x == a[j].x && a[i].y == a[j].y) continue;
                b[k].x = (a[i].x + a[j].x) / 2;
                b[k ++].y = (a[i].y + a[j].y) / 2 ;
            }
        }
        sort(b, b + k, cmp);
        int sum = 0, ans = 1;
        for(i = 0; i < k - 1; i++)
        {
            if(b[i].x == b[i + 1].x && b[i].y == b[i + 1].y)    ans++;
            else
            {
                sum += (years - 1 ) * years / 2 ;
                years = 1 ;
            }
        }
        cout << sum << endl;

    }
    /*fclose(stdin);
    fclose(stdout);*/
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325138393&siteId=291194637