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 ...
随机推荐
- Android优化——UI优化(一)优化布局层次
优化布局层次 1.避免布局镶嵌过深(如下) <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi ...
- Swift中的Masonry第三方库——SnapKit
在OC开发时我常用一个名叫Masonry的第三方Autolayout库,在转Swift后发现虽然Swift可以混编OC,但总感觉有些麻烦,在Github上发现了这个叫做SnapKit的第三方库 ...
- Mac 使用SSH远程登录
一.打开ssh Mac Terminal是自带SSH的,可以用whereis来看看: $ whereis ssh 但是在现有进程中找不到ssh对应的进程: $ ps aux | grep ssh ap ...
- R 语言实现牛顿下降法
凸是一个很好的性质.如果已经证明了某个问题是凸的,那这个问题基本上算是解决了. 最近在解决一个多目标优化的问题.多目标的问题往往是非凸的.好在能够知道这个问题的近似解大概是多少.这样这个多目标优化的问 ...
- C#属性: 利用set实现递归
直接帖代码: public class Bird { int xdata; /// <summary> /// 属性的简洁写法,等同于下面的xData方式 /// </summary ...
- 未能加载文件或程序集“XXXXX”或它的某一个依赖项。试图加载格式不正确的程序。
未能加载文件或程序集“FastColoredTextBox, Version=2.10.5.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项.试图加载 ...
- Windows7+VS2012下OpenGL 4的环境配置
系统环境 Windows 7 Ultimate x64,Visual Studio Ultimate 2012 Update 4,和一块支持OpenGL 4.x的显卡. 准备工作 首先用GPU Cap ...
- python中class 的一行式构造器
好处:避免类初始化时大量重复的赋值语句 用到了魔法__dict__ # 一行式构造器 class Test(): # 初始化 def __init__(self, a, b, c=2, d=3, e= ...
- Quartz.net打造信息抽取器
由于最近的一个项目需要定时抽取特定XML信息,然后保存到数据库,最后通过WebApi把手机端要使用的方法给暴露出来,所以去研究了一下Quartz.net.由于项目很小,我没用到Autofac,Repo ...
- 如何使用Native Messaging API 打开window程序
问 如何使用Native Messaging API 打开window程序 cmd javascript terminal chrome Tychio 2013年03月26日提问 关注 1 关注 收藏 ...