挑战程序竞赛系列(85):3.6极限情况(2)
2017-09-28 12:56
609 查看
挑战程序竞赛系列(85):3.6极限情况(2)
传送门:POJ 1418: Viva Confetti题意:
礼花:Confetti 是一些大小不一的彩色圆形纸片,人们在派对上、过节时便抛洒它们以示庆祝。落在地上的Confetti会堆叠起来,以至于一部分会被盖住而看不见。给定Confetti的尺寸和位置以及它们的叠放次序,你能计算出有多少Confetti是可以看见的吗?
思路:
此题的确需要丰富的想象力,一开始简单的以为圆心距离小于两圆半径之差即可,其实它只是其中一种覆盖情况。如下图:
实际上,还可以有:
所以按照上述思路肯定会出现漏判的情况,那么该怎么办呢?参考神牛的思路:
如果底层的某个圆上的所有圆弧能够被上层的圆覆盖,则说明该底层圆是不可见的。的确涵盖了几乎所有的情况,但还是有特例哟!比如:
这种情况就需要做特殊处理了,想象一下,如果把最底层的绿色圆变大一些,或者变小一些,必然有些边不能被覆盖到。所以我们需要求出每段圆弧,并在此基础上扩大圆的半径,进行特判。对应代码中,t = -1 和 t = 1的循环。(具体参看代码)
接着分析可见与不可见的圆,因为我们对圆进行了离散化处理,实际是分析每段圆弧是否能找到对应的上层圆将它覆盖,如果在某一段圆弧中,搜遍了所有上层圆,都没能将一条弧覆盖,那么此底层圆必然是可见的。
在搜索底层圆的上层圆时,从上往下盖住的第一个圆也是可见的。
所以我们只需找到第一个盖住底层圆的上层圆即可跳出,如果找不到这样的圆,程序自然找的是它自己,因为自己经过扩张后,总能将自己覆盖。
此处就把上述两种情况合并在一块了,的确高级。
证明:(反证法)
假设第一个盖住底层圆的圆a不可见,那么必然被其上层的圆{c,d,e…}所覆盖,那么必然可以将圆a的弧分成若干段,分别找到最上层的圆{c,d,e…}将其覆盖,而我们知道圆a与底层圆的弧是最小划分单元,矛盾,得证。
弧的离散化:
和上篇博文求弧的思路一致,参考链接:
http://blog.csdn.net/u014688145/article/details/78119535
代码如下:
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.Arrays; import java.util.StringTokenizer; public class Main{ String INPUT = "./data/judge/201709/P1418.txt"; public static void main(String[] args) throws IOException { new Main().run(); } static final double PI = Math.acos(-1); static final double EPS = 5E-13; static final int MAX_N = 102; class P{ double x; double y; P(double x, double y){ this.x = x; this.y = y; } } P[] o = new P[MAX_N]; // 圆心坐标 double[] r = new double[MAX_N]; // 圆半径 boolean[] visible = new boolean[MAX_N]; // 对应圆是否可见 int N; double[] angle = new double[2 * MAX_N]; int tot; double distance(P a, P b) { double dx = a.x - b.x; double dy = a.y - b.y; return Math.sqrt(dx * dx + dy * dy); } double norm(double ang) { while (ang < 0) { ang += 2 * PI; } while (ang > 2 * PI) { ang -= 2 * PI; } return ang; } void solve() { Arrays.fill(visible, false); for (int i = 0; i < N; ++i) { tot = 0; angle[tot++] = 0; angle[tot++] = 2 * PI; for (int j = 0; j < N; ++j) { if (i == j) continue; double d = distance(o[i], o[j]); if (r[i] + r[j] < d || d < r[i] - r[j] || d < r[j] - r[i]) continue; // 包含或者不相交 double phi = Math.atan2(o[j].y - o[i].y, o[j].x - o[i].x); double the = Math.acos((r[i] * r[i] + d * d - r[j] * r[j]) / (2 * r[i] * d)); angle[tot++] = norm(phi - the); angle[tot++] = norm(phi + the); } Arrays.sort(angle, 0, tot); for (int j = 0; j < tot - 1; ++j) { double mid = (angle[j] + angle[j + 1]) / 2; double nx = 0; double ny = 0; for (int t = -1; t < 2; t += 2) { nx = o[i].x + (r[i] + t * EPS) * Math.cos(mid); ny = o[i].y + (r[i] + t * EPS) * Math.sin(mid); P np = new P(nx, ny); int k = 0; for (k = N - 1; k >= 0; --k) { if (distance(np, o[k]) < r[k]) { break; } } if (k != -1) visible[k] = true; } } } int ans = 0; for (int i = 0; i < N; ++i) { if (visible[i]) ans ++; } out.println(ans); } void read() { while (true) { N = ni(); if (N == 0) break; for (int i = 0; i < N; ++i) { o[i] = new P(nd(), nd()); r[i] = 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); } } }
相关文章推荐
- 挑战程序竞赛系列(86):3.6极限情况(3)
- 挑战程序竞赛系列(84):3.6极限情况(1)
- 挑战程序竞赛系列(88):3.6平面扫描(2)
- 挑战程序竞赛系列(95):3.6数值积分(1)
- 挑战程序竞赛系列(89):3.6平面扫描(3)
- 挑战程序竞赛系列(83):3.6计算几何基础
- 挑战程序竞赛系列(87):3.6平面扫描(1)
- 挑战程序竞赛系列(52):4.2 Nim 与 Grundy 数
- 挑战程序竞赛系列(18):3.1查找第k大的值
- 挑战程序竞赛系列(19):3.1最小化第k大的值
- 挑战程序竞赛系列(71):4.7高度数组(1)
- 挑战程序竞赛系列(46):4.1Polya 计数定理(2)
- 挑战程序竞赛系列(9):2.4优先队列
- 挑战程序竞赛系列(56):4.4 双端队列(3)
- 挑战程序竞赛系列(64):4.7字符串上的动态规划(2)
- 挑战程序竞赛系列(39):4.1模运算的世界(2)
- 挑战程序竞赛系列(40):4.1模运算的世界(3)
- 挑战程序竞赛系列(10):2.4并查集
- 挑战程序竞赛系列(55):4.4 双端队列(2)
- 挑战程序竞赛系列(2):2.3优化递推关系式