codeforce 621 B. Wet Shark and Bishops
来源:互联网 发布:h3c配置vlan多个端口 编辑:程序博客网 时间:2024/05/16 11:26
Today, Wet Shark is given n bishops on a1000 by1000 grid. Both rows and columns of the grid are numbered from1 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 integersxi andyi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column wherei-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.
51 11 53 35 15 5
6
31 12 33 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
输入:第一个表示bishops的个数,接下来n行为bishops的坐标。
输出:对于处于同一条对角线的的bishop他们可以相互攻击,
解法:对于1000*1000的格子,共有4000多个对角线。分别扫描每条对角线存在的点的个数,然后对每条对角线求和。
AC:
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<algorithm>
#include<set>
#include<queue>
using namespace std;
#define maxn 1300
#define ll __int64
bool vis[maxn][maxn];
int cnt[4*maxn];
vector< pair<int,int> > q;
int main()
{
ll n;
ll ans=0;
scanf("%I64d",&n);
memset(vis,0,sizeof(vis));
memset(cnt,0,sizeof(cnt));
while(n--)
{
int x,y;
scanf("%d%d",&x,&y);
vis[x][y]=1;
}
for(int i=1;i<=1000;i++)
{
int x,y;
x=i;y=1;
while(x<=1000&&y<=1000)
{
if(vis[x][y]==1){cnt[i]++;}
x++;y++;
}
x=i;y=1;
while(x>=1&&y>=1)
{
if(vis[x][y]==1){cnt[1000+i]++;}
x--;y++;
}
x=1;y=i+1;
while(x<=1000&&y<=1000)
{
if(vis[x][y]==1){cnt[i+2000]++;}
x++;y++;
}
x=1000;y=i+1;
while(x>=1&&y<=1000)
{
if(vis[x][y]==1){cnt[3000+i]++;}
x--;y++;
}
}
for(int i=1;i<=4000;i++)
{
ans+=cnt[i]*(cnt[i]-1)/2;
}
printf("%I64d\n",ans);
}
- codeforce 621B Wet Shark and Bishops
- codeforce 621 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
- B. Wet Shark and Bishops
- CodeForces 621B B. Wet Shark and Bishops【数学+思路】
- Codeforces 621B Wet Shark and Bishops 【dp】
- 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(思维)
- Wet Shark and Bishops
- Wet Shark and Bishops
- 一些不错的技术文章
- spring mvc + mybaties+ mysql搭建--2016版
- 一个网页通用的测试用例(转载他人)
- 【Lintcode】斐波纳契数列
- sscanf,sscanf_s及其相关用法
- codeforce 621 B. Wet Shark and Bishops
- 算法系列_基础01_O(nlogn)的理解
- 自定义控制器的转场动画(Push、Pop)
- Android事件分发
- Android学习笔记(31):滚动视图ScrollView
- Cookie/Session机制详解
- 欢迎使用CSDN-markdown编辑器
- HTTPS 初解
- Json:前台对象数组传到后台解析