原题
A. Watchmen
3 seconds
256 megabytes
standard input
standard output
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi).
They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan considers the distance between watchmen i and j to be |xi - xj| + |yi - yj|. Daniel, as an ordinary person, calculates the distance using the formula .
The success of the operation relies on the number of pairs (i, j) (1 ≤ i < j ≤ n), such that the distance between watchman i and watchmenj calculated by Doctor Manhattan is equal to the distance between them calculated by Daniel. You were asked to compute the number of such pairs.
The first line of the input contains the single integer n (1 ≤ n ≤ 200 000) — the number of watchmen.
Each of the following n lines contains two integers xi and yi (|xi|, |yi| ≤ 109).
Some positions may coincide.
Print the number of pairs of watchmen such that the distance between them calculated by Doctor Manhattan is equal to the distance calculated by Daniel.
input
3 1 1 7 5 1 5
2
6 0 0 0 1 0 2 -1 1 0 1 1 1
output
11
In the first sample, the distance between watchman 1 and watchman 2 is equal to |1 - 7| + |1 - 5| = 10 for Doctor Manhattan and for Daniel. For pairs (1, 1), (1, 5) and (7, 5), (1, 5) Doctor Manhattan and Daniel will calculate the same distances.
分析
曼哈顿距离和两点间的距离相等的总数即是答案(但要排去x与y两点同样时重复的次数)。
代码
Github:https://github.com/GenialX/Codeforces/blob/master/src/Y16/M03/D10/Q1.java
[code lang=”java”]
import java.util.Arrays;
import java.util.Scanner;
/**
* A. Watchmen
* http://codeforces.com/contest/650/problem/A
*
* @author GenialX
*
*/
public class Q1 {
public static void main(String args[]) throws Exception{
Scanner in = new Scanner(System.in);
int pairCount = in.nextInt();
long sum = 0;
int[] x = new int[pairCount];
int[] y = new int[pairCount];
long[] z = new long[pairCount];
for(int i = 0; i < pairCount; i++) {
x[i] = in.nextInt();
y[i] = in.nextInt();
z[i] = (x[i] + 1000000000L);
z[i] *= 10000000000L;
z[i] += y[i];
}
Arrays.sort(x);
Arrays.sort(y);
Arrays.sort(z);;
sum = Q1.getIntSum(x, pairCount);
sum += Q1.getIntSum(y, pairCount);
sum += Q1.getLongSum(z, pairCount);
System.out.println(sum);
in.close();
return ;
}
public static long getLongSum(long[] x, int N) {
long xCnt = 0;
long xLast = Long.MIN_VALUE;
long sum = 0;
for(int i = 0; i < N; i++) {
if(xLast != x[i]) {
sum -= xCnt * (xCnt – 1) / 2;
xLast = x[i];
xCnt = 1;
} else {
xCnt++;
}
}
sum -= xCnt * (xCnt – 1) / 2;
return sum;
}
public static long getIntSum(int[] x, int N) {
long xCnt = 0;
int xLast = Integer.MIN_VALUE;
long sum = 0;
for(int i = 0; i < N; i++) {
if(xLast != x[i]) {
sum += xCnt * (xCnt – 1) / 2;
xLast = x[i];
xCnt = 1;
} else {
xCnt++;
}
}
sum += xCnt * (xCnt – 1) / 2;
return sum;
}
}
[/code]
问题
注意在计算时整形字段的溢出问题,如代码中getLongSum方法中的xCnt若为短整型(int),则会出现问题。
文章来源:胡旭个人博客 => 【原】Codeforces Round #345 (Div. 1) A watchmen
转载请注明出处,违者必究!