Help in this question

Revision en5, by Moham3d_3ssam, 2025-07-09 20:35:47

I use ternary search but the problem is that they are some invalid values in range [0:k] or in f(x), How can i deal with these invalid values?

This is the problem: 102881B - Anany in the Army

My steps:

I will brute force in value where i will make ternary search on each value of a, b, c then take the max area. The problem is when i add value from range [0:k] to any side, This may result that the triangle isn't right.

This is my Code:

long double f(long double a, long double b, long double c){
  if((a + b) <= c || (a + c) <= b || (b + c) <= a) return NINF;
  long double s = ((a + b + c) / 2);
  return sqrt(s * (s - a) * (s - b) * (s - c));
}

long double ts(long double a, long double b, long double c, long double k){
  long double l = 0, r = k;
  long double maxVal = 0;
  while(r - l > 1e-9){
    long double mid = ((l + r) / 2);
    if(f(a + mid, b, c) < f(a + mid + 1e-7, b, c)){
      l = mid;
    }else{
      r = mid;
    }
  }

  return f(a + r, b, c);
}

void solve(){
  long double a, b, c, k;
  read(a, b, c, k);


  cout << setprecision(10) << max({ts(a, b, c, k), ts(b, a, c, k), ts(c, a, b, k)}) << "\n";
}

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en5 English Moham3d_3ssam 2025-07-09 20:35:47 1 Tiny change: 'sn't right\n\n\n### ' -> 'sn't right.\n\n\n### '
en4 English Moham3d_3ssam 2025-07-09 07:39:18 4 Tiny change: 'n\n#### That is the pr' -> 'n\n#### This is the pr'
en3 English Moham3d_3ssam 2025-07-09 01:09:16 4
en2 English Moham3d_3ssam 2025-07-09 01:08:40 5 Tiny change: 'al with that invalid v' -> 'al with these invalid v'
en1 English Moham3d_3ssam 2025-07-09 00:30:02 1212 Initial revision (published)