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 ...
随机推荐
- iOS开发之Objective-C与JavaScript的交互
UIWebView是iOS最常用的SDK之一,它有一个stringByEvaluatingJavaScriptFromString方法可以将javascript嵌入页面中,通过这个方法我们可以在iOS ...
- python3.* socket例子
On Server: # -*- coding: utf-8 -*-#this is the server import socketif "__main__" == __name ...
- Flask学习 一 基本结构
-from flask import Flask +from flask import Flask,render_template -from flask import request -from f ...
- Gson解析Json数组
需求:从steam官网获取英雄数据,即为Json数据,并导入到本地数据库 Json数据是这样的 { "result": { "heroes": [ { &quo ...
- STM32F4系列单片机上使用CUBE配置MBEDTLS实现pem格式公钥导入
|版权声明:本文为博主原创文章,未经博主允许不得转载. 最近尝试在STM32F4下用MBEDTLS实现了公钥导入(我使用的是ECC加密),整个过程使用起来比较简单. 首先,STM32F4系列CUBE里 ...
- 解决IE8下CSS3选择器 :nth-child() 不兼容的问题
1.定义和用法 :nth-child(n) 选择器匹配属于其父元素的第 N 个子元素,不论元素的类型. n 可以是数字.关键词或公式. <ul> <li>1</li> ...
- emqtt 试用(八)ssl认证 - 代码验证
参考链接:http://emqtt.com/clients#java http://docs.emqtt.cn/zh_CN/latest/config.html#mqtt-ssl-8883 一.单向认 ...
- istio入门(05)istio的架构概念2
- api-gateway实践(03)新服务网关 - 网关请求拦截检查
参考链接:http://www.cnblogs.com/jivi/archive/2013/03/10/2952829.html 一.为什么要拦截检查请求? 防止重放攻击.篡改重放,进行使用规格检查 ...
- Spring Security 入门(3-11)Spring Security 的使用-自定义登录验证和回调地址
配置文件 security-ns.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...