A. Watchmen(Codeforces 650A)
A. Watchmen
3 seconds
256 megabytes
standard input
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.
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.
思路:Manhattan and Daniel will calculate the same distances曼哈顿距离和两点间的距离相等,A(x1,y1) ,B(X2,Y2);
|x1-x2|+|y1-y2|=sqrt((x1-x2)^2+(y1-y2)^2);
画个三角形你可以发现曼哈顿距离就是两直角边的和,而两点的距离为斜边
那么当且x1==x2||y1==y2;这两种距离相等
那么只要找x1==x2||y1==y2,开个数组记录各个x的种类及其个数,y的种类和个数,c[x],b[y],由于x和y的数值比较大所以离散化下可以。
sum+=c[x]*(c[x]-1)/2(c[x]〉=1);sum+=c[y]*(c[y]-1)/2;最如果点相同这样算就相当重复算了一遍,所以只要再找出那些相同的点,以及种类个数
R[i];ans+=(R[i]*(R[i]-1)/2;最后答案就是sum-ans;
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<string.h>
6 #include<map>
7 #include<queue>
8 #include<math.h>
9 #include<set>
10 #include<stack>
11 #include<map>
12 int cmp(const void*p,const void*q);
13 using namespace std;
14 typedef long long LL;
15 typedef struct pp
16 {
17 LL x;
18 LL y;
19 } ss;
20 ss aa[200005*4];LL bb[200005*4];
21 ss ak[200005*4];ss bk[200005*4];
22 LL cc[4*200005];LL yy[200005*4];
23 LL vv[200005*4];
24 typedef pair<int,int> P;
25 int main(void)
26 {
27 LL i,j,k,p,q;
28 scanf("%I64d",&k);
29 { LL cnt=0;LL x,y;memset(vv,0,sizeof(vv));
30 map<P,LL>my;memset(yy,0,sizeof(yy));my.clear();
31 for(i=0;i<k;i++)
32 {
33 scanf("%I64d %I64d",&x,&y);
34 bb[cnt++]=x;
35 bb[cnt++]=y;
36 }
37 for(i=0;i<cnt;i++)
38 {
39 aa[i].x=bb[i];
40 aa[i].y=i;
41 }
42 qsort(aa,cnt,sizeof(ss),cmp);
43 cc[0]=0;LL ans=0;
44 for(i=1;i<cnt;i++)
45 {
46 if(aa[i].x!=aa[i-1].x)
47 ans++;
48 cc[aa[i].y]=ans;
49 }LL uu=0;
50 for(i=0;i<cnt;i++)
51 {
52 if(i%2==0)
53 {
54 ak[uu].x=cc[i];
55 }
56 else {ak[uu].y=cc[i];uu++;}
57 }LL rns=0;LL bbq=1;
58 for(i=0;i<uu;i++)
59 {P l;l.first=ak[i].x;l.second=ak[i].y;
60 if(my[l]==0)
61 {
62 my[l]=bbq++;
63 }
64 }for(i=0;i<uu;i++)
65 {P l;l.first=ak[i].x;l.second=ak[i].y;
66 vv[my[l]]++;
67 }
68 LL an=0;
69 for(i=1;i<bbq;i++)
70 {
71 if(vv[i]>=2)
72 {
73 an+=vv[i]*(vv[i]-1)/2;
74 }
75 }
76 for(i=0;i<uu;i++)
77 {
78 yy[ak[i].x]++;
79 }LL sum=0;
80 for(i=0;i<500005;i++)
81 {
82 if(yy[i]>=2)
83 {
84 sum+=yy[i]*(yy[i]-1)/2;
85 }
86 }memset(yy,0,sizeof(yy));
87 for(i=0;i<uu;i++)
88 {
89 yy[ak[i].y]++;
90 }
91 for(i=0;i<500005;i++)
92 {
93 if(yy[i]>=2)
94 {
95 sum+=yy[i]*(yy[i]-1)/2;
96 }
97 }
98 printf("%I64d\n",sum-an);
99 }
100 return 0;
101 }
102
103 int cmp(const void*p,const void*q)
104 {
105 ss*nn=(ss*)p;
106 ss*mm=(ss*)q;
107 return nn->x-mm->x;
108 }
A. Watchmen(Codeforces 650A)的更多相关文章
- Watchmen CodeForces - 650A
Watchmen CodeForces - 650A Watchmen are in a danger and Doctor Manhattan together with his friend Da ...
- Codeforces 650A Watchmen
传送门 time limit per test 3 seconds memory limit per test 256 megabytes input standard input output st ...
- (水题)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 ...
- [刷题codeforces]650A.637A
650A Watchmen 637A Voting for Photos 点击查看原题 650A又是一个排序去重的问题,一定要注意数据范围用long long ,而且在写计算组合函数的时候注意也要用l ...
- Codeforces Round #345 (Div. 1) A - Watchmen 容斥
C. Watchmen 题目连接: http://www.codeforces.com/contest/651/problem/C Description Watchmen are in a dang ...
- 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 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 ...
随机推荐
- 《手把手教你》系列技巧篇(四十六)-java+ selenium自动化测试-web页面定位toast-下篇(详解教程)
1.简介 终于经过宏哥的不懈努力,偶然发现了一个toast的web页面,所以直接就用这个页面来夯实一下,上一篇学过的知识-处理toast元素. 2.安居客 事先声明啊,宏哥没有收他们的广告费啊,纯粹是 ...
- kubectl logs查看日志时出现failed to create fsnotify watcher: too many open files
因为系统默认的 fs.inotify.max_user_instances=128 太小,在查看日志的pod所在节点重新设置此值: 临时设置 sudo sysctl fs.inotify.max_us ...
- c/c++在线编译Output Limit Exceeded(OLE)错误
提示输出错误,有如下两个可能情况: 1. 不符合题目给出的输出格式,自己输出了多余的内容或者格式不正确 2. 输入数据的时候,未考虑到输入错误的情况 针对2,有如下的例子: 错误的情况: 1 int ...
- 源码分析-NameServer
架构设计 消息中间件的设计思路一般是基于主题订阅发布的机制,消息生产者(Producer)发送某一个主题到消息服务器,消息服务器负责将消息持久化存储,消息消费者(Consumer)订阅该兴趣的主题,消 ...
- 关于C语言中不同类型数据进行计算 有符号和无符号数进行计算
float是8个有效位, 做个试验: 输出如下: 上面说明了什么: 1, 18/2.2 是除不尽的, 因为是define,所以没有给ratio变量赋值类型,但是从sizeof输出的结果是8,所以系统默 ...
- 哪里可以下载支付宝demo或者sdk
http://club.alipay.com/read-htm-tid-9976972.html 这里有所有的demo和sdk包括移动产品的demo.在他的论坛里面呢 真心恶心啊.不放到主页.
- Linux学习 - 分区与文件系统
一.分区类型 1 主分区:总共最多只能分四个 2 扩展分区:只能有一个(主分区中的一个分区),不能存储数据和格式化,必须再划分成逻辑分区 才 ...
- 增大Oracle Virtualbox的磁盘空间
https://blog.csdn.net/hiyachen/article/details/102131823 背景 在virtualbox中装好Linux以及Application之后,发现硬盘空 ...
- ArrayList删除特定元素的方法
最朴实的方法,使用下标的方式: ArrayList<String> al = new ArrayList<String>(); al.add("a"); a ...
- Tomcat(1):安装Tomcat
一,安装Tomcat服务器 1,下载tomcat网址: http://tomcat.apache.org/ 2,找到Download 3,下载 4:下载完成后,解压到任意目录 5:解压完成后得到目录 ...