Problem p00775 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int build[20][40];

void printbuild() {
  for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 40; j++) {
      if (build[i][j])
        cout << "#";
      else
        cout << ".";
    }
    cout << endl;
  }
}

void makeOut() {
  for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 40; j++) {
      build[i][j] = 1 - build[i][j];
    }
  }
}

bool touchingSun(long h, long i, double c, long r) {
  return hypot(19 - h - c, i - 20) < r || hypot(19 - h - c, i - 19) < r;
}

bool touchingSunAll(double c, long r) {
  for (int h = 0; h < 20; h++) {
    for (int i = 0; i < 40; i++) {
      if (build[h][i] && touchingSun(h, i, c, r)) {
        return true;
      }
    }
  }
  return false;
}

void reset() {
  for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 40; j++) {
      build[i][j] = 0;
    }
  }
}

void fillbuild(long l, long r, long h) {
  for (int i = 19; i >= 20 - h; i--) {
    for (int j = l + 20; j < r + 20; j++) {
      build[i][j] = 1;
    }
  }
}

void solve(long n, long r) {
  reset();
  long l, rr, h;
  for (int i = 0; i < n; i++) {
    cin >> l >> rr >> h;
    fillbuild(l, rr, h);
  }
  makeOut();
  // printbuild();
  double c = -r;
  double step = 0.5;
  int cnt = 0;
  while (step > 0.00001) {
    if (!touchingSunAll(c, r)) {
      c += step;
    } else {
      c -= step;
      step /= 2;
    }
    cnt++;
    if (cnt > 10000)
      break;
  }
  printf("%.4f\\n", min(c + r, 20.0));
}

int main() {
  long n, r;
  while (1) {
    cin >> r >> n;
    if (n + r) {
      solve(n, r);
    } else {
      return 0;
    }
  }
}


Error: TLE

Fixed submission:

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

int build[20][40];

void printbuild() {
  for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 40; j++) {
      if (build[i][j])
        cout << "#";
      else
        cout << ".";
    }
    cout << endl;
  }
}

void makeOut() {
  for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 40; j++) {
      build[i][j] = 1 - build[i][j];
    }
  }
}

bool touchingSun(long h, long i, double c, long r) {
  return hypot(19 - h - c, i - 20) < r || hypot(19 - h - c, i - 19) < r;
}

bool touchingSunAll(double c, long r) {
  for (int h = 0; h < 20; h++) {
    for (int i = 0; i < 40; i++) {
      if (build[h][i] && touchingSun(h, i, c, r)) {
        return true;
      }
    }
  }
  return false;
}

void reset() {
  for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 40; j++) {
      build[i][j] = 0;
    }
  }
}

void fillbuild(long l, long r, long h) {
  for (int i = 19; i >= 20 - h; i--) {
    for (int j = l + 20; j < r + 20; j++) {
      build[i][j] = 1;
    }
  }
}

void solve(long n, long r) {
  reset();
  long l, rr, h;
  for (int i = 0; i < n; i++) {
    cin >> l >> rr >> h;
    fillbuild(l, rr, h);
  }
  makeOut();
  // printbuild();
  double c = -r;
  double step = 0.5;
  while (step > 0.00001) {
    if (!touchingSunAll(c, r)) {
      c += step;
    } else {
      c -= step;
      step /= 2;
    }
  }
  printf("%.4f\\n", c + r);
}

int main() {
  long n, r;
  while (1) {
    cin >> r >> n;
    if (n + r) {
      solve(n, r);
    } else {
      return 0;
    }
  }
}