HDOJ2428(Stars)
Stars
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 433 Accepted Submission(s): 156
Problem Description
Lucy loves stars very much. There are N (1 <= N <= 1000) stars in the sky. Assume the sky is a flat plane. All of the stars lie on it with a location (x, y), -10000 <= x, y <= 10000. Now, Lucy wants you to tell her how many squares with each of four vertices formed by these stars, and the edges of the squares should parallel to the coordinate axes.
Input
The first line of input is the number of test case.
The first line of each test case contains an integer N. The following N lines each contains two integers x, y, meaning the coordinates of the stars. All the coordinates are different.
The first line of each test case contains an integer N. The following N lines each contains two integers x, y, meaning the coordinates of the stars. All the coordinates are different.
Output
For each test case, output the number of squares in a single line.
Sample Input
2 1 1 2 4 0 0 1 0 1 1 0 1
Sample Output
0 1
/*
2009-05-13 16:53:20 Accepted 2428 31MS 264K 1174 B C++
*/
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
const int N = 1003;
typedef struct node
{
int x, y;
bool operator <(const node tmp) const
{
if(x != tmp.x)
return x < tmp.x;
else
return y < tmp.y;
}
};
node px[N];
int main()
{
int T, n, i, j, k, t;
bool flag;
int cntx, cnty;
node c1, c2;
set<node> vv;
scanf("%d", &T);
while(T--)
{
vv.clear();
scanf("%d", &n);
for(i = 0; i < n; i++)
{
scanf("%d%d", &px[i].x, &px[i].y);
vv.insert(px[i]);
}
sort(px, px + n);
i = 0;cntx = 0;
for(i = 0; i < n;)
{
flag = false;
j = i;
while((i + 1) < n && px[i].x == px[i + 1].x)
{
if(px[i].y != px[i + 1].y)
flag = true;
i++;
}
if(flag)
{
for(k = j ; k <= i; k++)
for(t = j + 1; t <= i; t++)
if(px[t].y > px[k].y)
{
c1 = px[k];
c1.x += px[t].y - px[k].y;
c2 = px[t];
c2.x += px[t].y - px[k].y;
if(vv.find(c1) != vv.end() && vv.find(c2) != vv.end())
cntx++;
}
}
i++;
}
printf("%d\n", cntx);
}
return 0;
}
2009-05-13 16:53:20 Accepted 2428 31MS 264K 1174 B C++
*/
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
const int N = 1003;
typedef struct node
{
int x, y;
bool operator <(const node tmp) const
{
if(x != tmp.x)
return x < tmp.x;
else
return y < tmp.y;
}
};
node px[N];
int main()
{
int T, n, i, j, k, t;
bool flag;
int cntx, cnty;
node c1, c2;
set<node> vv;
scanf("%d", &T);
while(T--)
{
vv.clear();
scanf("%d", &n);
for(i = 0; i < n; i++)
{
scanf("%d%d", &px[i].x, &px[i].y);
vv.insert(px[i]);
}
sort(px, px + n);
i = 0;cntx = 0;
for(i = 0; i < n;)
{
flag = false;
j = i;
while((i + 1) < n && px[i].x == px[i + 1].x)
{
if(px[i].y != px[i + 1].y)
flag = true;
i++;
}
if(flag)
{
for(k = j ; k <= i; k++)
for(t = j + 1; t <= i; t++)
if(px[t].y > px[k].y)
{
c1 = px[k];
c1.x += px[t].y - px[k].y;
c2 = px[t];
c2.x += px[t].y - px[k].y;
if(vv.find(c1) != vv.end() && vv.find(c2) != vv.end())
cntx++;
}
}
i++;
}
printf("%d\n", cntx);
}
return 0;
}