Prob_Solve 板


LINE

題目敘述網址: http://code.google.com/codejam/contest/204113/dashboard#s=p3&a=3 官方解答說明: http://code.google.com/codejam/contest/204113/dashboard#s=a&a=3 看了當初參賽者上傳的 code 才知道 large dataset 怎麼解. 想要請教版上高手的是, 當遇到需要用到浮點數的運算時, 要如何決定合理的 epsilon 值呢? 以這題為例, 在判斷某一個 midR 是否為一個足夠大的半徑時, 可能的判斷方式為: for (int i = 0; i < candidate.size() && !valid; i++) for (int j = i; j < candidate.size() && !valid; j++) { Point c1 = candidate.get(i); Point c2 = candidate.get(j); valid = true; for (Circle p : plant) { double epsilon = 1e-10; if (midR + epsilon < p.radius) {valid = false; break;} if (c1.distanceSquare(p.center) > Math.pow((midR + epsilon - p.radius), 2)) if (c2.distanceSquare(p.center) > Math.pow((midR + epsilon - p.radius), 2)) {valid = false; break;} } } 以實驗的結果來說, epsilon 的值設為 1e-4 ~ 1e-13 跑出來的結果都正確, 但設為多少比較合理呢? [小弟練習的 implementation in Java] import java.io.*; import java.util.*; class D { private static BufferedReader input; private static BufferedWriter output; public static void main(String[] args) { try { input = new BufferedReader(new FileReader(args[0] + ".in")); output = new BufferedWriter(new FileWriter(args[0] + ".out")); String line = input.readLine(); int testcases = getInt(line, 0); for (int testcase = 1; testcase <= testcases; testcase++) { line = input.readLine(); int N = getInt(line, 0); Circle[] plant = new Circle[N]; for (int i = 0; i < N; i++) { line = input.readLine(); plant[i] = new Circle(getInt(line, 0), getInt(line, 1), getInt(line, 2)); } double minR = 0, maxR = 10000; while ((maxR - minR) > 1e-6) { double midR = (maxR + minR) / 2; boolean valid = false; ArrayList<Point> candidate = new ArrayList<Point>(); for (Circle c : plant) {candidate.add(c.center);} for (int i = 0; i < N; i++) for (int j = i + 1; j < N; j++) { Circle c1 = new Circle(plant[i].center, midR - plant[i].radius); Circle c2 = new Circle(plant[j].center, midR - plant[j].radius); for (Point p : c1.intersectPoints(c2)) {candidate.add(p);} } for (int i = 0; i < candidate.size() && !valid; i++) for (int j = i; j < candidate.size() && !valid; j++) { Point c1 = candidate.get(i); Point c2 = candidate.get(j); valid = true; for (Circle p : plant) { double epsilon = 1e-10; if (midR + epsilon < p.radius) {valid = false; break;} if (c1.distanceSquare(p.center) > Math.pow((midR + epsilon - p.radius), 2)) if (c2.distanceSquare(p.center) > Math.pow((midR + epsilon - p.radius), 2)) {valid = false; break;} } } if (valid) {maxR = midR;} else {minR = midR;} } String result = "Case #" + testcase + ": " + minR; output(result); } input.close(); output.close(); } catch (Exception e) { e.printStackTrace(); } } public static int getInt(String line, int index) {return Integer.parseInt(getString(line, index));} public static String getString(String line, int index) { line = line.trim(); while (index > 0) {line = line.substring(line.indexOf(' ') + 1); index--;} if ((-1) == line.indexOf(' ')) {return line;} else {return line.substring(0, line.indexOf(' '));} } public static void output(String s) throws Exception { System.out.println(s); output.write(s); output.newLine(); } } class Point { public final double x, y; public Point(double x, double y) {this.x = x; this.y = y;} public double distanceSquare(Point another) {return (Math.pow((this.x - another.x), 2) + Math.pow((this.y - another.y), 2));} public double distance(Point another) {return Math.hypot((this.x - another.x), (this.y - another.y));} } class Circle { public final Point center; public final double radius; public Circle(Point c, double r) {center = c; radius = r;} public Circle(double x, double y, double r) {center = new Point(x, y); radius = r;} public Point[] intersectPoints(Circle another) { Point c0 = this.center; Point c1 = another.center; double r0 = this.radius; double r1 = another.radius; double d = c0.distance(c1); if (c0.x == c1.x && c0.y == c1.y) {return new Point[0];} if (d > r0 + r1) {return new Point[0];} if ((d + r0) < r1) {return new Point[0];} if ((d + r1) < r0) {return new Point[0];} // http://paulbourke.net/geometry/2circle/ double a = (r0 * r0 - r1 * r1 + d * d) / (2 * d); double h = Math.sqrt(r0 * r0 - a * a); double x2 = c0.x + a * (c1.x - c0.x) / d; double y2 = c0.y + a * (c1.y - c0.y) / d; Point[] intersects = new Point[2]; intersects[0] = new Point(x2 + h * (c1.y - c0.y) / d, y2 - h * (c1.x - c0.x) / d); intersects[1] = new Point(x2 - h * (c1.y - c0.y) / d, y2 + h * (c1.x - c0.x) / d); return intersects; } } --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.126.30.95
1F:推 DJWS:比大小的時候通常不需要eps 因為誤差加誤差就是更大的誤差 05/04 22:07
2F:→ DJWS:判斷等於的時候可以設eps 因為一定有誤差導致等於變成不等於 05/04 22:08
3F:→ DJWS:eps大小則是根據浮點數運算誤差決定 double可以存個15位數 05/04 22:11
4F:→ DJWS:對於一般的問題來說足夠精準 eps意思意思設個數字就可以了 05/04 22:14
5F:→ RockLee:感謝 D 大的經驗分享~ 05/06 17:16
6F:→ RockLee:順便分享一下我在「培養與鍛鍊程式設計的邏輯腦」中 05/06 17:16
7F:→ RockLee:看到的作者的經驗分享:(以下摘錄自該書 P260) 05/06 17:17
8F:→ RockLee:通常在比較包含誤差的浮動小數點值時, 05/06 17:17
9F:→ RockLee:都必須使用適當小的數 EPS,寫成下面這樣 05/06 17:17
10F:→ RockLee:a < b → a + EPS < b 05/06 17:18
11F:→ RockLee:a <= b → a < b + EPS 05/06 17:18
12F:→ RockLee:a == b → abs(a - b) < EPS 05/06 17:18







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Tech_Job站內搜尋

TOP