zoukankan      html  css  js  c++  java
  • CodeForces 651C

    Description

    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.

    Sample Input

    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

    Hint

    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,等式就会成立,暴力的话容易超时,学长当时给了题解,

    用stl的map去写,后来自己也看了下,的确很好用

    统计x相等的个数加上y相等的个数减去xy同时相等的个数,就是结果了

    代码是学长给的,发现比网上的一些简单多了

     1 #include <iostream>
     2 #include <map>
     3 using namespace std;
     4 int main()
     5 {
     6     int n;
     7     while (cin>>n)
     8     {
     9         map<int,int> mx;
    10         map<int,int> my;
    11         map<pair<int,int>,int>mxy;
    12         long long ans = 0;
    13         for (int i=0;i<n;++i)
    14         {
    15             int x,y;
    16             cin>>x>>y;
    17             ans += mx[x]++;
    18             ans += my[y]++;
    19             ans -= mxy[make_pair(x,y)]++;
    20         }
    21         cout<<ans<<endl;
    22     }
    23 }
  • 相关阅读:
    luogu P2570 [ZJOI2010]贪吃的老鼠【二分+最大流】
    luogu P5358 [SDOI2019]快速查询【模拟(?)】
    CF360E Levko and Game【贪心+dijsktra】
    bzoj 2632: [neerc2011]Gcd guessing game【贪心】
    bzoj 2535: [Noi2010]Plane 航空管制2【拓扑排序+堆】
    Amazon免费CE2基于docker部署nginx,并实现访问
    使用FlashFXP,密钥方式连接Amazon的CE2实例
    python 提示 AttributeError: module 'json' has no attribute 'dumps'
    ueditor工具栏新增按钮教程
    Express4+Mongodb超简单入门实例
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/5687952.html
Copyright © 2011-2022 走看看