[POJ2002]Squares(计算几何,二分)

题目链接:http://poj.org/problem?id=2002

给定一堆点,求这些点里哪些点可以构成正方形,题目给定n<=1000,直接枚举四个点是肯定会超时的,因此要做一些优化。

有公式,已知两个点在正方形对角,分别是(x1,y1)和(x2,y2),那么围成正方形后另外两个点(x3,y3)和(x4,y4)分别为:

x3 = x2 - (x2 - y1)
y3 = x2 + (x2 - x1)
x4 = x1 - (x2 - y1)
y4 = y1 + (x2 - x1)

那么我们需要枚举两个点,最后推算出这两个点和哪两个点可以围成正方形,然后再去查看剩下的点集里是否存在这两个点。我先排序,再做的二分查找,这个题也可以用hash去做,通过hash可以在O(1)的时间内确定是否存在,更加快捷。特别需要注意的是,假如存在一个正方形,那么必然会枚举到这里面的分别两条边。这个时候需要除以2即可。

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <iomanip>
 4 #include <cstring>
 5 #include <climits>
 6 #include <complex>
 7 #include <fstream>
 8 #include <cassert>
 9 #include <cstdio>
10 #include <bitset>
11 #include <vector>
12 #include <deque>
13 #include <queue>
14 #include <stack>
15 #include <ctime>
16 #include <set>
17 #include <map>
18 #include <cmath>
19
20 using namespace std;
21
22 const int maxn = 1010;
23 typedef struct Point {
24     int x, y;
25     Point() {}
26     Point(int xx, int yy) : x(xx), y(yy) {}
27 }Point;
28 int n, ans;
29 Point p[maxn];
30
31 bool cmp(Point a, Point b) {
32     if(a.x == b.x) return a.y < b.y;
33     return a.x < b.x;
34 }
35
36 bool bs(int x, int y) {
37     int ll = 0, rr = n, mm;
38     while(ll <= rr) {
39         mm = (ll + rr) >> 1;
40         if(p[mm].x == x && p[mm].y == y) return 1;
41         else if(cmp(p[mm], Point(x, y))) ll = mm + 1;
42         else rr = mm - 1;
43
44     }
45     return 0;
46 }
47
48 int main() {
49     // freopen("in", "r", stdin);
50     int x3, y3, x4, y4;
51     while(~scanf("%d", &n) && n) {
52         ans = 0;
53         for(int i = 0; i < n; i++) {
54             scanf("%d%d", &p[i].x, &p[i].y);
55         }
56         sort(p, p+n, cmp);
57         for(int i = 0; i < n; i++) {
58             for(int j = i + 1; j < n; j++) {
59                 x3 = p[j].x - (p[j].y - p[i].y);
60                 y3 = p[j].y + (p[j].x - p[i].x);
61                 x4 = p[i].x - (p[j].y - p[i].y);
62                 y4 = p[i].y + (p[j].x - p[i].x);
63                 if(bs(x3, y3) && bs(x4, y4)) ans++;
64             }
65         }
66         printf("%d\n", ans / 2);
67     }
68     return 0;
69 }
时间: 2024-11-18 04:57:40

[POJ2002]Squares(计算几何,二分)的相关文章

Poj2002 Squares

题意描述:有一堆平面散点集,任取四个点,求能组成正方形的不同组合方式有多少.相同的四个点,不同顺序构成的正方形视为同一正方形. 思路变迁: 1.最简单的方法,直接暴力搜索,即依次取四个顶点,根据其坐标判断是否能组成正方形.组成正方形的条件是四个顶点可组成的六条边里面,有四条相等,剩下两条相等.当然由于其时间复杂度为O(n^4),所以提交结果为TLE 2.考虑降低时间复杂度.如任取三个顶点,根据组成正方形的条件计算得到第四个顶点,判断其是否在点的集合内,其复杂度为O(n^3).或者任取两个顶点,根

【POJ】2318 TOYS ——计算几何+二分

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10281   Accepted: 4924 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away w

【bzoj1822】[JSOI2010]Frozen Nova 冷冻波 计算几何+二分+网络流最大流

题目描述 WJJ喜欢“魔兽争霸”这个游戏.在游戏中,巫妖是一种强大的英雄,它的技能Frozen Nova每次可以杀死一个小精灵.我们认为,巫妖和小精灵都可以看成是平面上的点. 当巫妖和小精灵之间的直线距离不超过R,且巫妖看到小精灵的视线没有被树木阻挡(也就是说,巫妖和小精灵的连线与任何树木都没有公共点)的话,巫妖就可以瞬间杀灭一个小精灵. 在森林里有N个巫妖,每个巫妖释放Frozen Nova之后,都需要等待一段时间,才能再次施放.不同的巫妖有不同的等待时间和施法范围,但相同的是,每次施放都可以

POJ 2398 计算几何+二分+排序

Toy Storage Time Limit: 1000MS  Memory Limit: 65536K Total Submissions: 3953  Accepted: 2334 Description Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box t

POJ 2318 计算几何+二分

TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10425 Accepted: 5002 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away when

poj3347 Kadj Squares (计算几何)

D - Kadj Squares Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description In this problem, you are given a sequence S1, S2, ..., Sn of squares of different sizes. The sides of the squares are integer numb

hdu 4033 Regular Polygon 计算几何 二分+余弦定理

题目链接 给一个n个顶点的正多边形, 给出多边形内部一个点到n个顶点的距离, 让你求出这个多边形的边长. 二分边长, 然后用余弦定理求出给出的相邻的两个边之间的夹角, 看所有的加起来是不是2Pi. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #includ

hdu 3264 09 宁波 现场 E - Open-air shopping malls 计算几何 二分

Description The city of M is a famous shopping city and its open-air shopping malls are extremely attractive. During the tourist seasons, thousands of people crowded into these shopping malls and enjoy the vary-different shopping. Unfortunately, the

POJ 3347 Kadj Squares(计算几何)

传送门 Kadj Squares Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2937 Accepted: 1151 Description In this problem, you are given a sequence S1, S2, -, Sn of squares of different sizes. The sides of the squares are integer numbers. We locate