18行代码AC_Wet Shark and Bishops CodeForces - 621B(数学推导+映射)
勵(lì)志用少的代碼做高效表達(dá)
Problem describe
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.
題意分析
題意:給定一個(gè)1000*1000的棋盤(pán),主教棋子的數(shù)量n。 規(guī)則是若某一主教與其他主教出現(xiàn)在同一條對(duì)角線上, 就會(huì)互相攻擊。 問(wèn)互相攻擊的主教棋子的對(duì)數(shù)。
首先思考:
若同一主對(duì)角線上有n個(gè)元素,則在這條對(duì)角線上互相攻擊棋子的對(duì)數(shù)為:n*(n-1)/2。(第一個(gè)棋子與其余n-1個(gè)互相攻擊,第二個(gè)棋子與其余n-2個(gè)互相攻擊…即:n-1+n-2+n-3+…+1最后使用等差數(shù)列求和公式)
因此問(wèn)題轉(zhuǎn)化為求出棋盤(pán)中每條對(duì)角線上的棋子個(gè)數(shù), 分別代入公式,將結(jié)果累加即可。
很容易推導(dǎo)出規(guī)則:同一主對(duì)角線的數(shù)相減相等; 同一副對(duì)角線的數(shù)相加相等;
注意:第一次嘗試時(shí),我首先想到利用map容器的歸納性質(zhì)做映射求解, 但其實(shí)直接用數(shù)組解即可(貌似大多數(shù)的map映射都可簡(jiǎn)化為數(shù)組)。
#include<bits/stdc++.h> using namespace std; int main() {ios::sync_with_stdio(false);int n, a[4010]; //采用同一數(shù)組的不同區(qū)間表達(dá)兩組數(shù)cin>>n; memset(a,0,sizeof(a));for(int i=0;i<n;i++) {int x, y;cin>>x>>y;a[x+y]++;a[x-y+3000]++;} long long sum=0;for(int i = 0; i < 4000; i++) if(a[i]>1) sum+=(a[i]*(a[i]-1)/2);cout << sum << endl; return 0; }
天空不會(huì)永遠(yuǎn)陰暗,當(dāng)烏云退盡的時(shí)候,藍(lán)天上燦爛的陽(yáng)光就會(huì)照亮大地。青草照樣會(huì)鮮綠無(wú)比,花朵仍然會(huì)蓬勃開(kāi)放。?????????????????——《平凡的世界》
總結(jié)
以上是生活随笔為你收集整理的18行代码AC_Wet Shark and Bishops CodeForces - 621B(数学推导+映射)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 暴力优化解法+哈希解法——2016年第七
- 下一篇: 18行代码AC_排序 HDU - 110