CodeForces - 621B Wet Shark and Bishops (数学几何&技巧)
来源:互联网 发布:易语言小游戏源码 编辑:程序博客网 时间:2024/04/29 15:11
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
51 11 53 35 15 5
6
31 12 33 5
0
Hint
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.
//题意:输入n,在输入n个点的坐标
表示在1000*1000的矩阵中有n条鲨鱼,它们所在的坐标即为所给的。如果在矩阵的对角线上有多条鲨鱼,那么这条对角线上的多条鲨鱼就会打架(并且即使它们不相邻,他们也会打架),问总共有多少对打架的鲨鱼。
//思路:
因为它是一个1000*1000的正方形,所以它的对角线条数总共有4000条,分别是y=x+b的两千条和y=-x+b的两千条.因为它们分别都是平行的,所以它们的b(截距)是唯一的。在输入的时候分别对它们的截距b的值进行记录累加就行了。
求出对应截距的个数后,就可以计算每条对角线上的打架鲨鱼的对数了。
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#define INF 0x3f3f3f3f#define ll long long#define N 3010#define M 1000000007using namespace std;int a[N];int b[N];int main(){int n;int x,y;int i,j,k;while(scanf("%d",&n)!=EOF){memset(a,0,sizeof(a));memset(b,0,sizeof(b));int xx;for(i=0;i<n;i++){scanf("%d%d",&x,&y);a[x+y]++;xx=y-x;if(xx<0)xx=(-xx+2000);b[xx]++;}int sum=0;for(i=0;i<=3000;i++){sum+=(a[i]*(a[i]-1)/2);sum+=(b[i]*(b[i]-1)/2);}printf("%d\n",sum);}return 0;}
- CodeForces - 621B Wet Shark and Bishops (数学几何&技巧)
- Codeforces--621B--Wet Shark and Bishops(数学)
- CodeForces 621B B. Wet Shark and Bishops【数学+思路】
- CodeForces 621 B Wet Shark and Bishops
- 【codeforces】#621B-Wet Shark and Bishops
- 【CodeForces】[621B]Wet Shark and Bishops
- 【CodeForces】#621B - Wet Shark and Bishops
- CodeForces 621B Wet Shark and Bishops
- CodeForces 621B Wet Shark and Bishops
- Codeforces 621B Wet Shark and Bishops
- CodeForces 621 B. Wet Shark and Bishops(水~)
- 【CodeForces】#621B - Wet Shark and Bishops(思维)
- Codeforces 621B Wet Shark and Bishops 【dp】
- Codeforces 621B - Wet Shark and Bishops(思维)
- codeforce 621B Wet Shark and Bishops
- codeforce 621 B. Wet Shark and Bishops
- *Wet Shark and Bishops(思维) CodeForces
- B. Wet Shark and Bishops
- jlink更新固件
- 页面简单布局之结构与表现原则
- 一个用BoW|Pyramid BoW+SVM进行图像分类的Matlab Demo
- HDU 2824 The Euler function [欧拉函数]
- 学习笔记--博弈组合-SG函数
- CodeForces - 621B Wet Shark and Bishops (数学几何&技巧)
- beego操作session
- jquery传参PHP的回调函数无法使用$(this)的解决办法
- kaggle-浮游生物分类比赛一等奖---译文(第三部分)
- Android 开发之AsyncTask的用法
- ECSHOP二次开发,在用户中心的订单详情页显示商品缩略图
- CSS(CS3)编码规范
- 2016蓝桥杯假期任务之《扶老奶奶过街》
- 多线程GCD的详细讲解 任务和队列(串行队列,并发队列) (一)