Codeforces 621B Wet Shark and Bishops 【dp】
来源:互联网 发布:mysql与oracle的关系 编辑:程序博客网 时间:2024/05/15 02:06
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.
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.
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.
题意:有n个象在1000*1000的棋盘上,对角线上的象会互相攻击,问互相攻击的象有多少对。
思路:设置dpl[i][j]表示左上角到(i, j)这一对角线象的个数,同理dpr[i][j]。dp扫一遍就好了。
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#include <string>#define INF 1000000#define eps 1e-8#define MAXN (200000+10)#define MAXM (100000+10)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while((a)--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1#define PI acos(-1.0)#pragma comment(linker, "/STACK:102400000,102400000")#define fi first#define se secondusing namespace std;typedef pair<int, int> pii;struct Node{ int x, y;};Node num[MAXN];bool cmp(Node a, Node b){ if(a.x != b.x) return a.x < b.x; else return a.y < b.y;}int dpl[1010][1010], dpr[1010][1010];int main(){ int n; Ri(n); CLR(dpl, 0); CLR(dpr, 0); int x, y; for(int i = 1; i <= n; i++) Ri(x), Ri(y), dpl[x][y] = dpr[x][y] = 1; LL ans = 0; for(int i = 1; i <= 1000; i++) { for(int j = 1; j <= 1000; j++) { if(dpl[i][j]) { ans += 1LL*dpl[i-1][j-1]; ans += 1LL*dpr[i-1][j+1]; } dpl[i][j] += dpl[i-1][j-1]; dpr[i][j] += dpr[i-1][j+1]; } } Pl(ans); return 0;}
- 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
- CodeForces 621B Wet Shark and Bishops
- 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(思维)
- codeforce 621B Wet Shark and Bishops
- codeforce 621 B. Wet Shark and Bishops
- B. Wet Shark and Bishops
- Codeforces Round #341 (Div. 2) B. Wet Shark and Bishops
- Codeforces Round #341 (Div. 2) --B. Wet Shark and Bishops
- HDU1863 并查集与最小生成树
- 逐梦C++补遗篇之一:cout与cerr的区分
- 使用adb shell查看文件
- Mgengto 导入所有产品的图片,按sku命名分类
- (八) shiro + spring + mybatis整合开发
- Codeforces 621B Wet Shark and Bishops 【dp】
- Android AES加密/解密代码
- 如何使用Spark大规模并行构建索引
- CreateProcess()函数用法一个应用程序中调用另一个应用程序的.exe
- 如何从word开始处理html文件?以及windows上安装python lxml beautifulsoup
- 支付宝 return_url 和 notify_url 的区分
- 全文检索之lucene的优化篇 建索引库
- C#数字四舍五入的问题
- 最简单的基于FFmpeg的AVfilter的例子-纯净版