CodeForces 621B Wet Shark and Bishops
2016-07-28 17:05
513 查看
题目:
Description
Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns
of the grid are numbered from 1 to 1000. 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.
Input
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
Output one integer — the number of pairs of bishops which attack each other.
Sample Input
Input
Output
Input
Output
这个题目是这次比赛最简单的题目。
在1000*1000的棋盘上面,有1999条左斜线,还有1999条右斜线。
输入数据的同时,统计每条斜线上面的象的数量k。
最后统计k*(k-1)/2求和即可。
代码:
Description
Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns
of the grid are numbered from 1 to 1000. 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.
Input
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
Output one integer — the number of pairs of bishops which attack each other.
Sample Input
Input
5 1 1 1 5 3 3 5 1 5 5
Output
6
Input
3 1 1 2 3 3 5
Output
0
这个题目是这次比赛最简单的题目。
在1000*1000的棋盘上面,有1999条左斜线,还有1999条右斜线。
输入数据的同时,统计每条斜线上面的象的数量k。
最后统计k*(k-1)/2求和即可。
代码:
#include<iostream> #include<string.h> using namespace std; int sumlist[2000]; //x+y-1,1-1999 int diflist[2000]; //x-y+1000,1-1999 int main() { int n; int x, y; memset(sumlist, 0, sizeof(sumlist)); memset(diflist, 0, sizeof(diflist)); cin >> n; while (n--) { cin >> x >> y; sumlist[x + y - 1]++; diflist[x - y + 1000]++; } int sum = 0; for (int i = 1; i < 2000; i++)
sum += sumlist[i] * (sumlist[i] - 1) / 2 + diflist[i] * (diflist[i] - 1) / 2; cout << sum; return 0; }
相关文章推荐
- [linux]通过ssh远程设定各服务器时间,从而实现集群时间同步
- 制作Linux(Fedora、Ubuntu、CentOS)优盘启动 制作Linux(Fedora、Ubuntu、CentOS)优盘启动
- mac nginx 整合tomcat 配置
- OpenCV imgproc模块(二)
- nginx+php-fpm出现502 bad gateway错误解决方法__转发
- 如何避免误用分布式事务(System.Transactions.TransactionScope)
- 韩购物网站服务器遭黑客攻击 千万用户信息被泄
- CentOS6配置部署Zabbix监控
- jenkins和docker 使用docker作为slave
- 观止云技术实践| 可追溯日志:视频云时代的新运维大胸器
- WANMPServer一键服务器v0.3 64位版
- 搭建nginx+tomcat+Java的负载均衡环境
- Kali Linux 秘籍 第二章 定制 Kali Linux
- linux终端在已经显示的输出中查找
- Linux 批量更改文件后缀名
- Linux中常用操作命令
- 对/etc/rc.d/init.d/目录的一点理解
- @property 的用法
- 一次nginx 504 Gateway Time-out错误排查、解决记录__转载
- 记一次学习冯大辉的网站运维之道