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 ...
随机推荐
- iOS数据本地持久化
p1:归档.Preference(NSUserDefault).沙盒存储 iOS开发中本地存储主要有三种形式 XML属性列表(plist)归档 Preference(偏好设置) NSKeyedAr ...
- S2--《深入.NET平台和C#编程》
第一章 深入.NET框架 1.1 Microsoft .NET框架概述 .NET框架的优势 * 提供了一个面向对象的编程环境,完全支持面向对象编程,.NET 框架提高了软件的可复用性,可扩展 ...
- 023医疗项目-模块二:药品目录的导入导出-从数据库中查出数据用XSSF导出excel并存放在虚拟目录最后下载(包括调试)
我们要实现的效果: 进入到这个页面后,输入要查询的条件,查询出药品表的数据,然后按下导出按钮 ,就会在服务器的一个目录下生成一个药品表的excel表格. 点击"导出"之后 ...
- GIT 专贴
1.官网 git-scm.com github.com 代码库 2.源码
- 【转】【Thread】ReaderWriterLock 读写锁
ReaderWriterLock类 通常来讲,一个类型的实例对于并行的读操作是线程安全的,但是并行地更新操作则不是(并行地读和更新也不是). 这对于资源也是一样的,比如一个文件.当保护类型的实例安全时 ...
- 设置Eclipse中的tab键为4个空格的完整方法
1.点击 window->preference-,依次选择 General->Editors->Text Editors,选中右侧的 insert space for tabs;如下 ...
- JS 禁用和重新启用a标签的点击事件
function changeHomePageModule(){ var css = $('#collapseExample').attr('class'); if(css=='collapse'){ ...
- Eclipse里面启用genymotion
E:/Users/zhuxuekui/AppData/Local/Android/sdk as里面的SDK目录 1.打开eclipse并从云仓库里面下载genymotion插件 注意:这里面有一个坑, ...
- 从log4j日志无缝迁移至logback
ogback对比log4j的有点在此就不赘述了. 由于在项目的原有代码中,大量的日志生成是通过log4j实现的,新的代码希望通过logback的方式生成日志,同时希望将老的代码在不修改的情况下直接将日 ...
- ASP.Net MVC如何访问的静态页面
MVC开发中,因为View文件夹下的web.config文件默认会把任何方法的请求的任何文件,路径都交给 System.Web.HttpNotFoundHandler 去处理.起到Controller ...