很有意义的一道二分题
题意:给出点,问有多少个正方形
因为n<1000,就提示了可以用O(n^2)级别的代码
很容易就想到枚举正方形的两个点,那么可以通过这两个点可以得到对应的另外两个点,那么如何验证这两个点是否存在呢
当然会想到二分。先将坐标读入后排序好,然后用二分查询是否存在就可以了
因为给了3秒,所以O(n^2logn)也是可以接受的
难点:正方形并不一定是平行坐标轴的,可能是斜的,所以如何用两个已知点求得另外两个点呢?
这里有个公式可以用来变换
double x = tx * cos(w) - ty * sin(w);
double y = tx * sin(w) + ty * cos(w);
可以得到点绕着圆心转w后的坐标
所以旋转后的坐标我们就能求出来了,假如枚举出的两个点分别是A,B
那么C点就是B围绕A旋转90度,D就是A围绕B旋转-90度
或者C点就是B围绕A旋转-90度,D就是A围绕B旋转90度
因为一条边可能对应两个方向不同的正方形嘛
那么怎么使枚举的时候不会重复计算正方形呢
假如枚举A是用下标i,那么枚举B的时候j要从i+1开始枚举
另外,在二分查找点的时候,应该在排序后的[j+1,n]的下标范围内二分查找,也就是查找B点后面的点,这样点的枚举就有次序了,就不会出现重复记数了
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int MX = 1000 + 10;
const int INF = 0x3f3f3f3f;
const double exps = 1e-6;
const double pi = acos(-1.0);
struct Point {
int x, y;
Point() {}
Point(int a, int b) {
x = a;
y = b;
}
bool operator<(const Point&b)const {
if(x == b.x) return y < b.y;
return x < b.x;
}
} P[MX];
bool BS(Point p, int l, int r) {
int L = l, R = r, m;
while(L <= R) {
m = (L + R) >> 1;
if(P[m].x == p.x && P[m].y == p.y) return true;
if(P[m] < p) L = m + 1;
else R = m - 1;
}
return false;
}
bool transXY(Point A, Point B, Point &C, int f) {
int tx = B.x - A.x, ty = B.y - A.y;
C.x = A.x - ty * f;
C.y = A.y + tx * f;
}
int main() {
//freopen("input.txt", "r", stdin);
int n;
while(~scanf("%d", &n), n) {
for(int i = 1; i <= n; i++) {
scanf("%d%d", &P[i].x, &P[i].y);
}
sort(P + 1, P + 1 + n);
int ans = 0;
for(int i = 1; i <= n - 3; i++) {
for(int j = i + 1; j <= n - 2; j++) {
Point C, D;
transXY(P[i], P[j], C, 1);
transXY(P[j], P[i], D, -1);
if(BS(C, j + 1, n) && BS(D, j + 1, n)) {
ans++;
}
transXY(P[i], P[j], C, -1);
transXY(P[j], P[i], D, 1);
if(BS(C, j + 1, n) && BS(D, j + 1, n)) {
ans++;
}
}
}
printf("%d\n", ans);
}
return 0;
}