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 ...
随机推荐
- CSMA/CD解释与理解
1. CSMA/CD含义 CSMA/CD即载波监听多点接入/碰撞检测,此协议是使用在总线型网络中的,不同计算机是通过多点接入的方式连接在一起.协议的重点在于监听和碰撞检测. 2. 为什么要监听和碰撞检 ...
- python 线程安全
http://www.cnblogs.com/monsteryang/p/6592385.html
- CentOS 6.4 yum安装LAMP环境
一.制作连外网的yum源文件 1. centOS安装完成时是默认存在的,不需要做任何操作,可以直接使用yum 命令进行操作, 默认是在 /etc/yum.repos.d/目录下的 2. 如果你因为制 ...
- 二、Android应用的界面编程(六)ProgressBar及其子类[SeekBar、RatingBar]er
通常用于向用户显示某个耗时操作完成的百分比.Android支持几种风格的进度条,通过style属性可以为ProgressBar指定风格.该属性支持如下几个属性值. # @android:style/W ...
- jsp联合javascript操作html
1 执行的先后顺序 jsp先处理,给页面里面的变量赋值等等.然后整个页面发送给客户端,在客户端执行javascipt相关的代码. 2 jsp文件的构成 html文件+java程序片段+jsp标签=js ...
- iOS与JS开发交互总结
hybrid.jpg 前言 Web 页面中的 JS 与 iOS Native 如何交互是每个 iOS 猿必须掌握的技能.而说到 Native 与 JS 交互,就不得不提一嘴 Hybrid. Hybri ...
- SVM vs. Softmax
http://cs231n.github.io/linear-classify/
- 洛谷 P3629 [APIO2010]巡逻
题目在这里 这是一个紫题,当然很难. 我们往简单的想,不建立新的道路时,从1号节点出发,把整棵树上的每条边遍历至少一次,再回到1号节点,会恰好经过每条边两次,路线总长度为$2(n-1)$,根据树的深度 ...
- windows下的mysql闪退问题
早上来启动MySQL发现输入密码就闪退,连续试了好几次,最后到网上查到了解决方案. 与Linux系统下MySQL密码丢失的操作步骤基本一样. 首先要跳过密码启动MySQL服务. 启动服务必须使用全路径 ...
- 项目中对模板和js,css文件进行压缩的处理类
我们知道,在html的页面中,所有空格和换行符其实都会占据一定的空间,即使使用了gzip压缩,在传输过程中依然会浪费用户的流量和我们自己服务器的带宽,此脚本就是为了解决这个问题而诞生的. 请自行下载G ...