C - Watchmen
题目链接:https://vjudge.net/contest/237394#problem/C
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.
Input
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.
Output
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.
Examples
3
1 1
7 5
1 5
2
6
0 0
0 1
0 2
-1 1
0 1
1 1
11
Note
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.
题目大意:输入n,代表有n个点,下面n行代表每个点的坐标,求用以上两种方式求出答案一样的点有多少个
个人思路:第一想法是直接暴力,(这时候还不太清楚怎么大概判断是否会超时),然后很正常的超时了,这里介绍一下怎么大概估计一下是否会超时
1000ms大概能判断10^7的数据,而且是在数据比较水,条件比较好的情况下,反正在这时候就已经很极限了,可能有时候能判断10^8的数据,这就要求后台数据特别水,而且编译环境好的情况下才有可能ac,而这题时限是3000ms,所以充其量估计也就能判3*10^8的数据,所以直接做肯定是超时的,那么下面看一下用
map来做,嗯。。。很快
还有,两个点要满足条件,必须要x相同或者y相同,但要记住减去重复的点
看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
#include<map>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=2e5+;
const ll maxa=;
const double x=(1.0+sqrt(5.0))/;
#define INF 0x3f3f3f3f3f3f
//n*(n-1)/2等价于0+1+2+···+n-1
map<ll,ll>mp1;//用于计算有多少个x相同的点
map<ll,ll>mp2;//用于计算有多少个y相同的点
map<pair<ll,ll>,ll>mp;//用于计算有多少个x和y都相同的点
int main()
{
mp1.clear();
mp2.clear();
mp.clear();//清空
int n;
ll a,b,ans=,num=;
cin>>n;
for(int i=;i<n;i++)
{
cin>>a>>b;
num+=mp[make_pair(a,b)];//发现map有个好处,就是关键字是什么都可以,这就大大方便了存储
mp[make_pair(a,b)]++;
ans+=mp1[a];
mp1[a]++;
ans+=mp2[b];
mp2[b]++;
}
cout<<ans-num<<endl;
return ;
}
C - Watchmen的更多相关文章
- Codeforces Round #345 (Div. 1) A. Watchmen
A. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...
- Codeforces 650A Watchmen
传送门 time limit per test 3 seconds memory limit per test 256 megabytes input standard input output st ...
- CodeForces 651C Watchmen map
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn t ...
- Codeforces Round #345 (Div. 1) A. Watchmen 模拟加点
Watchmen 题意:有n (1 ≤ n ≤ 200 000) 个点,问有多少个点的开平方距离与横纵坐标的绝对值之差的和相等: 即 = |xi - xj| + |yi - yj|.(|xi|, |y ...
- codeforces 651C Watchmen
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn t ...
- CodeForces 651 C Watchmen
C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #345 (Div. 1) A - Watchmen 容斥
C. Watchmen 题目连接: http://www.codeforces.com/contest/651/problem/C Description Watchmen are in a dang ...
- codeforces 650 C. Watchmen(数学公式)
C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...
- Watchmen CodeForces - 650A
Watchmen CodeForces - 650A Watchmen are in a danger and Doctor Manhattan together with his friend Da ...
随机推荐
- bzoj 2406 矩阵 —— 有源汇上下界可行流
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2406 这题,首先把题目那个式子的绝对值拆成两个限制,就成了网络流的上下界: 有上下界可行流原 ...
- 用Fiddler2来监听HTTP(记:用skydrive sdk访问时,出错后用Fidder抓包分析)
最近在写一个关于如何上传文件到skydrive的demo, 用REST上传失败. 安装Telerik的Fiddler后, 可以监听http或者https通信, 然后可以在软件中看到返回的json数据或 ...
- 如何使用ODB(How to use odb On windows)
1.下载ODB library:ODB Compiler,Common Runtime Library,Database Runtime Library. http://www.codesynthes ...
- Python:生成器函数
生成器函数:包含yield语句的函数: 生成器对象:生成器对象和迭代器对象行为相似,都支持可迭代接口:__next__(),若想执行生成器函数内部语句,则需要迭代协议’ A.生成器函数被调用时,并不会 ...
- Ruby中print、p、puts的区别
三个方法的作用都是将一个字符串打印到控制台 比较项目 puts print p 换行符 末尾添加换行符 末尾不加换行符 末尾添加换行符 非字符串对象的输出 调用该对象的to_s方法 ...
- 第三天的 No session 问题
1.1 No session(理解) 初始化快递员对象中 定区集合 Web层转Courier对象为json串时候,对象中有fixedareas集合属性,jpa集合属性加载策略延迟加载.在action中 ...
- 获取剪切板上DataFormats.Dib格式的文件
if (formats.Contains(System.Windows.Forms.DataFormats.Dib)) { using (var img = System.Windows.Forms. ...
- 19.Imagetragick 命令执行漏洞(CVE-2016–3714)
Imagetragick 命令执行漏洞(CVE-2016–3714) 漏洞简介: Imagetragick 命令执行漏洞在16年爆出来以后,wooyun上面也爆出了数个被该漏洞影响的大厂商,像腾讯, ...
- 解决Torch.load()错误信息: UnicodeDecodeError: 'ascii' codec can't decode byte 0x8d in position 0: ordinal not in range(128)
使用PyTorch跑pretrained预训练模型的时候,发现在加载数据的时候会报错,具体错误信息如下: File "main.py", line 238, in main_wor ...
- Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings (容斥原理)
题目链接 Description Yaroslav thinks that two strings s and w, consisting of digits and having length n ...