挑战程序竞赛系列(87):3.6平面扫描(1)

来源:互联网 发布:手游数据互通什么意思 编辑:程序博客网 时间:2024/05/17 22:05

挑战程序竞赛系列(87):3.6平面扫描(1)

传送门:POJ 2932: Coneology


题意:

平面上有N个两两没有公共点的圆,i号圆的圆心在(xi,yi),半径为ri。求所有最外层的,不被任何圆包含的圆。

此题还给了一个条件,任何两圆都没有公共点,所以要么是包含关系,要么相离,不存在相交的情况。

所以朴素的做法是枚举,而判断一个圆是否包含另一个圆的方法,只需要考虑两个圆心的距离是否小于等于大圆的半径即可。时间复杂度为O(n2)

此题还可以更快,采用记忆化手段,把先前的访问的信息记录下来,但记录什么样的信息需要考究下。

既然是扫描,我们能够想到的是从左至右按x轴方向以铅垂线的方式扫,对于平面中的这些圆,相对顺序与答案无关,所以不妨按照x轴排个序再思考问题。

考虑两个圆的情况,无非两种情况,要么相离,要么包含,如果包含,答案只可能是最左侧的圆。

alt text

考虑三个的情况,如果两个圆被一个大圆包含,如下:
alt text

那么根据扫描算法,大圆一定最先被检测到,记录之,而第二圆是左侧的小圆,是否要记录下来呢?其实它被一个更大的圆所包含,所以对于第三个圆来说,该圆是冗余的,它的信息完全无用,所以可以直接忽略。

如果还不够形象,继续看图:
alt text

对于第三个圆,如果都在大圆内部,只会出现上述两种情况,但不管上述哪两种情况,第二个圆对于第三个圆来说都是无用的,有一个更大的圆包含着它。

于是,在记录信息时,如果一个圆被更大的圆包含时,则完全不需要把此圆记录下来,相反没有被包含时,则需记录。

那么如何确定这些最外圆的集合,当前圆跟最外圆集合中的哪几个比较呢?

答案是根据圆心的y坐标排序,选取最外圆集合中最靠近当前圆心的上圆和下圆,为什么咧?

反证法,假设有更远的圆存在,那么近圆一定被这更远的圆所包含,那么自然不会出现在最外圆集合中,推出矛盾,得证。不过这玩意只能靠证明理解么。。。好像是不太直观,但有这思路就行。

或者参考《挑战》吧:
这里写图片描述

所以每个圆最多只会跟集合中的两个圆比较,省去大量的冗余比较,高明啊。

扫描的话,考虑极限情况,每个圆构造两条直线,圆的最左端和最右端,好了,看看代码吧。

代码如下:

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.Collections;import java.util.List;import java.util.StringTokenizer;import java.util.TreeSet;public class Main{    String INPUT = "./data/judge/201709/P2932.txt";    public static void main(String[] args) throws IOException {        new Main().run();    }    static final int MAX_N = 40000 + 16;    class E implements Comparable<E>{        double x;        int id;        E(double x, int id){            this.x  = x;            this.id = id;        }        @Override        public int compareTo(E o) {            return Double.compare(x, o.x);        }        @Override        public boolean equals(Object obj) {            return compareTo((E)(obj)) == 0;        }    }    class C{        double x;        double y;        double r;        C(double r, double x, double y){            this.x = x;            this.y = y;            this.r = r;        }    }    int N;    C[] cs = new C[MAX_N];    E[] xs = new E[2 * MAX_N];    // j 是否包含于 i    boolean inside(int i, int j) {        double dx = cs[i].x - cs[j].x;        double dy = cs[i].y - cs[j].y;        return dx * dx + dy * dy <= cs[i].r * cs[i].r;    }    void solve() {        for (int i = 0; i < N; ++i) {            C c = cs[i];            xs[2 * i]     = new E(c.x - c.r, i);            xs[2 * i + 1] = new E(c.x + c.r, i + N);        }        Arrays.sort(xs, 0, 2 * N);        TreeSet<E> set = new TreeSet<E>();        List<Integer> ans = new ArrayList<Integer>();        for (int i = 0; i < 2 * N; ++i) {            E e = xs[i];            int id = e.id % N;            E crtC = new E(cs[id].y, id);            if (e.id < N) {                E up = set.ceiling(crtC);                E dn = set.floor(crtC);                if (up != null && inside(up.id, id)) continue;                if (dn != null && inside(dn.id, id)) continue;                ans.add(id);                set.add(crtC);            }            else {                set.remove(crtC);            }        }        Collections.sort(ans);        out.println(ans.size());        for (int i = 0; i < ans.size(); ++i) {            out.print((ans.get(i) + 1) + (i + 1 == ans.size() ? "\n" : " "));        }    }    void read() {        N = ni();        for (int i = 0; i < N; ++i) {            cs[i] = new C(nd(), nd(), nd());        }        solve();    }    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);        }    }}

alt text

阅读全文
0 0
原创粉丝点击