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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

#define SPBR(w, n) std::cout << (w + 1 == n ? '\\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e8;
const double PI = acos(-1);

using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;

int f(vector<pair<int, int>> &v) {
  sort(ALL(v));
  int ret = INF;
  REP(i, v.size() - 1) {
    if (v[i].second == 0 && v[i + 1].second != 0) {
      chmin(ret, v[i + 1].first - v[i].first);
    }
  }
  return ret;
}
int f(map<int, vector<pair<int, int>>> &m) {
  int ret = INF;
  for (auto p : m) {
    chmin(ret, f(p.second));
  }
  return ret;
}

signed main() {
  int N;
  cin >> N;

  vector<int> x(N), y(N), d(N);
  REP(i, N) {
    char c;
    cin >> x[i] >> y[i] >> c;
    if (c == 'U')
      d[i] = 0;
    if (c == 'R')
      d[i] = 1;
    if (c == 'D')
      d[i] = 2;
    if (c == 'L')
      d[i] = 3;
  }

  int ans = INF;
  REP(i, 4) {
    {
      map<int, vector<pair<int, int>>> m;
      REP(i, N) {
        if (d[i] != 0 && d[i] != 2)
          continue;
        m[x[i]].emplace_back(y[i], d[i]);
      }
      chmin(ans, f(m) * 5);
    }
    {
      map<int, vector<pair<int, int>>> m;
      REP(i, N) {
        if (d[i] != 0 && d[i] != 1)
          continue;
        m[x[i] + y[i]].emplace_back(y[i], d[i]);
      }
      chmin(ans, f(m) * 10);
    }
    REP(i, N) {
      int px = x[i], py = y[i];
      x[i] = py;
      y[i] = -px;
      d[i] = (d[i] + 1) % 4;
    }
  }

  if (ans == INF)
    cout << "SAFE"
         << "\\n";
  else
    cout << ans << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

#define SPBR(w, n) std::cout << (w + 1 == n ? '\\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e8;
const double PI = acos(-1);

using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;

int f(vector<pair<int, int>> &v) {
  sort(ALL(v));
  int ret = INF;
  REP(i, v.size() - 1) {
    if (v[i].second == 0 && v[i + 1].second != 0) {
      chmin(ret, v[i + 1].first - v[i].first);
    }
  }
  return ret;
}
int f(map<int, vector<pair<int, int>>> &m) {
  int ret = INF;
  for (auto p : m) {
    chmin(ret, f(p.second));
  }
  return ret;
}

signed main() {
  int N;
  cin >> N;

  vector<int> x(N), y(N), d(N);
  REP(i, N) {
    char c;
    cin >> x[i] >> y[i] >> c;
    if (c == 'U')
      d[i] = 0;
    if (c == 'R')
      d[i] = 1;
    if (c == 'D')
      d[i] = 2;
    if (c == 'L')
      d[i] = 3;
  }

  int ans = INF;
  REP(i, N) {
    {
      map<int, vector<pair<int, int>>> m;
      REP(i, N) {
        if (d[i] != 0 && d[i] != 2)
          continue;
        m[x[i]].emplace_back(y[i], d[i]);
      }
      chmin(ans, f(m) * 5);
    }
    {
      map<int, vector<pair<int, int>>> m;
      REP(i, N) {
        if (d[i] != 0 && d[i] != 1)
          continue;
        m[x[i] + y[i]].emplace_back(y[i], d[i]);
      }
      chmin(ans, f(m) * 10);
    }
    REP(i, N) {
      int px = x[i], py = y[i];
      x[i] = py;
      y[i] = -px;
      d[i] = (d[i] + 1) % 4;
    }
  }

  if (ans == INF)
    cout << "SAFE"
         << "\\n";
  else
    cout << ans << "\\n";

  return 0;
}