首页 > 其他分享 >CodeForces - 621B Wet Shark and Bishops (数学几何&技巧)

CodeForces - 621B Wet Shark and Bishops (数学几何&技巧)

时间:2023-05-08 21:32:12浏览次数:39  
标签:Shark bishops int each CodeForces Wet 1000 Bishops

Time Limit: 2000MS

 

Memory Limit: 262144KB

 

64bit IO Format: %I64d & %I64u

CodeForces - 621B


Wet Shark and Bishops



Submit Status




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





Input



51 11 53 35 15 5





Output



6





Input



31 12 33 5





Output



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)

//题意:输入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 1000000007
using 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;
}

 




标签:Shark,bishops,int,each,CodeForces,Wet,1000,Bishops
From: https://blog.51cto.com/u_16079508/6256253

相关文章

  • CodeForces - 618A Slime Combining (快速幂)
    CodeForces-618ASlimeCombiningTimeLimit: 2000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionYourfriendrecentlygaveyousomeslimesforyourbirthday.Youhave n slimesallinitiallywithvalue 1.Youare......
  • CodeForces - 626B Cards (全排列&模拟)
    TimeLimit: 2000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uCodeForces-626BCardsSubmit StatusDescriptionCatherinehasadeckof ntakeanytwo(notnecessarilyadjacent)cardswithdifferentcolorsandexchangethemforanewcardof......
  • CodeForces - 597A Divisibility (模拟)
    TimeLimit: 1000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uCodeForces-597ADivisibilitySubmit StatusDescriptionFindthenumberof k-divisiblenumbersonthesegment [a, b].Inotherwordsyouneedtofindthenumberofsuchintegerv......
  • Codeforces Round 871 (Div. 4)
    A.LoveStory#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongintread(){intx=0,f=1,ch=getchar();while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();if......
  • Codeforces Round 871 (Div. 4)
    A.LoveStory题意:给定n个长度为10的字符串,问其与codeforces字符串的对应下标字母不同的个数。分析:对于每个字符串从前往后依次和“codeforces”对应字符比较然后统计不同字母数即可code:#include<bits/stdc++.h>usingnamespacestd;intmain(){ std::ios::sync_wit......
  • Codeforces Round 871 (Div. 4)
    CodeforcesRound871(Div.4)A-LoveStory#include<bits/stdc++.h>usingnamespacestd;typedefpair<int,int>PII;typedefpair<string,int>PSI;constintN=1e5+5,INF=0x3f3f3f3f,Mod=1e6;constdoubleeps=1e-6;typedeflonglongll;i......
  • Codeforces 871 div4(重拳出击)
    Codeforces871div4ABC简单题D题意每次操作可以将当前的数分成两份,一份是\(\frac{1}{3}\),一份是\(\frac{2}{3}\),问当前数n可否进行若干次操作,最终出现一份大小为m的片。递归一下就好了,数据最大才\(10^7\)代码voiddfs(intx){ if(x==m){flag=1;return;} if(flag)re......
  • Codeforces Round 870 (Div. 2)
    CodeforcesRound870(Div.2)A-TrustNobody思路:枚举每一种说谎人数x,若a[i]大于x则说谎人数加一,判断最后说谎总人数是否为x,若是则输出x,结束枚举;若没有满足的x则-1#include<bits/stdc++.h>usingnamespacestd;typedefpair<int,int>PII;typedefpair<string,int>PSI;......
  • Codeforces 1817F - Entangled Substrings(SA)
    为什么赛时不开串串题?为什么赛时不开串串题?为什么赛时不开串串题?为什么赛时不开串串题?为什么赛时不开串串题?一种SA做法,本质上和SAM做法等价,但是说来也丢人,一般要用到SAM的题我都是拿SA过的/wul考虑将\(ac\)看作一个整体。记\(\text{occ}(S)\)为\(S\)出现位置的集......
  • Codeforces Round 848 (Div. 2)C
    B.TheForbiddenPermutation一定要注意题目中说的是对于alli满足才算不好的,我们做的时候只要破坏一个i这个a就不算好的了,被这一点坑了,没注意到all。#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;constintN=2e5+10;intp[N],a[N];vo......