|
| 1 | + |
| 2 | +/** |
| 3 | +
|
| 4 | +Binary Search (Eqn Solve) |
| 5 | +========================= |
| 6 | +
|
| 7 | +Demo Problem : LightOj 1043 ( Triangle Partitioning ) |
| 8 | +------------------------------------------------------ |
| 9 | +
|
| 10 | + A |
| 11 | + /\ |
| 12 | + / \ |
| 13 | + D/----\E |
| 14 | + / \ |
| 15 | + / \ |
| 16 | + B----------C |
| 17 | +
|
| 18 | +You are given AB, AC and BC. DE is parallel to BC. |
| 19 | +You are also given the area ratio between ADE and BDEC. You have to find the value of AD. |
| 20 | +
|
| 21 | +Idea |
| 22 | +---- |
| 23 | +
|
| 24 | +AB/AD = AC/AE = BC/DE |
| 25 | +
|
| 26 | +So , AE = (AC*AD)/AB |
| 27 | +and , DE = (BC*AD)/AB |
| 28 | +
|
| 29 | +The area ratio of those two triangles can be calculated as a function of AD . |
| 30 | +So, we can Binary Search over AD |
| 31 | +
|
| 32 | +**/ |
| 33 | + |
| 34 | +/** Which of the favors of your Lord will you deny ?* */ |
| 35 | + |
| 36 | +#include<bits/stdc++.h> |
| 37 | +using namespace std; |
| 38 | + |
| 39 | +double triangleRatio (double ab,double ac,double bc,double ad) |
| 40 | +{ |
| 41 | + double ae=(ac*ad)/ab, de=(bc*ad)/ab,s1,s2; |
| 42 | + |
| 43 | + s1 = (ab+ac+bc)/2.0; |
| 44 | + s2 = (ad+ae+de)/2.0; |
| 45 | + |
| 46 | + double areaBig = sqrt(s1 * (s1-ab) * (s1-ac) * (s1-bc)); |
| 47 | + double areaSmall = sqrt(s2 * (s2-ad) * (s2-ae) * (s2-de)); |
| 48 | + |
| 49 | + double areaTrap = areaBig-areaSmall; |
| 50 | + |
| 51 | + double ratio = areaSmall/areaTrap; |
| 52 | + |
| 53 | + return ratio; |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | +double EPS = 0.00000001; |
| 58 | + |
| 59 | +double BS(double lo,double hi ,double ab,double ac,double bc,double target) |
| 60 | +{ |
| 61 | + double mid; |
| 62 | + |
| 63 | + do |
| 64 | + { |
| 65 | + mid = (hi + lo) / 2.0; |
| 66 | + |
| 67 | + double value = triangleRatio(ab,ac,bc,mid); |
| 68 | + |
| 69 | + if (value>target) hi = mid; |
| 70 | + else lo = mid; |
| 71 | + } |
| 72 | + while ((hi - lo) > EPS); |
| 73 | + |
| 74 | + return mid; |
| 75 | +} |
| 76 | + |
| 77 | +int main() |
| 78 | +{ |
| 79 | + int n; |
| 80 | + scanf("%d",&n); |
| 81 | + |
| 82 | + for(int i=1; i<=n; i++) |
| 83 | + { |
| 84 | + double ab,ac,bc,ratio; |
| 85 | + scanf("%lf %lf %lf %lf",&ab,&bc,&ac,&ratio); |
| 86 | + |
| 87 | + printf("Case %d: %lf\n",i,BS(0,ab,ab,bc,ac,ratio)); |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + return 0; |
| 92 | +} |
0 commit comments