1 / 2
May 2020

here is my code, although its giving correct answers for my test cases, however, I am getting wa’s.
please help.

#include <bits/stdc++.h>
using namespace std;
#define EPS 0.00001
#define PI 3.1415926535

bool dis (int radii[],int n, int f, double r )
{
for(int i=0; i<n; i++)
{
double area = pow(radii[i], 2), temp = r*r;
while( area >= temp)
{
area -= temp; f–;
}
if( f<=0 )
return true;
}
return false;
}

int main()
{
int t;
cin>>t;
while(t–)
{
cout<<fixed<<setprecision(4);
int n, f;
scanf("%d %d", &n , &f);
double res=0;
int radii[n];
for(int i=0; i<n; i++)
scanf("%d", &radii[i]);
sort(radii, radii+n);

    if( n >= f+1 )
    {
        res = PI*radii[n-f-1]*radii[n-f-1];
        cout<<res<<endl; continue;
    }

    double left =0, right = radii[0];
    double mid;
    while ( (right-left) >= EPS)
    {
        mid = left +(right -left)/2;
       
        if( dis(radii, n, f+1, mid))
            left = mid;
        else
            right =mid;
    }
    res =  PI*pow(mid, 2);
    cout<<res<<endl;
}
return 0;

}

  • created

    May '20
  • last reply

    May '20
  • 1

    reply

  • 512

    views

  • 2

    users

I don’t think it is correct for following test case:

1
4 1
1 1 1 100