LightOj 1056 (Olympics)

#lightoj #cp #problem_solving #binary_search #geometry

Idea


Binary Search / Geometry

  • Total Perimeter = 2(s+a) = 2*(r*theta +a) = 2*(sqrt(a^2+b^2)*arctan(b/a) + a)
  • So, we can Binary Search over a
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

/** Which of the favors of your Lord will you deny ? **/

#include<bits/stdc++.h>
using namespace std;

#define LL long long
#define PII pair<int,int>
#define PLI pair<LL,int>
#define LPII pair<LL, pair<int,int> >
#define MP make_pair
#define F first
#define S second
#define LINF LLONG_MAX

#define EPS 1e-11

int main()
{
int tc;
scanf("%d",&tc);

//freopen("LightOj1056.txt","w",stdout);

for(int i=1;i<=tc;i++)
{
int x,y;
char ch;
cin>>x>>ch>>y;

double a,b=(y/(x*1.0))*a;

double target=200; /// 400/2

double lo=0,hi=200,mid=(lo+hi)/2;
a=mid;
b=(y/(x*1.0))*a;

double val = sqrt(a*a+b*b)*atan(b/(a*1.0)) + a;

while(1)
{
if(fabs(val-target)<=EPS)
break;
else if(val>target)
hi=mid;
else if(val<target)
lo=mid;

mid = (lo+hi)/2;
a = mid;
b = (y/(x*1.0))*a;

val = sqrt(a*a+b*b)*atan(b/(a*1.0)) + a;
}

cout<<"Case "<<i<<": "<<fixed<<setprecision(8)<<a<<" "<<fixed<<setprecision(8)<<b<<endl;


}

return 0;

}