codeforces 650 C. 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.
思路:(|i.x-j.x|+|i.y-j.y|)^2=(i.x-j.x)^2+(i.y-j.y)^2+2*|i.x-j.x|*|i.y-j.y|=(i.x-j.x)^2+(i.y-j.y)^2;
所以满足条件i.x==j.x或i.y==j.y至少一个;两次分别对x和y排序找到相同的有多少,最后再减去x和y同时相同的就是结果了,因为当时没用long long 导致最后测数据的时候wa了,大哭一场啊!!!
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+6;
struct node
{
int x,y;
};
node a[N];
int cmp1(node u,node v)
{
return u.x<v.x;
}
int cmp2(node u,node v)
{
if(u.y==v.y)return u.x<v.x;
return u.y<v.y;
}
/*
int cmp3(node u,node v)
{
if(u.x==v.x)
{
return u.y<v.y;
}
return u.x<v.x;
}*/
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a,a+n,cmp1);
long long num=1,ans=0;
for(int i=1;i<n;i++)
{
if(a[i].x==a[i-1].x)
{
num++;
}
else
{
long long s=(num-1)*num/2;
ans+=s;
num=1;
}
}
long long s=(num-1)*num/2;
ans+=s;
sort(a,a+n,cmp2);
num=1;
for(int i=1;i<n;i++)
{
if(a[i].y==a[i-1].y)
{
num++;
}
else
{
long long s=(num-1)*num/2;
ans+=s;
num=1;
}
}
s=num*(num-1)/2;
ans+=s;
num=1;
for(int i=1;i<n;i++)
{
if(a[i].x==a[i-1].x&&a[i].y==a[i-1].y)
{
num++;
}
else
{
long long s=num*(num-1)/2;
ans-=s;
num=1;
}
}
s=num*(num-1)/2;
ans-=s; cout<<ans<<"\n"; return 0;
}
codeforces 650 C. Watchmen(数学公式)的更多相关文章
- Codeforces 650 D. Zip-line
$ >Codeforces \space 650 D. Zip-line<$ 题目大意 : 有一个长度为 \(n\) 的序列 \(h\) ,\(m\) 次询问,每一次询问求如果把序列中第 ...
- CodeForces 651 C Watchmen
C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...
- 【CodeForces - 651C 】Watchmen(map)
Watchmen 直接上中文 Descriptions: 钟表匠们的好基友马医生和蛋蛋现在要执行拯救表匠们的任务.在平面内一共有n个表匠,第i个表匠的位置为(xi, yi). 他们需要安排一个任务计划 ...
- 【Codeforces 650 D】Zip-line
题意:给一个序列以及\(n\)个查询,每一个查询是问(假装)把第\(a_i\)个数改为\(b_i\)之后原序列的最长上升子序列的长度. 思路:线段树优化\(dp\). 肯定离线做啊. 首先我们考虑\( ...
- CodeForces 630Q Pyramids(数学公式)
IT City administration has no rest because of the fame of the Pyramids in Egypt. There is a project ...
- 【Codeforces Round 650】Codeforces #334 (Div. 1)
模拟CF650,ABC三题,RK90 Codeforces 650 A 思路:首先看式子 \(\sqrt{(x_i-x_j)^2+(y_i-y_j)^2}=|x_i-x_j|+|y_i-y_j|\) ...
- Codeforces Round #345 (Div. 1) A - Watchmen 容斥
C. Watchmen 题目连接: http://www.codeforces.com/contest/651/problem/C Description Watchmen are in a dang ...
- Watchmen CodeForces - 650A
Watchmen CodeForces - 650A Watchmen are in a danger and Doctor Manhattan together with his friend Da ...
- (水题)Codeforces - 650A - Watchmen
http://codeforces.com/contest/650/problem/A 一开始想了很久都没有考虑到重复点的影响,解欧拉距离和曼哈顿距离相等可以得到 $x_i=x_j$ 或 $y_i=y ...
随机推荐
- ListView中加载大量的图片
情况是这样的:我需要把大约四五十个车标在一个listView中展示出来,一般在用ListView的时候撑死十来个图标,按不同分类使用,这倒好办,在创建view的时候使用R.drawable.xxx指定 ...
- 多媒体开发之rtp 打包发流---udp 丢包问题
http://blog.csdn.net/acs713/article/details/19339707
- Android蓝牙通信具体解释
蓝牙通信的大概过程例如以下: 1.首先开启蓝牙 2,搜索可用设备 3,创建蓝牙socket.获取输入输出流 4,读取和写入数据 5.断开连接关闭蓝牙 还要发送配对码发送进行推断! 以下是全部的源码:不 ...
- Java引用类型作为形参和返回值
一.什么是引用类型 在Java中引用类型包括三种:类.抽象类.接口. 二.引用类型作为形参使用 1.类作为形参 /** * 类作为形参,实际传递的是该类的对象 */ class Student { p ...
- Linux - SVN的基本操作
SVN的基本操作 本文地址: http://blog.csdn.net/caroline_wendy $ svn diff //显示改动 $ post-review --summary="b ...
- 大数据学习系列(5)-- 局域网yum仓库搭建
https://www.cnblogs.com/nulige/p/6081192.html
- ASP向上取整
<%Function Ceil(value) Dim return return = int(value) Cei2=value-return if Cei2>0 ...
- isinstance/issubclass/type的区别?
type() 判断某个对象是否是该类创建的,只看一层,如果是继承类,也不会考虑继承类的类型.. Issubclass() 判断该类是否是另一个类的派生类,也就是子类,参数为类. isinstance( ...
- linux 10 -Bash Shell编程
二十三. Bash Shell编程: 1. 读取用户变量: read命令是用于从终端或者文件中读取输入的内建命令,read命令读取整行输入,每行末尾的换行符不被读入.在read命令后 ...
- R语言数据管理(三):数据读入
R的数据读入非常灵活,即可以在R软件中直接输入,也可以读入外部数据. 一.直接输入数据 ①c()函数 ②scan()函数 其功能类似c()函数,scan()实际上是一种键盘输入数据函数.当输入scan ...