Codeforces 650A 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.
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.
--------------------------------------------------------------------------------------
Solution
不难看出,
两点(x1,y1), (x2,y2)的曼哈顿距离=欧几里得距离<==> x1=x2或y1=y2
对于所有x与y坐标,统计在对应竖直线与水平线上的点的个数,再删除重合点造成的重复计数即可,当然也要统计重合点的数目。
Implementation
#include <bits/stdc++.h>
using namespace std; typedef long long LL; map<pair<int,int>,LL> m;
map<int,LL> cnt[]; int main(){
ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=; i<n; i++){
int x, y;
cin>>x>>y;
cnt[][x]++; cnt[][y]++;
m[{x,y}]++;
}
LL ans=;
for(int i=; i<; i++)
for(auto it=cnt[i].begin(); it!=cnt[i].end(); it++){
ans+=it->second*(it->second-)/;
}
for(auto it=m.begin(); it!=m.end(); it++)
ans-=it->second*(it->second-)/;
cout<<ans<<'\n';
return ;
}
用range-for还可将for-head写得更简洁些:
#include <bits/stdc++.h>
using namespace std; typedef long long LL; map<pair<int,int>,LL> m;
map<int,LL> cnt[]; int main(){
ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=; i<n; i++){
int x, y;
cin>>x>>y;
cnt[][x]++; cnt[][y]++;
m[{x,y}]++;
}
LL ans=;
for(int i=; i<; i++)
for(auto it:cnt[i])
ans+=it.second*(it.second-)/;
for(auto it:m)
ans-=it.second*(it.second-)/;
cout<<ans<<'\n';
return ;
}
---------------------------------------------------
记录这道题是为了复习STL containers。
基础不牢,地动山摇。
Codeforces 650A Watchmen的更多相关文章
- (水题)Codeforces - 650A - Watchmen
http://codeforces.com/contest/650/problem/A 一开始想了很久都没有考虑到重复点的影响,解欧拉距离和曼哈顿距离相等可以得到 $x_i=x_j$ 或 $y_i=y ...
- codeforces Codeforces 650A Watchmen
题意:两点(x1,y1), (x2,y2)的曼哈顿距离=欧几里得距离 也就是:x1=x2或y1=y2,再删除重合点造成的重复计数即可. #include <stdio.h> #includ ...
- Watchmen CodeForces - 650A
Watchmen CodeForces - 650A Watchmen are in a danger and Doctor Manhattan together with his friend Da ...
- [刷题codeforces]650A.637A
650A Watchmen 637A Voting for Photos 点击查看原题 650A又是一个排序去重的问题,一定要注意数据范围用long long ,而且在写计算组合函数的时候注意也要用l ...
- A. Watchmen(Codeforces 650A)
A. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...
- 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 ...
- codefroces 650A. Watchmen
A. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...
- CodeForces - 651C Watchmen (去重)
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn t ...
随机推荐
- ASP.NET 里的 JSON操作
最近项目中需要用到 JSON操作,google了一下 找到了几个比较好的操作方法.... 一 .使用 mircosoft 提供的 .NET Framework 自带的 json操作方法 1. 使用Ja ...
- Lua windows环境搭建
Lua语言的小巧和功能强大在朋友做的一个项目中得以验证,自己也尝试着了解一下,首先在window系统上搭建一个学习环境. 官网:https://www.lua.org/ 搭建运行环境提供2种方式,源码 ...
- 【转】【WPF】资源读取 URI
一开始看到WPF里面经常用如下语句来构造资源文件Uri: Uri uri = new Uri("/AssemblyName;component/image.png"); 我还以为这 ...
- UltraEdit编辑器使用心得之正则表达式篇
ultraEdit 中通过Ctrl+R 可以快速进行文本替换等处理操作,如果在这中间用一些正则表达式那将帮助NI更高效的进行文字处理操作,相关正则表达式列述如下: % 匹配行首 - 表示搜索字符串必须 ...
- 信息安全系统设计基础实验四:外设驱动程序设计 20135211李行之&20135216刘蔚然
北京电子科技学院(BESTI) 实 验 报 告 封面 课程:信息安全系统设计基础 班级:1352 ...
- 20145208 实验五 Java网络编程
20145208 实验五 Java网络编程 实验内容 1.用书上的TCP代码,实现服务器与客户端. 2.客户端与服务器连接 3.客户端中输入明文,利用DES算法加密,DES的秘钥用RSA公钥密码中服务 ...
- Linux 安装配置Subversion edge
2014-04-14:修正部分描述.添加JAVA_HOME报错处理步骤.添加配置sudoers 系统:CentOS 5.8 ,6.4 Subversion版本:Subversion Edge 4.0. ...
- Html中metra的含义
from: http://dev.csdn.net/article/60/60902.shtm meta是用来在HTML文档中模拟HTTP协议的响应头报文.meta 标签用于网页的<head&g ...
- 小白学习mysql之存储过程的优劣分析以及接入控制
存储过程的优劣 存储过程是一组实现特定功能的SQL语句集合,存储过程一经编译便存储在了服务器上,可以通过调用存储过程的名字以及传入相应的参数来使用存储过程.要高层次的掌握存储过程,不能觉得依葫芦画瓢, ...
- 如何用MAT分析Android应用内存泄露
使用工具:Android Studio 2.0 Preview, Android Device Monitor, MAT(Memory Analyzer). 点击Android Studio工具栏上的 ...