zoukankan      html  css  js  c++  java
  • [Codeforces 650A] Watchmen

    [题目链接]

            https://codeforces.com/problemset/problem/650/A

    [算法]

            显然 , 只有横坐标 / 纵坐标相等的点 , 才会满足 : .    = | xi - xj | + | yi - yj |

            如果有n个点的横 / 纵坐标相等 , 那么它们将会对答案产生n(n - 1) / 2的贡献

            不妨维护三个std :: map , 分别记录横坐标相同 , 纵坐标相同和重点的个数 

            时间复杂度 : O(NlogN)

    [代码]

             

    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN = 1e5 + 10;
    
    int n;
    long long ans;
    map<int,int> a,b;
    map<pair<int,int>,int> c;
    
    template <typename T> inline void read(T &x)
    {
        T f = 1; x = 0;
        char c = getchar();
        for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
        for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
        x *= f;
    }
    
    int main()
    {
            
            read(n);
            for (int i = 1; i <= n; i++) 
            {
                    int x , y;
                    read(x); read(y);
                    a[x]++; b[y]++;
                    c[make_pair(x,y)]++;
            }
            for (map< int,int > :: iterator it = a.begin(); it != a.end(); it++)
            {
                    int value = it -> second;
                    if (value == 1) continue;
                    ans += 1ll * value * (value - 1) / 2;        
            }
            for (map< int,int > ::iterator it = b.begin(); it != b.end(); it++)
            {
                    int value = it -> second;
                    if (value == 1) continue;
                    ans += 1ll * value * (value - 1) / 2;
            }
            for (map< pair<int,int>,int > :: iterator it = c.begin(); it != c.end(); it++)
            {
                    int value = it -> second;
                    if (value == 1) continue;
                    ans -= 1ll * value * (value - 1) / 2;
            }
            printf("%I64d
    ",ans);
            
            return 0;
        
    }
  • 相关阅读:
    52、saleforce 第一篇
    nodejs自定义模块
    NodeJS require路径
    Angularjs ngTable使用备忘
    HTML5拖拽功能中 dataTransfer对象详解
    Javascript闭包深入解析及实现方法
    Grunt实例
    Grunt插件uglify
    javascript 将字符串当函数执行
    多个springboot项目部署在同一tomcat上,出现jmx错误
  • 原文地址:https://www.cnblogs.com/evenbao/p/9715159.html
Copyright © 2011-2022 走看看