codeforces 651C Watchmen
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi).
They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan considers the distance between watchmen i and j to be |xi - xj| + |yi - yj|. Daniel, as an ordinary person, calculates the distance using the formula .
The success of the operation relies on the number of pairs (i, j) (1 ≤ i < j ≤ n), such that the distance between watchman i and watchmen j calculated by Doctor Manhattan is equal to the distance between them calculated by Daniel. You were asked to compute the number of such pairs.
题目大意:发现有一些点的欧氏距离和曼哈顿距离是相等的,他认为这个现象特别有趣。为了发现一些规律和性质,他给出了 n 个点,想知道这些点中有多少对点的欧氏距离与曼哈顿距离相等
The first line of the input contains the single integer n (1 ≤ n ≤ 200 000) — the number of watchmen.
Each of the following n lines contains two integers xi and yi (|xi|, |yi| ≤ 109).
Some positions may coincide.
Print the number of pairs of watchmen such that the distance between them calculated by Doctor Manhattan is equal to the distance calculated by Daniel.
3
1 1
7 5
1 5
2
6
0 0
0 1
0 2
-1 1
0 1
1 1
11
In the first sample, the distance between watchman 1 and watchman 2 is equal to |1 - 7| + |1 - 5| = 10 for Doctor Manhattan and for Daniel. For pairs (1, 1), (1, 5) and (7, 5), (1, 5) Doctor Manhattan and Daniel will calculate the same distances.
比较容易发现,两个点 A 和 B 能够贡献答案当且仅当 A 和 B 有至少一维坐标相等。于是
我们离散化以后对于每个相同的 x 和 y 坐标统计一下答案就好了。
注意可能有坐标相同的点,这些点对会在 x 坐标和 y 坐标上都贡献一次答案,所以要去重。
时间复杂度 O(n log n) 。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
struct Pair
{
int x,y;
bool operator <(const Pair &z)
const
{
return (x<z.x||(x==z.x&&y<z.y));
}
};
map<Pair,int>Map;
int X[],Y[],a[],x[],y[],n,sz;
long long ans;
int main()
{int i;
cin>>n;
for (i=;i<=n;i++)
{
scanf("%d%d",&x[i],&y[i]);
a[i]=x[i];a[n+i]=y[i];
}
sz=unique(a+,a+*n+)-(a+);
sort(a+,a+sz+);
for (i=;i<=n;i++)
{
x[i]=lower_bound(a+,a+sz+,x[i])-a;
y[i]=lower_bound(a+,a+sz+,y[i])-a;
}
for (i=;i<=n;i++)
{
if (Map.count((Pair){x[i],y[i]})) ans-=Map[(Pair){x[i],y[i]}];
ans+=X[x[i]]+Y[y[i]];
if (Map.count((Pair){x[i],y[i]}))
Map[(Pair){x[i],y[i]}]++;
else Map[(Pair){x[i],y[i]}]=;
X[x[i]]++;Y[y[i]]++;
}
cout<<ans;
}
codeforces 651C Watchmen的更多相关文章
- CodeForces 651C Watchmen map
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn t ...
- CodeForces - 651C Watchmen (去重)
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn t ...
- Codeforces 651C Watchmen【模拟】
题意: 求欧几里得距离与曼哈顿距离相等的组数. 分析: 化简后得到xi=xj||yi=yj,即为求x相等 + y相等 - x与y均相等. 代码: #include<iostream> #i ...
- 【CodeForces - 651C 】Watchmen(map)
Watchmen 直接上中文 Descriptions: 钟表匠们的好基友马医生和蛋蛋现在要执行拯救表匠们的任务.在平面内一共有n个表匠,第i个表匠的位置为(xi, yi). 他们需要安排一个任务计划 ...
- codeforces 651C(map、去重)
题目链接:http://codeforces.com/contest/651/problem/C 思路:结果就是计算同一横坐标.纵坐标上有多少点,再减去可能重复的数量(用map,pair存一下就OK了 ...
- Codeforces 650A Watchmen
传送门 time limit per test 3 seconds memory limit per test 256 megabytes input standard input output st ...
- (水题)Codeforces - 650A - Watchmen
http://codeforces.com/contest/650/problem/A 一开始想了很久都没有考虑到重复点的影响,解欧拉距离和曼哈顿距离相等可以得到 $x_i=x_j$ 或 $y_i=y ...
- CodeForces 651C
Description Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg s ...
- codeforces Codeforces 650A Watchmen
题意:两点(x1,y1), (x2,y2)的曼哈顿距离=欧几里得距离 也就是:x1=x2或y1=y2,再删除重合点造成的重复计数即可. #include <stdio.h> #includ ...
随机推荐
- 201621123050 《Java程序设计》第11周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序BounceThread 1.1 BallR ...
- 浅谈CPU三级缓存和缓存命中率
CPU: CPU缓存(Cache Memory)是位于CPU与内存之间的临时存储器,它的容量比内存小的多但是交换速度却比内存要快得多.缓存的出现主要是 为了解决CPU运算速度与内存读写速度不匹配的矛盾 ...
- 关于安装wordpress出现500 Internal Server Error的解决方案
安装新版本wordpress出现500 Internal Server Error的问题: 在./wp-includes/class-http.php的268行,改成 $request_order = ...
- django搭建web (五) views.py
http请求: HttpRequest http响应: HttpResponse 所在位置:django.http isinstance(request,HttpResponse) True-> ...
- LOW版统计词频
import string path = 'waldnn' with open(path,'r') as text: words = [raw_word.strip(string.punctuatio ...
- Java 后端微信小程序支付demo (网上说的坑里面基本上都有)
Java 后端微信小程序支付 一.遇到的问题 1. 商户号该产品权限未开通,请前往商户平台>产品中心检查后重试 2.签名错误 3.已经调起微信统一下单接口,可以拿到预支付ID,但是前端支付的时候 ...
- javascript抛物投栏(抛物线实践)
平面内,到定点与定直线的距离相等的点的轨迹叫做抛物线.水平抛物线就是水平匀速,垂直加速的运动. 抛物线的性质:面内与一个定点F和一条定直线l 的距离相等的点的轨迹叫做抛物线. 定点F叫做抛物线的焦点. ...
- velocity学习总结
什么是velocity velocity是一个基于Java的模板引擎,它可以实现彻底的前后端,前端不允许像jsp那样出现Java代码,而是利用context容器传递变量,在java代码里面我们可以往容 ...
- php类中的$this,static,const,self这几个关键字使用方法
本篇文章主要分享一下关于php类中的$this,static,final,const,self这几个关键字使用方法 $this $this表示当前实例,在类的内部方法访问未声明为const及stati ...
- api-gateway实践(14)前端签名密钥和后端签名密钥
1.前端签名密钥 1.1.缓存管理初始:engine初始的时候,从redis拉取全部前端绑定关系到engine缓存.新增/绑定:绑定签名密钥和服务实例,同时缓存该关系到redis,同时缓存到engin ...