B. Wet Shark and Bishops(思维)
2 seconds
256 megabytes
standard input
standard output
Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to1000. Rows are numbered from top to bottom, while columns are numbered from left to right.
Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.
The first line of the input contains n (1 ≤ n ≤ 200 000) — the number of bishops.
Each of next n lines contains two space separated integers xi and yi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.
Output one integer — the number of pairs of bishops which attack each other.
5 1 1 1 5 3 3 5 1 5 5
6
3 1 1 2 3 3 5
0
In the first sample following pairs of bishops attack each other: (1, 3), (1, 5), (2, 3), (2, 4), (3, 4) and (3, 5). Pairs(1, 2), (1, 4), (2, 5) and (4, 5) do not attack each other because they do not share the same diagonal.
题解:
在同一斜线上的象可以相互攻击,问可以攻击的对数;由于x+y相等的象在同一/上,相减相等的在同一\上,计算相等的数目,对数就是n*(n-1)/2;
由于相减可能为负,加一个整数就可以了;
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%I64d",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%I64d",x)
#define P_ printf(" ")
typedef __int64 LL;
const int MAXN=4040;
int m[MAXN];
int main(){
int n;
while(~SI(n)){
mem(m,0);
int a,b;
for(int i=0;i<n;i++){
SI(a);SI(b);
m[a+b]++;
m[a-b+3020]++;
}
LL ans=0;
for(int i=0;i<MAXN;i++){
if(m[i]){
// printf("%d %d\n",i,m[i]);
ans+=(m[i]-1)*m[i]/2;
}
}
printf("%I64d\n",ans);
}
return 0;
}
B. Wet Shark and Bishops(思维)的更多相关文章
- Codeforces 612B. Wet Shark and Bishops 模拟
		
B. Wet Shark and Bishops time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...
 - Wet Shark and Bishops(思维)
		
Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are nu ...
 - Codeforces Round #341 Div.2 B. Wet Shark and Bishops
		
题意:处在同一对角线上的主教(是这么翻译没错吧= =)会相互攻击 求互相攻击对数 由于有正负对角线 因此用两个数组分别保存每个主教写的 x-y 和 x+y 然后每个数组中扫描重复数字k ans加上kC ...
 - 【CodeForces 621B】Wet Shark and Bishops
		
题 题意 1000*1000的格子里,给你n≤200 000个点的坐标,求有多少对在一个对角线上. 分析 如果求每个点有几个共对角线的点,会超时. 考虑到对角线总共就主对角线1999条+副对角线199 ...
 - codeforce 621B 	 Wet Shark and Bishops
		
对角线 x1+y1=x2+y2 或者x1-y1=x2-y2 #include<iostream> #include<string> #include<algorithm& ...
 - Chocolate&&木块拼接&&Cards&& Wet Shark and Bishops
		
B. Chocolate time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
 - CodeForces 621B Wet Shark and Bishops
		
记录一下每个对角线上有几个,然后就可以算了 #include<cstdio> #include<cstring> #include<cmath> #include& ...
 - Codeforces--621B--Wet Shark and Bishops(数学)
		
 B. Wet Shark and Bishops time limit per test 2 seconds memory limit per test 256 megabytes input ...
 - Wet Shark and Flowers(思维)
		
C. Wet Shark and Flowers time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
 
随机推荐
- Python:staticmethod vs classmethod
			
Being educated under Java background, static method and class method are the same thing. But not so ...
 - Oracle 11g R2安装手册(图文教程)For Windows
			
1.Oracle 11g R2安装手册(图文教程)For Windows 1.下载Oracle 11g R2 for Windows版本,下载地址如下 官方网站: http://download.or ...
 - 项目与软件推荐之编辑器-QOwnNotes(刺激自己)
			
项目与软件推荐之编辑器-QOwnNotes 今天推荐一款软件 QOwnNotes,是一款普通文本笔记软件.以某个路径为目录,罗列出目录下所有的 md 文件或者 txt 文件. 有如下亮点: 启动速度快 ...
 - C#中通过Process启动的外部第三方程序MainWindowHandle句柄为0
			
原文 C#中通过Process启动的外部第三方程序MainWindowHandle句柄为0 前几天遇到了一个在C#的winform程序中,启动一个第三方jar程序,并修改jar运行窗体的标题的问题. ...
 - 50% 的财富 500 强企业使用 Windows Azure
			
在上周的北美TechEd大会上,我有幸见到了来自世界各地的客户.合作伙伴和分析师,其数量之多,让人震惊.没有什么比亲耳聆听使用 Windows Azure 来开创新天地的客户亲口讲述他们的故事更令人振 ...
 - ajax跨域请求的方案
			
$.get("@Hosts.Default.Www/api/XXXXX/Getxxx/"+@Model.UserId, function(data) { $("#tota ...
 - paip.c++ qt 共享库dll的建立
			
paip.c++ qt 共享库dll的建立 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/at ...
 - 全国计算机等级考试二级教程-C语言程序设计_第10章_字符串
			
字符型指针数组 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> //参数中,int a ...
 - wxpython 树形控件全选和取消全选
			
#encoding:utf-8 import wx import wx.lib.agw.customtreectrl as CT class MyFrame(wx.Frame): def __init ...
 - 基于Linux 3.0.8 Samsung FIMC(S5PV210) 的摄像头驱动框架解读(一)
			
作者:咕唧咕唧liukun321 来自:http://blog.csdn.net/liukun321 FIMC这个名字应该是从S5PC1x0開始出现的.在s5pv210里面的定义是摄像头接口.可是它相 ...