挑战程序竞赛系列(62):4.6平面上的分治法(2)

来源:互联网 发布:teradata待遇知乎 编辑:程序博客网 时间:2024/06/06 12:20

挑战程序竞赛系列(62):4.6平面上的分治法(2)

传送门:Codeforces 97B: Superset


题意:(参考hankcs)
http://www.hankcs.com/program/algorithm/codeforces-97b-superset.html

点集:给定n个点,请添加一些点,使任意两点满足①在同一条水平线或竖直线上②或构成一个矩形框住其他点。

直接看hankcs的解释吧,通俗易懂。

指出一点:新的点一定由这些坐标的横纵坐标生成,所以求出投影即能满足条件①,条件②在求解①的过程中自然满足。

注意Java中Set的使用方法,需要重写hashCode 和 equals方法,因为hashCode可能相同,所以判断两个对象是否相等还需要加入eqauls方法。

代码如下:

import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Arrays;import java.util.HashSet;import java.util.List;import java.util.Set;import java.util.StringTokenizer;public class Main{    String INPUT = "./data/judge/201709/C097B.txt";    public static void main(String[] args) throws IOException {        new Main().run();    }    static final int MAX_N = 10000 + 16;    class Pair implements Comparable<Pair>{        int x;        int y;        Pair(int x, int y){            this.x = x;            this.y = y;        }        @Override        public String toString() {            return x + " " + y;        }        @Override        public int compareTo(Pair o) {            return this.x - o.x;        }        @Override        public int hashCode() {            return x * 17 + y * 13;        }        @Override        public boolean equals(Object obj) {            if (this == obj) return true;            if (obj == null || getClass() != obj.getClass()) return false;             Pair p = (Pair) obj;            return x == p.x && y == p.y;        }    }    Set<Pair> ans = new HashSet<>();    void solve(Pair[] p, int l, int r) {        if (r - l <= 1) return;        int m = (r + l) / 2;        int x = p[m].x;        for (int i = l; i < r; ++i) {            ans.add(new Pair(x, p[i].y));        }        solve(p, l, m);        solve(p, m, r);    }    void read() {        int N = ni();        Pair[] p = new Pair[N];        for (int i = 0; i < N; ++i) {            p[i] = new Pair(ni(), ni());        }        Arrays.sort(p);        for (Pair ps : p) ans.add(ps);        solve(p, 0, N);        out.println(ans.size());        for (Pair pp : ans) {            out.println(pp);        }    }    FastScanner in;    PrintWriter out;    void run() throws IOException {        boolean oj;        try {            oj = ! System.getProperty("user.dir").equals("F:\\java_workspace\\leetcode");        } catch (Exception e) {            oj = System.getProperty("ONLINE_JUDGE") != null;        }        InputStream is = oj ? System.in : new FileInputStream(new File(INPUT));        in = new FastScanner(is);        out = new PrintWriter(System.out);        long s = System.currentTimeMillis();        read();        out.flush();        if (!oj){            System.out.println("[" + (System.currentTimeMillis() - s) + "ms]");        }    }    public boolean more(){        return in.hasNext();    }    public int ni(){        return in.nextInt();    }    public long nl(){        return in.nextLong();    }    public double nd(){        return in.nextDouble();    }    public String ns(){        return in.nextString();    }    public char nc(){        return in.nextChar();    }    class FastScanner {        BufferedReader br;        StringTokenizer st;        boolean hasNext;        public FastScanner(InputStream is) throws IOException {            br = new BufferedReader(new InputStreamReader(is));            hasNext = true;        }        public String nextToken() {            while (st == null || !st.hasMoreTokens()) {                try {                    st = new StringTokenizer(br.readLine());                } catch (Exception e) {                    hasNext = false;                    return "##";                }            }            return st.nextToken();        }        String next = null;        public boolean hasNext(){            next = nextToken();            return hasNext;        }        public int nextInt() {            if (next == null){                hasNext();            }            String more = next;            next = null;            return Integer.parseInt(more);        }        public long nextLong() {            if (next == null){                hasNext();            }            String more = next;            next = null;            return Long.parseLong(more);        }        public double nextDouble() {            if (next == null){                hasNext();            }            String more = next;            next = null;            return Double.parseDouble(more);        }        public String nextString(){            if (next == null){                hasNext();            }            String more = next;            next = null;            return more;        }        public char nextChar(){            if (next == null){                hasNext();            }            String more = next;            next = null;            return more.charAt(0);        }    }    static class ArrayUtils {        public static void fill(int[][] f, int value) {            for (int i = 0; i < f.length; ++i) {                Arrays.fill(f[i], value);            }        }        public static void fill(int[][][] f, int value) {            for (int i = 0; i < f.length; ++i) {                fill(f[i], value);            }        }        public static void fill(int[][][][] f, int value) {            for (int i = 0; i < f.length; ++i) {                fill(f[i], value);            }        }    }}

alt text

阅读全文
0 0