HDU 2428

来源:互联网 发布:唯一视觉婚纱摄影 知乎 编辑:程序博客网 时间:2024/06/18 16:34

题目

露西非常爱星星。
天空中有N(1 <= N <= 1000)颗星。
假设天空是平面的。
所有的星星都位于它的位置(x,y),-10000 <= x,y <= 10000.
现在,露西想让你告诉她这些星形成的四个顶点中的每一个有多少个正方形,正方形边缘应平行于坐标轴。

输入:
第一行输入是测试用例数。
每个测试用例的第一行包含一个整数N.以下N行各包含两个整数x,y,表示星的坐标。 所有坐标都不同。

输出:
输出正方形的数目


思路

分为两部分,第一部分是输入点并进行点的排序;
第二部分通过选定的两点判断是否四个点都存在。

x最大的点排列在前,再者y最大的点排列在前。
如何另外两点是否存在?
题目的时间限制意思就是让我们简化时间复杂度到0(n^3)减少到0(n^2logn)
使用hash判断点是否存在就能达到要求。

代码

#include<iostream>#include<map>#include<string>using namespace std;hash<string> str_hash;struct point{    int x;    int y;}points[1001];map<size_t, bool> San;int main(){    int test;    cin >> test;    while (test--)    {        int num = 0;        int n;        cin >> n;        for (int i = 0; i < n; i++)        {            int x, y;            cin >> x >> y;            points[i].x = x;            points[i].y = y;            size_t s = str_hash(to_string(points[i].x) + "," + to_string(points[i].y));            San[s] = true;            for (int j = 0; j < i; j++)            {                if (points[i].x>points[j].x)                {                    int z = points[j].x;                    points[j].x = points[i].x;                    points[i].x = z;                    z = points[j].y;                    points[j].y = points[i].y;                    points[i].y = z;                }                else if (points[i].x == points[j].x)                {                    if (points[i].y > points[j].y)                    {                           int z = points[j].x;                        points[j].x = points[i].x;                        points[i].x = z;                        z = points[j].y;                        points[j].y = points[i].y;                        points[i].y = z;                    }                }            }        }        for (int i = 0; i < n; i++)        {            //cout << points[i].x << " " << points[i].y << endl;            for (int j = i+1; j < n; j++)            {                if (points[j].x == points[i].x)                {                    int length = points[i].y - points[j].y;                    point p1,p2;                    p1.x = points[i].x - length;                    p1.y = points[i].y - length;                    p2.x = points[i].x - length;                    p2.y = points[i].y;                    string s1, s2;                    s1 = to_string(p1.x) + "," + to_string(p1.y);                    s2 = to_string(p2.x) + "," + to_string(p2.y);                    if (San.find(str_hash(s1)) != San.end() && San.find(str_hash(s2)) != San.end())                        num++;                }            }        }        cout << num << endl;    }    return 0;}
原创粉丝点击