Codeforces Round #345 (Div. 2)(C)排列组合,思维

来源:互联网 发布:手机淘宝网客户端 编辑:程序博客网 时间:2024/05/17 08:06

C. 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 watchmen j 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
31 17 51 5
output
2
input
60 00 10 2-1 10 11 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.




题意:给你n个点让你找出满足下式的等式:

|xi - xj| + |yi - yj|==



题解:开始根本不知道怎么入手,暴力肯定是不可以的,1e10肯定会超时的,那么我们可以先去化简等式去找一下规律,

化简后的式子如下:2|xi - xj| * |yi - yj|=0

那么观察一下就可以清晰的看出,要找出满足(xi==xj||yi==yj)这样的点,这样就大大

简化了问题,我们统计x,y相同的点使用排列公式C(N,2)求出全排列,但是这里有

一个问题,我们的点会重复,这样会重复多算一些组合,在x中算过一次又在y

中算了一次,那么我们只要减去每个相同坐标的内部全排列C(M,2),就会去掉重

复计算的部分,我们使用STL的map维护相同点的个数,使用set维护相同(x/y)

上点的(y/x)的个数,这一题就解决了




#include <set>    #include <map>    #include <list>     #include <cmath>     #include <queue>     #include <vector>    #include <cstdio>     #include <string>     #include <cstring>    #include <iomanip>     #include <iostream>     #include <sstream>    #include <algorithm>#include <utility>#define LL long long   using namespace std;#define N int(1e3+5)   #define inf 0x3f3f3f3fmap<int,multiset<int>>xx, yy;map<pair<int, int>,int>pp;int main(){#ifdef CDZSC  freopen("i.txt", "r", stdin);#endifint n;int x, y;while (~scanf("%d", &n)){xx.clear();pp.clear();yy.clear();for (int i = 0;i<n; i++){scanf("%d%d", &x, &y);xx[x].insert(y);yy[y].insert(x);if (!pp.count(make_pair(x, y)))pp[make_pair(x, y)] = 1;elsepp[make_pair(x, y)]++;}LL ans = 0; map<int, multiset<int> >::iterator it;for (it = xx.begin();it!=xx.end();it++){LL x = it->second.size();ans += ((x - 1)*x) / 2;}for (it = yy.begin(); it !=yy.end(); it++){LL x = it->second.size();ans += ((x-1)*x)/2;}for (map<pair<int, int>, int>::iterator it2 = pp.begin(); it2 != pp.end(); it2++){LL x = it2->second;ans -= (x*(x - 1)) / 2;}printf("%lld\n", ans);}return 0;}







0 0
原创粉丝点击