挑战程序竞赛系列(87):3.6平面扫描(1)
2017-09-28 22:46
555 查看
挑战程序竞赛系列(87):3.6平面扫描(1)
传送门:POJ 2932: Coneology题意:
平面上有N个两两没有公共点的圆,i号圆的圆心在(xi,yi),半径为ri。求所有最外层的,不被任何圆包含的圆。
此题还给了一个条件,任何两圆都没有公共点,所以要么是包含关系,要么相离,不存在相交的情况。
所以朴素的做法是枚举,而判断一个圆是否包含另一个圆的方法,只需要考虑两个圆心的距离是否小于等于大圆的半径即可。时间复杂度为O(n2)。
此题还可以更快,采用记忆化手段,把先前的访问的信息记录下来,但记录什么样的信息需要考究下。
既然是扫描,我们能够想到的是从左至右按x轴方向以铅垂线的方式扫,对于平面中的这些圆,相对顺序与答案无关,所以不妨按照x轴排个序再思考问题。
考虑两个圆的情况,无非两种情况,要么相离,要么包含,如果包含,答案只可能是最左侧的圆。
考虑三个的情况,如果两个圆被一个大圆包含,如下:
那么根据扫描算法,大圆一定最先被检测到,记录之,而第二圆是左侧的小圆,是否要记录下来呢?其实它被一个更大的圆所包含,所以对于第三个圆来说,该圆是冗余的,它的信息完全无用,所以可以直接忽略。
如果还不够形象,继续看图:
对于第三个圆,如果都在大圆内部,只会出现上述两种情况,但不管上述哪两种情况,第二个圆对于第三个圆来说都是无用的,有一个更大的圆包含着它。
于是,在记录信息时,如果一个圆被更大的圆包含时,则完全不需要把此圆记录下来,相反没有被包含时,则需记录。
那么如何确定这些最外圆的集合,当前圆跟最外圆集合中的哪几个比较呢?
答案是根据圆心的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); } } }
相关文章推荐
- 挑战程序竞赛系列(88):3.6平面扫描(2)
- 挑战程序竞赛系列(89):3.6平面扫描(3)
- 挑战程序竞赛系列(62):4.6平面上的分治法(2)
- 挑战程序竞赛系列(61):4.6平面上的分治法(1)
- 挑战程序竞赛系列(85):3.6极限情况(2)
- 挑战程序竞赛系列(86):3.6极限情况(3)
- 挑战程序竞赛系列(95):3.6数值积分(1)
- 挑战程序竞赛系列(83):3.6计算几何基础
- 挑战程序竞赛系列(84):3.6极限情况(1)
- 挑战程序竞赛系列(91):3.6凸包(2)
- 挑战程序竞赛系列(92):3.6凸包(3)
- 挑战程序竞赛系列(33):POJ 2991 Crane
- 挑战程序竞赛系列(25):3.5最大权闭合图
- 挑战程序竞赛系列(72):4.7高度数组(2)
- 挑战程序竞赛系列(78):4.3 2-SAT(2)
- 挑战程序竞赛系列(49):4.2 推理与动态规划算法(2)
- 挑战程序竞赛系列(54):4.4 双端队列(1)
- 挑战程序竞赛系列(64):4.7字符串上的动态规划(2)
- 挑战程序竞赛系列(66):4.7字符串匹配(1)
- 挑战程序竞赛系列(67):4.7字符串匹配(2)