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

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define LFOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define LRFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int main(void) {

  while (1) {
    int r;
    int n;
    int xl, xr, h;
    int dp[41] = {};
    double dph[41] = {};
    double mini = 20.0;
    double minihigh = 20.0;

    cin >> r >> n;
    if (n == 0 && r == 0) {
      break;
    }

    FOR(i, 1, n) {
      cin >> xl >> xr >> h;

      FOR(j, xl, xr - 1) {
        if (dp[j + 20] < h) {
          dp[j + 20] = h;
        }
      }
    }

    /*for (int i = -r; i <= r-1; i++) {
            cout << i << " " << dp[i+20] << endl;
    }*/

    for (int i = -r; i <= r; i++) {
      double rr, xx, yy;
      if (i < 0) {
        rr = (double)r;
        xx = (double)(i + 1);
        yy = sqrt(rr * rr - xx * xx);
        dph[i + 20] = dp[i + 20] + r - yy;
      }
      if (i == 0) {
        rr = (double)r;
        xx = (double)(i);
        yy = sqrt(rr * rr - xx * xx);
        dph[i + 20] = dp[i + 20] + r - yy;
      }
      if (i > 0) {
        rr = (double)r;
        xx = (double)(i);
        yy = sqrt(rr * rr - xx * xx);
        dph[i + 20] = dp[i + 20] + r - yy;
      }
    }

    // cout << endl;
    for (int i = -r; i <= r - 1; i++) {
      // cout << i << " " << dph[i + 20] << endl;
      if (dph[i + 20] < mini) {
        mini = dph[i + 20];
      }
    }

    printf("%.5lf\\n", max(mini, 0.0));
    cout << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define LFOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define LRFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int main(void) {

  while (1) {
    double r;
    double center;
    double high;
    double answer = 20.0;
    double maxhigh = 0.0;
    double smallhigh = 0.0;
    int flag;
    int n;
    double xl[21] = {}, xr[21] = {}, h[21] = {};

    cin >> r >> n;

    if (n == 0) {
      break;
    }

    FOR(i, 1, n) {
      cin >> xl[i] >> xr[i] >> h[i];
      if (h[i] > maxhigh) {
        maxhigh = h[i];
      }
      if (xl[i] <= -r && xr[i] >= r && smallhigh < h[i]) {
        smallhigh = h[i];
      }
    }

    answer = maxhigh;

    for (double i = smallhigh; i <= maxhigh; i += 0.0001) {
      center = -r + i;

      for (double j = 0; j <= r; j += 0.0001) {
        high = sqrt(r * r - j * j) + center;

        // cout << center << " " << high << endl;
        flag = 0;
        FOR(k, 1, n) {
          if (xl[k] <= j && xr[k] >= j && high <= h[k]) {
            switch (flag) {
            case 0:
              flag = 1;
              break;
            case 2:
              flag = 3;
              break;
            }
          }
          if (xl[k] <= -j && xr[k] >= -j && high <= h[k]) {
            switch (flag) {
            case 0:
              flag = 2;
              break;
            case 1:
              flag = 3;
              break;
            }
          }

          if (high <= 0) {
            break;
          }
          if (flag == 3) {
            break;
          }
          if (k == n) {
            answer = i - 0.0001;
            // cout << j << " " << high << endl;
          }
        }
        if (answer != maxhigh) {
          break;
        }
      }
      if (answer != maxhigh) {
        break;
      }
    }

    printf("%.5lf\\n", answer);
  }

  return 0;
}