Problem Description
During a programming contest, teams cannot sit close to each other, because then a team might copy the solution of another team. You are given the locations of the teams and the minimum required Euclidian distance between two teams. You have to find the number of pairs of teams that sit too close to each other.
 
Input
On the first line an integer t (1 <= t <= 100): the number of test cases. Then for each test case:
One line with two integers n (1 <= n <= 100 000) and d (1 <= d <= 50): the number of teams and the minimum distance between two teams.
n lines with two integers xi (0 <= xi <= 1 000 000 000) and yi (0 <= yi <= 1 000 000 000): the coordinates of the i-th team. No two teams will have the same coordinates.
 
Output
For each test case:
One line with the number of pairs of teams that sit too close to each other.

题目大意:给平面上n个点,问有多少对点间的距离小于d。

思路:一眼可以看到,虽然x轴和y轴的范围很大,但是d却很小,这应该是一个突破口。

那么这个d怎么利用呢。思考一下,对于任意一个点P,和它的距离小于d的点,至少应该在一个以P为中心的2d*2d的正方形中。

因为要题目要求的是对数,那么我们可以只考虑P点的右半部分的点(因为左边部分的点与P的组合在计算左边的点的时候已经算了)。

那么,我们就是要考虑,对于点P(x0, y0),只考虑x = x0、x0+1、……、x0+d-1的点。

穷举这些x = x0 + i,那么y轴的范围就应该在[y0 - t, y0 + t]之间,其中 t 是满足i^2 + t^2 < d^2的最大整数(注意题目都是整点)。

如果对于每一个x = x0 + i的点我们都可以收集起来,按y轴排好,那么二分查找,可以得到 结果 = 小于等于y0 + t的数目 - 小于y0 - t的数目。

因为x的范围很大,不可能说每个点开个数组,我们可以用C++的map存起来,每个有可能的x开一个vector,然后排序即可(注意找map中是否存在x的时候最好用map.find(x),直接用map[x]会创建一个x留在map中,可能导致效率的减缓)。

如果不用map也不是不可以,只要把点都从小到大(x轴为第一关键字,y轴第二关键字)排好序,每次需要哪个x的时候二分查找也是可以的。

我们好像还没算x轴相同的呢。对于x轴相同的,弄俩指针,从前往后扫就可以统计出来了,这个还是不难的。

细节可以看代码,时间复杂度为O(n*d*log(n))。

代码(14109MS):

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long LL; const int MAXN = ; int T, n, d, x, y;
map<int, vector<int> > mymap; LL solve() {
LL ans = ;
for(map<int, vector<int> >::iterator it = mymap.begin(); it != mymap.end(); ++it) {
sort(it->second.begin(), it->second.end());
for(int i = , j = , n = it->second.size(); i < n; ++i) {
while(j < n && it->second[i] + d > it->second[j]) ++j;
ans += j - i - ;
}
}
int d2 = d * d;
for(map<int, vector<int> >::iterator it = mymap.begin(); it != mymap.end(); ++it) {
for(vector<int>::iterator p = it->second.begin(); p != it->second.end(); ++p) {
for(int i = , t = d; i < d; ++i) {
while(i * i + t * t >= d2) --t;
map<int, vector<int> >::iterator nx = mymap.find(it->first + i);
if(nx != mymap.end()) {
ans += (upper_bound(nx->second.begin(), nx->second.end(), *p + t) - nx->second.begin()) -
(lower_bound(nx->second.begin(), nx->second.end(), *p - t) - nx->second.begin());
}
}
}
}
return ans;
} int main() {
scanf("%d", &T);
while(T--) {
mymap.clear();
scanf("%d%d", &n, &d);
for(int i = ; i < n; ++i) {
scanf("%d%d", &x, &y);
mymap[x].push_back(y);
}
printf("%I64d\n", solve());
}
}

HDU 2366 Space(二分计数)的更多相关文章

  1. hdu 2962 Trucking (二分+最短路Spfa)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others ...

  2. UVA 10816 + HDU 1839 Dijstra + 二分 (待研究)

    UVA 题意:两个绿洲之间是沙漠,沙漠的温度不同,告诉起点,终点,求使得从起点到终点的最高温度最小的路径,如果有多条,输出长度最短的路径: 思路:用最小费用(最短路径)最大流(最小温度)也能搞吧,但因 ...

  3. hdu 2413(最大匹配+二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2413 思路:由于要求最少的时间,可以考虑二分,然后就是满足在limit时间下,如果地球战舰数目比外星战 ...

  4. HDU 5884 Sort (二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 nn个有序序列的归并排序.每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的 ...

  5. hdu 1281棋盘游戏(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281   Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘, ...

  6. HDU 1025 DP + 二分

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1025 求最长递增子序列,O(n^2)的复杂度超时,需要优化为O(n*logn) f[i]存储长度为i的最小 ...

  7. hdu 2289 要二分的杯子

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2289 大意是 一个Cup,圆台形,给你它的顶部圆的半径,底部圆的半径,杯子的高度,和此时里面装的水的体 ...

  8. HDU 1025 LIS二分优化

    题目链接: acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Time Limit: ...

  9. HDU 5200 Trees 二分

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5200 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

随机推荐

  1. MySQL- 锁(2)

    InnoDB行锁实现方式 InnoDB行锁是通过给索引上的索引项加锁来实现的,这一点MySQL与Oracle不同,后者是通过在数据块中对相应数据行加锁来实现的.InnoDB这种行锁实现特点意味着:只有 ...

  2. Qt 自定义 滚动条 样式(模仿QQ)

    今天是时候把软件中的进度条给美化美化了,最初的想法就是仿照QQ. 先前的进度条是这样,默认的总是很难受欢迎的:美化之后的是这样,怎么样?稍微好看一点点了吧,最后告诉你实现这个简单的效果在Qt只需要加几 ...

  3. mssql全文索引

    在使用全文索引的时候例如: SELECT [PRID] ,[PRCode] ,[PRDesc] FROM [test1].[dbo].[PerformanceIssue] where contains ...

  4. Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)

    A - Coins Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Descript ...

  5. Round and Round We Go

    http://acm.hdu.edu.cn/showproblem.php?pid=1313 考查大整数与小整数相乘 #include<iostream> #include<cstd ...

  6. SQLSERER 中select排序问题

    SELECT  * FROM 表名 ORDER BY PageNO DESC 这种排序会排出这种效果:1, 11,2,20 select             *,              RIG ...

  7. Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. An example is the root-to-leaf path1->2->3which represents the number123. Find the total sum of a

    class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public class ...

  8. 玩转HTML5移动页面

    (1) 动画雪碧图涉及的动画十分多,用的元素也十分多,请务必使用雪碧图(Sprite)!网上的工具有一些可以帮助你生成雪碧图的工具,例如CssGaga,GoPng等等,自动化构建工具Grunt和Gul ...

  9. POJ 1528问题描述

    Description From the article Number Theory in the 1994 Microsoft Encarta: ``If a, b, c are integers ...

  10. 脚本编程中的test、bash调试、变量计算、参数

    脚本编程中的test.bash调试.变量计算.参数 1.文件测试 -e FILE:测试文件是否存在 -f FILE:测试文件是否为普通文件 -d FILE:测试路径是否为目录 -r FILE:测试当前 ...