Codeforces Round #345 (Div. 1) A watchmen

看到此文,是否觉得体内洪荒之力爆发,饥渴难耐想吐槽、情不自禁想捐赠
本文为原创文章,尊重辛勤劳动,可以免费摘要、推荐或聚合,亦可完整转载,但完整转载需要标明原出处,违者必究。

支付宝微  信

原题

A. Watchmen

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input
3
1 1
7 5
1 5
output
2
input
6
0 0
0 1
0 2
-1 1
0 1
1 1
output
11
Note

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

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 &lt; 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 &lt; 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 &lt; 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;
	}
	
}

问题

注意在计算时整形字段的溢出问题,如代码中getLongSum方法中的xCnt若为短整型(int),则会出现问题。


文章来源:胡旭个人博客 => 【原】Codeforces Round #345 (Div. 1) A watchmen

转载请注明出处,违者必究!


这是一篇原创文章,如果您觉得有价值,可以通过捐赠来支持我的创作~
捐赠者会展示在博客的某个页面,钱将会用在有价值的地方,思考中...


分类: JAVA, 技术, 技术分享, 算法, 编程, 随记 | 标签: , , , , | 评论 | Permalink

发表评论

电子邮件地址不会被公开。 必填项已用*标注