zoukankan      html  css  js  c++  java
  • 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 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) == 0;

    用到了容斥原理,计算 xi & xj 相等的点有多少个,计算 yi & yj 相等的点有多少个,然后再减去 xi & xj 和 yi & yj 都想的点有多少个。

    package codefroces345;
    
    import java.io.*;
    import java.util.*;
    
    public class  C345{
        /*
        * java io 系统给的这么慢。。。时间是优化后的3倍。。。
        * */
        static class Pair{
            int x, y;
            public Pair(int x, int y){
                this.x = x;
                this.y = y;
            }
            @Override
            public int hashCode(){
                final int prime = 31;
                int result = 1;
                result = prime * result + x;
                result = prime * result + y;
                return result;
            }
    
            @Override
            public boolean equals(Object obj){
                if(this == obj){
                    return true;
                }
                if(this == null) {
                    return false;
                }
                if(getClass() != obj.getClass()){
                    return false;
                }
                Pair other = (Pair)obj;
                if(x != other.x)
                    return false;
                if(y != other.y)
                    return false;
                return true;
            }
        }
    
        public static void main(String[] args){
            Scanner scanner = new Scanner(new InputStreamReader(System.in));
            int n;
            n = scanner.nextInt();
            HashMap<Integer, Integer> xs = new HashMap<>();
            HashMap<Integer, Integer> ys = new HashMap<>();
            HashMap<Pair, Integer> both = new HashMap<>();
    
            for(int i = 0; i < n; ++i) {
                int x = scanner.nextInt();
                int y = scanner.nextInt();
                Pair p = new Pair(x, y);
                xs.put(x, xs.getOrDefault(x, 0) + 1);
                ys.put(y, ys.getOrDefault(y, 0) + 1);
                both.put(p, both.getOrDefault(p, 0) + 1);
            }
    
            long ans = 0;
            for(int v : xs.values()){
                ans += (long) v * (v-1) / 2;
            }
            for(int v : ys.values()){
                ans += (long) v * (v-1) / 2;
            }
            for(int v : both.values()) {
                ans -= (long) v * (v-1) / 2;
            }
            System.out.println(ans);
        }
    }
    

      

    看人家们的代码,用到了BufferedReader包装加速

    package codefroces345;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.util.HashMap;
    import java.util.StringTokenizer;
    
    /**
     * Created by lenovo on 2016-03-10.
     */
    
    /*
    * 经过 i/o 包装后,只要600ms左右,还是学的少
    * */
    public class C {
        BufferedReader br;
        PrintWriter out;
        StringTokenizer st;
        boolean eof;
    
        static class Pair {
            int x, y;
    
            public Pair(int x, int y) {
                this.x = x;
                this.y = y;
            }
    
            @Override
            public int hashCode() {
                final int prime = 31;
                int result = 1;
                result = prime * result + x;
                result = prime * result + y;
                return result;
            }
    
            @Override
            public boolean equals(Object obj) {
                if (this == obj)
                    return true;
                if (obj == null)
                    return false;
                if (getClass() != obj.getClass())
                    return false;
                Pair other = (Pair) obj;
                if (x != other.x)
                    return false;
                if (y != other.y)
                    return false;
                return true;
            }
        }
        void solve() throws IOException {
            int n = nextInt();
            HashMap<Integer, Integer> xs = new HashMap<>();
            HashMap<Integer, Integer> ys = new HashMap<>();
            HashMap<Pair, Integer> both = new HashMap<>();
    
            for (int i = 0; i < n; i++) {
                int x = nextInt();
                int y = nextInt();
                Pair p = new Pair(x, y);
                xs.put(x, xs.getOrDefault(x, 0) + 1);
                ys.put(y, ys.getOrDefault(y, 0) + 1);
                both.put(p, both.getOrDefault(p, 0) + 1);
            }
    
            long ans = 0;
            for (int v : xs.values()) {
                ans += (long)v * (v - 1) / 2;
            }
    
            for (int v : ys.values()) {
                ans += (long)v * (v - 1) / 2;
            }
    
            for (int v : both.values()) {
                ans -= (long)v * (v - 1) / 2;
            }
    
            out.println(ans);
        }
        C() throws  IOException{
            br = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
            solve();
            out.close();
            br.close();
        }
        public static void main(String[] args) throws  IOException{
            new C();
        }
        String nextToken(){
            while(st == null || !st.hasMoreTokens()){
                try{
                    st = new StringTokenizer(br.readLine());
                } catch(IOException e) {
                    eof = true;
                    return null;
                }
            }
            return st.nextToken();
        }
    
        String nextString(){
            try{
                return br.readLine();
            } catch (Exception e) {
                eof = true;
                return null;
            }
        }
    
        int nextInt() throws  IOException {
            return Integer.parseInt(nextToken());
        }
    
        long nextLong() throws IOException {
            return Long.parseLong(nextToken());
        }
    
        double nextDouble() throws IOException {
            return Double.parseDouble(nextToken());
        }
    }
    

      

  • 相关阅读:
    ubuntu密码正确,却不能登录图形界面
    【转】ubuntu右键在当前位置打开终端
    一些值得学习的Unity教程 (很实用的包括源码)
    Git 报错:git
    Unity3D面试——真实的面试,unity3d面试
    拖拽以及常用的鼠标事件
    白话经典算法系列之一 冒泡排序的三种实现
    c#封装三维向量,另外也看了下别人的C++封装
    c#面试3(选择题)
    Unity3D中目标相对自身的前后左右方位判断
  • 原文地址:https://www.cnblogs.com/ya-cpp/p/5262702.html
Copyright © 2011-2022 走看看