Boomerang Constellations
题意:
给定N≤2×103个点,求满足dis(x,y)=dis(y,z)的三元组(x,y,z)个数,(x,y,z)与(z,y,x)被认为是相同的
分析:
首先有个显然的C3n的做法,直接枚举3个点,显然会T
可以发现,枚举中心点就可以了,预处理出两两点之间的距离dis[i][j]
对于每个中心点i,sort一下dis[i][1...n],对于一些相同的距离个数k,它们的贡献是C2k
那么答案就是∑ni=1∑kC2k
代码:
//
// Created by TaoSama on 2016-01-10
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 2e3 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n;
int x[N], y[N], d[N][N];
int get(int i, int j) {
return (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);
}
int main() {
freopen("boomerang_constellations.txt", "r", stdin);
freopen("boomerang_constellations_out.txt", "w", stdout);
ios_base::sync_with_stdio(0);
int t; scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%d%d", x + i, y + i);
for(int i = 1; i <= n; ++i)
for(int j = i; j <= n; ++j)
d[i][j] = d[j][i] = get(i, j);
long long ans = 0;
for(int i = 1; i <= n; ++i) {
sort(d[i] + 1, d[i] + 1 + n);
for(int j = 1; j <= n; ++j) {
int last = d[i][j], cnt = 1;
while(j + 1 <= n && d[i][j + 1] == last) ++cnt, ++j;
// printf("%d: %d cnt: %d last: %d\n", i, j, cnt, last);
ans += cnt * (cnt - 1) >> 1;
}
}
static int kase = 0;
printf("Case #%d: %lld\n", ++kase, ans);
}
return 0;
}
High Security
题意:
给定一个2∗N的网格,N≤103,′.′是空地