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 ...
随机推荐
- 根据群ID和用户Id查询 + string QueryQunByUserIdAndQunId(int userId, int qunId) V1.0
#region 根据群ID和用户Id查询 + string QueryQunByUserIdAndQunId(int userId, int qunId) V1.0 /// <summary ...
- jQuery学习-事件之绑定事件(四)
今天我们来学习jQuery.Event对象.jQuery为了添加自己的处理机制,及可以传递用户自定义数据,于是Event对象就出世了. 1 jQuery.Event = function( src ...
- poj3085
#include <stdio.h> #include <stdlib.h> int main() { ; scanf("%d",&n); whil ...
- wx.button
wx.Button A button is a control that contains a text string, and is one of the most common elements ...
- 抽象工厂模式firstones
与工厂方法模式的区别是工厂子类中会创建出同一类型的不同产品对象.工厂方法模式则工厂子类中只是创建一种具体的产品对象 结构: 产品基类:子类继承的虚函数方法 具体产品子类:实现该产品功能 工厂基类:工厂 ...
- $(window).width() is not a function
昨天在帮同事调试JS的时候,FF报错了,$(window).width() is not a function.我以前也是这么用的,都没问题,为什么现在就报错了呢? 这是因为jQuery用" ...
- POJ 2774 Long Long Message&&HDU 1403 Longest Common Substring&&COJ 1203
后缀数组的买1送2题... HDU的那题数据实在是太水了,后来才发现在COJ和POJ上都是WA..原因在一点:在建立sa数组的时候里面的n应该是字符串长度+1....不懂可以去看罗大神的论文... 就 ...
- JSON数据格式介绍
JSON定义 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于阅读和编写,同一时候也易于机器解析和生成.它基于ECMA262语言规范(1999 ...
- javascript高级知识点——继承
代码信息来自于http://ejohn.org/apps/learn/. 继承是如何工作的 function Person(){} function Ninja(){} Ninja.prototype ...
- bootstrap的导航改造
在使用bootstrap制作后台时用到了响应式导航条,其中dropdown组件更是用的比较多,用的多需要点击的就多,dropdown默认鼠标左键单击才展开,如果使用鼠标放上去(hover)就展开则会省 ...