Блог пользователя Moham3d_3ssam

Автор Moham3d_3ssam, история, 9 месяцев назад, По-английски

I need a plan to improve myself in DP as i think this topic is hard to me and don't know the main idea of it and when we use DP in general.

Полный текст и комментарии »

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

Автор Moham3d_3ssam, история, 10 месяцев назад, По-английски

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";
}

Полный текст и комментарии »

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится