跳过正文
  1. Posts/

codeforces #341 div 2 B. Wet Shark and Bishops

·2 分钟

http://codeforces.com/contest/621/problem/B

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 x__i and y__i (1 ≤ x__i, y__i ≤ 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.

题意:给出n个点,问在同一对角线上的有多少对。

思路:同一对角线上恒纵坐标和相同或者差相同。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年01月31日 星期日 22时02分40秒
 4File Name :code/cf/#341/B.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=2E5+7;
34const int C=1000;
35struct point
36{
37    int x,y;
38
39    void in()
40    {
41	scanf("%d %d",&x,&y);
42    }
43}p[N];
44int n ;
45LL a[2005],b[2005];  //b :2..2000
46			//a:-999 ..999
47			//
48LL cal( LL x)
49{
50    LL res = x*(x-1)/2;
51    return res;
52}
53int main()
54{
55	#ifndef  ONLINE_JUDGE
56	freopen("code/in.txt","r",stdin);
57  #endif
58
59	cin>>n;
60	for ( int i = 0 ;i < n ; i++) p[i].in();
61
62	ms(a,0);
63	ms(b,0);
64
65	for ( int i = 0 ; i < n ; i++)
66	{
67	    a[p[i].x-p[i].y+C]++;
68	    b[p[i].x+p[i].y]++;
69	}
70	LL ans = 0 ;
71	for ( int i = 1 ; i <=2001 ; i++)
72	{
73	    if (a[i]==0) continue;
74	    ans += cal(a[i]);
75	}
76	for ( int i = 1  ; i <= 2001 ; i ++)
77	{
78	    if (b[i]==0) continue;
79	    ans += cal(b[i]);
80	}
81	cout<<ans<<endl;
82
83
84  #ifndef ONLINE_JUDGE
85  fclose(stdin);
86  #endif
87    return 0;
88}

相关文章

uva 120 Stacks of Flapjacks

·2 分钟
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid;=8&page;=show_problem&problem;=56 题意:给出一个长度为n的序列(无重复元素),询问经过多少次flip(i)操作,使得序列升序排列。定义flip(i)为将1到n-i+1的元素反转… 思路:先离散化,然后注意读入….

codeforces 27 C. Unordered Subsequence

·2 分钟
http://codeforces.com/contest/27/problem/C 题意:给出一个序列,问是否存在一个disordered的子序列。。输出长度并输出组成子序列的下表(1..n)。如果有多组,输出任意一组。 disordered的意思是。。升序或者降序(不严格也可以)之外的情况。 思路: 首先我们可以知道,我们要找的子序列至少需要三个点。因为两个点怎么看都是有序的。而如果有k个点(k>3)组成的子序列存在。。那么机智得去掉其中一些点,可以只剩三个 ,同样满足题意。所以我们只需要找到三个点即可。如果把点以下标为横坐标,值为纵坐标花在坐标系上,就是找一个v型或者倒v型的三个点。

codeforces 612 A. The Text Splitting

http://codeforces.com/contest/612/problem/A 水题…直接枚举就好。 1/* *********************************************** 2Author :111qqz 3Created Time :2015年12月25日 星期五 22时58分26秒 4File Name :code/cf/edu4/A.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=105; 34int n,p,q; 35char st[N]; 36bool v[10005]; 37int main() 38{ 39 #ifndef ONLINE_JUDGE 40 freopen("code/in.txt","r",stdin); 41 #endif 42 cin>>n>>p>>q; 43 cin>>st; 44 ms(v,false); 45 int b,c; 46 for ( int i = 0 ; i*p<=105 ; i++ ) 47 { 48 for ( int j = 0 ; j*q <= 105 ; j++) 49 { 50 v[i*p+j*q] = true; 51 if (i*p+j*q==n) 52 { 53 b = i; 54 c = j; 55 } 56 } 57 } 58// cout<<"b:"<<b<<endl; 59// cout<<"c:"<<c<<endl; 60 if (!v[n]) 61 { 62 puts("-1"); 63 } 64 else 65 { 66 int cnt = 0 ; 67 printf("%d\n",b+c); 68 for ( int i = 1 ; i <= b ; i++) 69 { 70 for ( int i = 0 ; i < p ; i++) 71 printf("%c",st[cnt]),cnt++; 72 printf("\n"); 73 } 74 for ( int i =1 ; i <= c ; i++) 75 { 76 for ( int i = 0 ; i < q ; i++) 77 printf("%c",st[cnt]),cnt++; 78 printf("\n"); 79 } 80 } 81 82 83 84 #ifndef ONLINE_JUDGE 85 fclose(stdin); 86 #endif 87 return 0; 88}

codeforces #336 div 2 A. Saitama Destroys Hotel

·1 分钟
http://codeforces.com/contest/608/problem/A 题意:一个电梯,从s到0层,单项,给出n个人,每个人在某时间出现在某层,问要花多长时间把所有人运到0。初始电梯在s,每下一层话费时间1,上来人不花时间。 思路:由于电梯只能是单项,那么到达某一层的时候,一定要等到把这层中最晚来的那个接走。所以排序的时候按照楼层高到低为第一关键字,等待时间长到短为第二关键字。对于同一楼层出现的,只考虑第一个即可,也就是最后出现的。需要注意的是最后接完最后一个人以后把电梯运行道0层。

codeforces 466 C. Number of Ways

·2 分钟
http://codeforces.com/problemset/problem/466/C 题意:给定一个序列。要将序列分成三个非零的连续部分,使得三部分的和相等。问有多少中分法。 思路:首先可以知道,如果是序列的和不为3的倍数,那么一定无解,输出0.设序列的和为sum,那么每一部分的和就应该为sum/3。我们可以预处理出从1开始的和为sum/3的点(我开了数组表示前缀和。。想了下其实不用。。我只需要点的信息。。所以用一个变量表示即可),将点的下标存在p[i]里。对于每一个p[i],我想要知道比p[i]大且补与p[i]相邻的点中,有多少个j,使得从j到n的和为sum/3。因为如果有两部分的和都为sum/3,那么剩下的那部分也一定为sum/3.然后要知道有多少个满足题意的j,我们可以从后往前扫一遍,标记从n开始往前扫,和为sum/3的点,可以用一个0,1数组表示。如果和为sum/3,那么标记为1,否则为0.然后再用一个类似前缀和的思路。再开一个数组c记录从j到n有的和为多少,也就是从j到n有多少个点满足该点到n的和为sum/3.