CodeForces 651 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
两种距离相等的情况就是两个点的横坐标相等或者纵坐标相等。这里分别排序一下,然后注意要减去两个点相同的情况
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
#define MAX 200000
struct Node
{
long long int x;long long int y; }a[MAX+5];
long long int ans[MAX+5];
int cmp(Node a,Node b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
int cmp2(Node a,Node b)
{
if(a.y==b.y)
return a.x<b.x;
return a.y<b.y;
}
int n;
long long int ans2[MAX+5];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
sort(a+1,a+n+1,cmp);
memset(ans,0,sizeof(ans));
int cnt=0;
int cot=0;
ans[0]=1;
ans2[0]=1;
long long int num=1;
for(int i=2;i<=n;i++)
{
//cout<<a[i].x<<" "<<a[i-1].x<<endl;
if(a[i].x==a[i-1].x)
{
if(a[i].y==a[i-1].y)
ans2[cot]++;
else
{
cot++;
ans2[cot]=1;
}
ans[cnt]++;
}
else
{
cot++;
ans2[cot]=1;
cnt++;
ans[cnt]=1;
}
}
long long int res=0;
long long int res2=0;
for(int i=0;i<=cnt;i++)
{
res+=(ans[i]*(ans[i]-1))/2; }
for(int i=0;i<=cot;i++)
{
res2+=(ans2[i]*(ans2[i]-1))/2; } sort(a+1,a+n+1,cmp2);
memset(ans,0,sizeof(ans));
cnt=0;
ans[0]=1;
for(int i=2;i<=n;i++)
{
if(a[i].y==a[i-1].y)
{
ans[cnt]++;
}
else
{
cnt++;
ans[cnt]=1;
}
}
for(int i=0;i<=cnt;i++)
res+=(ans[i]*(ans[i]-1))/2;
printf("%lld\n",res-res2);
return 0;
}
CodeForces 651 C Watchmen的更多相关文章
- Codeforces 651 C. Watchmen-曼哈顿距离和欧几里得距离
C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- codeforces 650 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 651 A Joysticks
A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Codeforces 651 B. Beautiful Paintings
B. Beautiful Paintings time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 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 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 650A Watchmen
传送门 time limit per test 3 seconds memory limit per test 256 megabytes input standard input output st ...
随机推荐
- 【转】WCF服务的创建和发布到IIS
一. WCF服务的创建 有两种创建方式: 1.WCF服务库 2.WCF服务应用程序 如下图所示: 这里选择WCF服务库.注意事项: 1.WCF服务库是一个类库项目,这里选择.net 3.5版本(版本高 ...
- 高速入门:十分钟学会Python
初试牛刀 如果你希望学习Python这门语言.却苦于找不到一个简短而全面的新手教程.那么本教程将花费十分钟的时间带你走入Python的大门.本文的内容介于教程(Toturial)和速查手冊(Cheat ...
- CleanMyMac 4破解版-最强中文版_破解版_激活码_注册码
最新版CleanMyMac 4中文版本已经发布了,也受到了广大用户的喜爱.众所周知, 注册码是开启软件的钥匙,在获取软件安装包之后需要有效的注册码才能激活软件.但是关于CleanMyMac 4注册码的 ...
- preventDefault
e.preventDefault()阻止事件默认行为 例如: $("a").click(function (e) { alert("默认行为被禁止喽"); ...
- Spring-MVC案例:Spitter的笔记
源码地址:https://github.com/Young4Dream/yan/tree/master/Maven_spittr 笔记: 1.当DispatcherServlet启动时,会创建Spri ...
- DeDeCMS织梦的采集教程
http://www.tuicool.com/articles/VziaEz dede 第一步.我们打开织梦后台点击采集——采集节点管理——增加新节点 第二步.新增节点-配置网址索引 填写要采集 ...
- row_number()over函数的使用(转)
(转)http://hi.baidu.com/122439049/blog/item/0c9c48131b2734d5f7039e13.html row_number() OVER (PARTITIO ...
- php-fpm打开错误日志的配置
nginx与apache不一样,在apache中可以直接指定php的错误日志,那样在php执行中的错误信息就直接输入到php的错误日志中,可以方便查询. 在nginx中事情就变成了这样:nginx只对 ...
- java日志之slf4j与logback简单使用
最近在开发遇到日志是使用slf4j与logback.xml的配置,所以就记录下来了. 1.导入这几个jar包: Logback 分为三个模块:logback-core,logback-classic, ...
- ZooKeeper(七)-- ZK原生API实现分布式锁
一.使用场景 在分布式应用,往往存在多个进程提供同一服务.这些进程有可能在相同的机器上,也有可能分布在不同的机器上. 如果这些进程共享了一些资源,可能就需要分布式锁来锁定对这些资源的访问. 二.实现分 ...