Codeforces Round #341 (Div. 2)(B)数学
2016-02-03 22:38
405 查看
B. Wet Shark and Bishops
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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 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 test(s)
input
output
input
output
Note
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,x-y都是固定且唯一的值。其实这个原理很好证明,由斜截式 y = kx+b可知,显然x-y(或y-x)是一个常数,那么我们只需要开2个一维数组就可以表示所有的线段了,注意x-y可能是负数,所以数组开为最大和的2倍在加上一个最大和防止负数就好了。
其实我是想吐槽这一题真的有毒呀
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define N 5000int line1
,line2
;
int half=N/2;
int f(int x)
{
return ((x-1)*x)/2;
}
int main()
{
memset(line1,0,sizeof(line1));
memset(line2,0,sizeof(line2));
int n,x,y;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&x,&y);
line1[x+y]++;
line2[x-y+half]++;
}
int ans=0;
for(int i=0;i<N;i++)
{
ans+=f(line1[i]);
ans+=f(line2[i]);
}
printf("%d\n",ans);
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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 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 test(s)
input
5 1 1 1 5 3 3 5 1 5 5
output
6
input
3 1 1 2 3 3 5
output
0
Note
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,x-y都是固定且唯一的值。其实这个原理很好证明,由斜截式 y = kx+b可知,显然x-y(或y-x)是一个常数,那么我们只需要开2个一维数组就可以表示所有的线段了,注意x-y可能是负数,所以数组开为最大和的2倍在加上一个最大和防止负数就好了。
其实我是想吐槽这一题真的有毒呀
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define N 5000int line1
,line2
;
int half=N/2;
int f(int x)
{
return ((x-1)*x)/2;
}
int main()
{
memset(line1,0,sizeof(line1));
memset(line2,0,sizeof(line2));
int n,x,y;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&x,&y);
line1[x+y]++;
line2[x-y+half]++;
}
int ans=0;
for(int i=0;i<N;i++)
{
ans+=f(line1[i]);
ans+=f(line2[i]);
}
printf("%d\n",ans);
return 0;
}
相关文章推荐
- Python 14.3 UDP编程
- poj1088 dp 记忆化搜索 (入门好题)
- Nios学习笔记1——流水灯实验
- vim与复制,删除,粘贴,块操作以及快速替换功能
- Git Shell 基本命令(官网脱水版)
- Table ‘performance_schema.session_variables’ doesn’t exist
- BitmapFactory.Options详解
- Codeforces 621 E Wet Shark and Blocks
- Python与Qt UI的图形界面编程(一)
- break,continue,return的区别
- Linux部分命令解释(命令缩写代表什么意思)
- 记录安卓学习过程(1)
- 【斜率优化】[CEOI2004]锯木厂选址——从这里开始斜率优化的大门
- php中函数的定义,调用,及获取参数的方法。和位运算等。
- HDU.1003【max-sum】---16.2.3
- react(2)--Thinking in React
- ORB-SLAM2:基于可识别特征的自主导航与地图构建
- log4j【5】( log4j日志异步化大幅提升系统性能 )
- hdu 4489 The King’s Ups and Downs (组合数学 + dp )
- 乐美客HiKey 96Board