The judgment theorem of Lightoj 1058 parallelogram

Source: Internet
Author: User



The main topic: give you n points, ask that n points can be composed of the maximum number of parallelogram.



Topic idea: This problem card time, and card memory. You have to try to optimize as much as possible.



The judgment theorem of parallelogram:


    1. The four sides of the two groups are parallel to each other by parallelogram (definition method).
    2. A set of parallel and equal four-sided shapes is parallelogram;
    3. The four sides of the two groups were equal to each other and parallelogram;
    4. The two groups of diagonally equal four-sided quadrilateral are parallelogram (two pairs of sides parallel judgment);
    5. The four-sided diagonal lines are parallelogram.


This problem is judged by theorem 5.



The midpoint coordinates of each edge are recorded, and if the midpoint coordinates of the two edges are the same, the two edges are shown to be a parallelogram two diagonal line.





#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#define INF 0x3f3f3f
#define MAX 1000005

using namespace std;

struct node
{
    double midx, midy;
    int x, y;
} Map [MAX];

int cmp (node A, node B)
{
    if (A.midx! = B.midx)
        return A.midx> B.midx;
    return A.midy> B.midy;
}
int main ()
{
    int T, n, i, j, num = 1, k;
    long long sum, cnt, q;
    scanf ("% d", & T);
    while (T--)
    {
        sum = 0;
        cnt = 0;
        scanf ("% d", & n);
        for (i = 1; i <= n; i ++)
        {
            scanf ("% d% d", & Map [i] .x, & Map [i] .y);
        }

        for (i = 1; i <= n; i ++)
        {
            for (j = i + 1; j <= n; j ++)
            {
                Map [cnt] .midx = (Map [i] .x + Map [j] .x) /2.0;
                Map [cnt ++]. Midy = (Map [i] .y + Map [j] .y) /2.0;
            }
        }

        sort (Map, Map + cnt, cmp); // To save time in subsequent operations, sort first

        i = 0;
        k = 0;
        q = 1;
        while (i <cnt)
        {
            if (Map [i] .midx == Map [k] .midx && Map [i] .midy == Map [k] .midy && k! = i)
                {
                    sum + = q; // The newly added edge can form a new parallelogram with every previous edge with the same midpoint
                    q ++;
                }
            else if (Map [i] .midx! = Map [k] .midx || Map [i] .midy! = Map [k] .midy)
            {
                k = i;
                q = 1;
            }
            i ++;
        }
        printf ("Case% d:% lld \ n", num ++, sum);
    }
    return 0;
} 





The judgment theorem of Lightoj 1058 parallelogram


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.