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 ...
随机推荐
- Leetcode 1——twosum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- alpha冲刺第二天
一.合照 二.项目燃尽图 三.项目进展 图形界面基本完成 接口文档框架完成,接下来将会不断细化填充 登录界面向服务器请求数据进行ing 四.明日规划 1.注册登录接口能够完成 2.研究idea实现获得 ...
- Java的HelloWorld程序
Java的HelloWorld程序 1.新建文本文档,编写HelloWorld程序,最后保存时记得保存成.java格式 2.在D盘新建一个HelloJava文件夹用于保存java程序 3.使用WIN+ ...
- Tornado 网站demo 三
模板 修改index.py #!/usr/bin/env Python # coding=utf-8 import tornado.web import methods.readdb as mrd c ...
- 201421123042 《Java程序设计》第9周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1. List中指定元素的删除(题集题目) 1.1 实验总结.并回答:列举至 ...
- Linux之用户与用户组
1.Linux是一种 多用户多任务分时操作系统. 2.Linux的用户只有两个等级:root用户和非root用户. Linux系统默认 内置了root用户 和一些非root用户,如nobody,a ...
- django + nginx + uwsgi + websocket
最近使用django框架做了一个简单的聊天机器人demo, 开发的过程中使用了django自带的websocket模块,当使用django框架自带的wsgi服务去启动的话,没有什么问题.如果要使用uw ...
- AssemblyExecuteAdapter
BizTalk custom adapter AssemblyExecuteAdapter 功能 更为方便的扩展BizTalk custom adapter 的交互方式,只需要实现IAssemblyE ...
- python全栈开发-常用模块的一些应用
一.random模块详解 1.概述 首先我们看到这个单词是随机的意思,他在python中的主要用于一些随机数,或者需要写一些随机数的代码,下面我们就来整理他的一些用法 2.常用方法 1. random ...
- maven入门(9)Maven常用命令
Maven常用命令 清理 clean编译 compile打包 package安装 install跳过测试 clean package -Dmaven.test.skip=true