Header Ad

HackerEarth Ratio - TIE BREAKER problem solution

In this HackerEarthRatio - TIE BREAKER problem solution You are given N ratios in the form of A and B that is represented as A/B. The values of A and B are represented as double data type values. The values of B are incorrect. The actual values of B are B + R. You know the actual sum of all the ratios that is available in variable K.


HackerEarth Ratio - TIE BREAKER problem solution


HackerEarth Ratio - TIE BREAKER problem solution.

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll n;
double c,t,lo,hi,mid,d[1005],s[1005];
int main()
{
freopen("samp.txt","r",stdin);
freopen("sout.txt","w",stdout);
ll i,j,k=0;
cin>>n;
lo=-1000000.0;
for(i=1;i<=n;i++)
{
cin>>d[i]>>s[i];
lo=max(lo,-1.0*s[i]);
}
cin>>t;
hi=10000000.0;
k=0;
while(k<=5000&&lo<=hi)
{
mid=(lo+hi)/2.0;
k++;
double cur=0.0;
for(i=1;i<=n;i++)
{
cur+=(d[i]/(s[i]+mid+0.0));
}
if(cur>=t)
lo=mid;
else
hi=mid;
}
cout<<fixed<<setprecision(8)<<mid;
return 0;
}

Second solution

#include<bits/stdc++.h>
#define eps 0.000001
using namespace std;
int n;
double a[1005],b[1005];
double f(double r)
{
double val=0.0;
for(int i=0;i<n;i++)
val+=a[i]/(b[i]+r);
return val;
}
int main()
{
int col;
cin>>n>>col;
assert(n>=1 && n<=1000);
assert(col==2);
double l=-2000.0,r=2000.0;
for(int i=0;i<n;i++)
{
cin>>a[i]>>b[i];
assert(a[i]>=1 && a[i]<=1000);
assert(abs(b[i])<=1000);
l=max(l,-1.0*b[i]);
}
double k,ans=0;l+=1e-7;
cin>>k;
assert(k>=1 && k<=1e6);
while(l<=r)
{
double mid=(l+r)/2.0;
double val=f(mid);
if(val>=k){ans=mid;l=mid+1e-7;}
else if(val<k) {r=mid-1e-7;}
}
cout<<fixed<<setprecision(7)<<ans<<"\n";
return 0;
}


Post a Comment

0 Comments