বুধবার, ১৫ জুন, ২০১৬

LOJ Bisection + Geometry 1137


Handling Tricky / Corner cases while bisecting is important .
When , some parameters are extremely small 0   ,   -THRESHOLD or too big   +THRESHOLD
It's  a good idea to check them separately to avoid WA s .


Code : -


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define sful(a) scanf("%llu",&a);

#define sfulul(a,b) scanf("%llu %llu",&a,&b);

#define sful2(a,b) scanf("%llu %llu",&a,&b); // A little different

#define sfc(a) scanf("%c",&a);

#define sfs(a) scanf("%s",a);


#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case %d: ",++t) // t initialized 0

#define cCASE(t) cout<<"Case "<<++t<<": ";

#define INF 1000000000   //10e9

#define EPS 1e-9

#define flc fflush(stdout); //For interactive programs , flush while using pf (that's why __c )

using namespace std;
/*
Idea:
Always look into triangles
You need a function F such that
F(h) = L'
Now,idea is Work with radius of the circle
Find R
Then , S = R*theta
and , L' = S
So,from h I need to find R , work with the triangle adjacent to circle
do some replacement and
    (x+h)^2 = x^2 + h^2


acos() function returns in radian ... Wasted some time on this
*/
double L,n,C,S;
double F(double h)
{
    double l = L/2.00;
    double x = ((l*l)-(h*h))/(2.00*h);
    double R = x+h;

    double expr = ( 2.00*(R*R) - (L*L)) /(2.00*R*R) ;

    double angA = acos( expr ); //This is already in radians


    S = R*angA;

    return S;
}
int main()
{
    //write();
    int tc,cas =0;
    sfi(tc);
    while(tc--)
    {


        cin>>L>>n>>C;

        double h_lo = 0.0000000;
        double h_hi = L/2.000000;
        double h_mid;

        double L_p = (1.00+n*C)*L; 
        if(L==0.00 || n==0.00 || C==0.00000) //This was IMPORTANT
        {                         //Without it Got WA
            h_mid = 0.00;
        }
        else
        
        while(h_lo<=h_hi)
        {
            h_mid = (h_hi+h_lo)/2.00000;

            if(fabs(h_hi-h_lo)<EPS)
                break;
            F(h_mid);
            if( S > L_p )
            {
                //too much h
                h_hi = h_mid;

            }
            else
            {
                h_lo = h_mid;

            }




        }


        CASE(cas);
        pf("%.8lf\n",h_mid);

    }

    return 0;
}

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন