• [Codeforces] 650A


    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 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
    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.

    题意

    给出一个点集,定义一个二元关系成立条件如下:两点距离原点的曼哈顿距离和欧几里得距离相等,求点集中有多少对这种符合关系的点集

    分析

    其实只要两个点符合Xi == Xj或者Yi == Yj就行了(具体为什么画个Rt三角形就行),重点是如何处理同坐标的点。

    首先根据各点X排序一下,然后计算同X点的关系数。在这里先把同一个坐标的那些点忽略不计(注意)

    之后根据各点Y排序,这次将同坐标的点和同Y点一起加起来。

    顺便,CF要求不能用long long要用__int64和%I64d ...

    代码

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<algorithm>
     6 #define maxn 200000
     7 using namespace std;
     8 
     9 struct Poi{
    10     __int64 x,y;
    11 }list[maxn*2];
    12 
    13 bool com_x(Poi A,Poi B){
    14     if(A.x < B.x) return true;
    15     if(A.x == B.x) return A.y < B.y;
    16     if(A.x > B.x) return false;
    17 }
    18 
    19 bool com_y(Poi A,Poi B){
    20     if(A.y < B.y) return true;
    21     if(A.y == B.y) return A.x < B.x;
    22     if(A.y > B.y) return false;
    23 }
    24 
    25 int main(){
    26     __int64 n,cnt1 = 1,cnt2 = 1,ans = 0;
    27     scanf("%I64d",&n);
    28     for(int i = 0;i < n;i++){
    29         scanf("%I64d%I64d",&list[i].x,&list[i].y);
    30     }
    31     
    32     sort(list,list+n,com_x);
    33     
    34     for(int i = 1;i < n;i++){
    35         if(list[i].x == list[i-1].x){
    36             cnt1++;
    37             if(list[i].y == list[i-1].y) cnt2++;
    38             else{
    39                 ans -= cnt2*(cnt2-1)/2;
    40                 cnt2 = 1;
    41             }
    42         }else{
    43             ans += cnt1*(cnt1-1)/2;
    44             cnt1 = 1;
    45             ans -= cnt2*(cnt2-1)/2;
    46             cnt2 = 1;
    47         }
    48     }
    49     
    50     ans += cnt1*(cnt1-1)/2-cnt2*(cnt2-1)/2;
    51     
    52     cnt1 = 1;
    53     sort(list,list+n,com_y);
    54     
    55     for(int i = 1;i < n;i++){
    56         if(list[i].y == list[i-1].y) cnt1++;
    57         else{
    58             ans += cnt1*(cnt1-1)/2;
    59             cnt1 = 1;
    60         }
    61     }
    62     
    63     ans += cnt1*(cnt1-1)/2;
    64     
    65     printf("%I64d",ans);
    66     
    67     return 0;
    68 }
    Sure to read??

    理论基础

    算法设计

    转载请注明出处 -- 如有意见欢迎评论
  • 相关阅读:
    jsonp的实现原理和缺点
    vue $set的使用初了解
    js将多维数组转换为一维数组
    微信小程序之页面打开数量限制
    ZOJ 1463 Brackets Sequence
    CSUOJ 1248 非变性聚丙烯酰胺凝胶电泳
    ZOJ 1524 Supermarket
    POJ 1012 Joseph
    ZOJ 1276 Optimal Array Multiplication Sequence
    POJ 1742 Coins
  • 原文地址:https://www.cnblogs.com/Chorolop/p/7148107.html
Copyright © 2020-2023  润新知