博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Codeforces] 650A - Watchmen
阅读量:5131 次
发布时间:2019-06-13

本文共 3564 字,大约阅读时间需要 11 分钟。

A. Watchmen
time limit per test 
3 seconds
memory limit per test 256 megabytes
input 
standard input
 
output 
standard output

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
input
3 1 1 7 5 1 5
output
2
input
6 0 0 0 1 0 2 -1 1 0 1 1 1
output
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.

 

题意

给出一个点集,定义一个二元关系成立条件如下:两点距离原点的曼哈顿距离和欧几里得距离相等,求点集中有多少对这种符合关系的点集

 

分析

其实只要两个点符合Xi == Xj或者Yi == Yj就行了(具体为什么画个Rt三角形就行),重点是如何处理同坐标的点。

首先根据各点X排序一下,然后计算同X点的关系数。在这里先把同一个坐标的那些点忽略不计(注意)

之后根据各点Y排序,这次将同坐标的点和同Y点一起加起来。

顺便,CF要求不能用long long要用__int64和%I64d ...

 

代码

1 #include
2 #include
3 #include
4 #include
5 #include
6 #define maxn 200000 7 using namespace std; 8 9 struct Poi{10 __int64 x,y;11 }list[maxn*2];12 13 bool com_x(Poi A,Poi B){14 if(A.x < B.x) return true;15 if(A.x == B.x) return A.y < B.y;16 if(A.x > B.x) return false;17 }18 19 bool com_y(Poi A,Poi B){20 if(A.y < B.y) return true;21 if(A.y == B.y) return A.x < B.x;22 if(A.y > B.y) return false;23 }24 25 int main(){26 __int64 n,cnt1 = 1,cnt2 = 1,ans = 0;27 scanf("%I64d",&n);28 for(int i = 0;i < n;i++){29 scanf("%I64d%I64d",&list[i].x,&list[i].y);30 }31 32 sort(list,list+n,com_x);33 34 for(int i = 1;i < n;i++){35 if(list[i].x == list[i-1].x){36 cnt1++;37 if(list[i].y == list[i-1].y) cnt2++;38 else{39 ans -= cnt2*(cnt2-1)/2;40 cnt2 = 1;41 }42 }else{43 ans += cnt1*(cnt1-1)/2;44 cnt1 = 1;45 ans -= cnt2*(cnt2-1)/2;46 cnt2 = 1;47 }48 }49 50 ans += cnt1*(cnt1-1)/2-cnt2*(cnt2-1)/2;51 52 cnt1 = 1;53 sort(list,list+n,com_y);54 55 for(int i = 1;i < n;i++){56 if(list[i].y == list[i-1].y) cnt1++;57 else{58 ans += cnt1*(cnt1-1)/2;59 cnt1 = 1;60 }61 }62 63 ans += cnt1*(cnt1-1)/2;64 65 printf("%I64d",ans);66 67 return 0;68 }
Sure to read??

 

理论基础

算法设计

转载于:https://www.cnblogs.com/Chorolop/p/7148107.html

你可能感兴趣的文章
SignalR简介
查看>>
gbk和gb2312的区别
查看>>
Android工程方法数超过64k,The number of method references in a .dex file cannot exceed 64K.
查看>>
ftp (文件传输协议)
查看>>
ipsec在企业网中的应用(IKE野蛮模式)(转)
查看>>
Debian使用相关
查看>>
Cocos2d JS 之消灭星星(十一) 本地保存玩家信息
查看>>
Python 字典(Dictionary)
查看>>
Python XML解析
查看>>
.net后台页面跳转
查看>>
04_dubbo_ioc
查看>>
luogu2261 [CQOI2007] 余数之和
查看>>
luogu1600 天天爱跑步
查看>>
JS产生随机数的几个用法!
查看>>
如何使用VC++写一个小程序来检测.NetFrameWork版本
查看>>
2017.2.15
查看>>
ASP.NET Core:WebAppCoreAngular
查看>>
python学习笔记(4)-基本数据类型-数字类型及操作
查看>>
java 操作POI参考文章
查看>>
winform中ComboBox控件的简单使用
查看>>