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

#include <bits/stdc++.h>
#define bug1(x) cout << #x << " = " << x << endl
#define bug2(x, y) cout << #x << " = " << x << " " << #y << " = " << y << endl
#define bug3(x, y, z)                                                          \\
  cout << #x << " = " << x << " " << #y << " = " << y << " " << #z << " = "    \\
       << z << endl
#define bug4(x, y, z, m)                                                       \\
  cout << #x << " = " << x << " " << #y << " = " << y << " " << #z << " = "    \\
       << z << " " << #m << " = " << m << endl

using namespace std;

class Debugger {
public:
  template <typename T>
  void printVector(vector<T> const &vec, bool printSize = true) {
    if (printSize) {
      cout << vec.size() << endl;
    }
    for (auto &elem : vec) {
      cout << elem << " ";
    }
    cout << endl;
  }

  template <typename T> void printMatrix(vector<vector<T>> const &matrix) {
    cout << matrix.size() << " ";
    if (!matrix.empty()) {
      cout << matrix[0].size();
    }
    cout << endl;
    for (auto &vec : matrix) {
      printVector(vec, false);
    }
    cout << endl;
  }
};

const int maxN = 100005;
const int INF = 1e9;

class TaskB {
private:
  Debugger debugger;
  vector<int> dx = {0, 0, 1, -1};
  vector<int> dy = {1, -1, 0, 0};

public:
  void solveOne(int it) {
    int n;
    cin >> n;
    vector<int> vals(n * n);
    for (int i = 0; i < n * n; i++) {
      cin >> vals[i];
    }
    vector<vector<int>> dist(n, vector<int>(n, INF));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dist[i][j] = min(min(i, n - i - 1), min(j, n - j - 1));
      }
    }

    vector<vector<int>> cost(n, vector<int>(n, 1));
    int ans = 0;
    for (int i = 0; i < n * n; i++) {
      vals[i]--;
      int x = vals[i] / n;
      int y = vals[i] % n;
      ans += dist[x][y];
      cost[x][y] = 0;
      deque<pair<int, int>> dq;
      dq.push_front({x, y});
      while (!dq.empty()) {
        x = dq.front().first;
        y = dq.front().second;
        dq.pop_front();
        for (int dir = 0; dir < 4; dir++) {
          int xnew = dx[dir] + x;
          int ynew = dy[dir] + y;
          if (xnew >= n or xnew < 0 or ynew >= n or ynew < 0) {
            continue;
          }
          if (cost[x][y] + dist[x][y] < dist[xnew][ynew]) {
            dist[xnew][ynew] = cost[x][y] + dist[x][y];
            if (cost[xnew][ynew] == 0) {
              dq.push_front({xnew, ynew});
            } else {
              dq.push_back({xnew, ynew});
            }
          }
        }
      }
    }

    cout << ans << endl;
  }

  void solve() {
    int tc = 1;
    for (int it = 1; it <= tc; it++) {
      solveOne(it);
    }
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  TaskB solver;
  solver.solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define bug1(x) cout << #x << " = " << x << endl
#define bug2(x, y) cout << #x << " = " << x << " " << #y << " = " << y << endl
#define bug3(x, y, z)                                                          \\
  cout << #x << " = " << x << " " << #y << " = " << y << " " << #z << " = "    \\
       << z << endl
#define bug4(x, y, z, m)                                                       \\
  cout << #x << " = " << x << " " << #y << " = " << y << " " << #z << " = "    \\
       << z << " " << #m << " = " << m << endl

using namespace std;

class Debugger {
public:
  template <typename T>
  void printVector(vector<T> const &vec, bool printSize = true) {
    if (printSize) {
      cout << vec.size() << endl;
    }
    for (auto &elem : vec) {
      cout << elem << " ";
    }
    cout << endl;
  }

  template <typename T> void printMatrix(vector<vector<T>> const &matrix) {
    cout << matrix.size() << " ";
    if (!matrix.empty()) {
      cout << matrix[0].size();
    }
    cout << endl;
    for (auto &vec : matrix) {
      printVector(vec, false);
    }
    cout << endl;
  }
};

const int maxN = 100005;

class TaskB {
private:
  Debugger debugger;
  vector<int> dx = {0, 0, 1, -1};
  vector<int> dy = {1, -1, 0, 0};

public:
  void solveOne(int it) {
    int n;
    cin >> n;
    vector<int> vals(n * n);
    for (int i = 0; i < n * n; i++) {
      cin >> vals[i];
    }

    reverse(vals.begin(), vals.end());

    vector<vector<int>> cost(n, vector<int>(n, 0));
    int total = 0;
    for (int i = 0; i < n * n; i++) {
      vals[i]--;
      int x = vals[i] / n;
      int y = vals[i] % n;

      deque<pair<int, int>> dq;
      dq.push_front({x, y});
      const int INF = 1e9;
      vector<vector<int>> dist(n, vector<int>(n, INF));
      dist[x][y] = 0;
      int ans = -1;
      while (!dq.empty() and ans == -1) {
        x = dq.front().first;
        y = dq.front().second;
        dq.pop_front();
        for (int dir = 0; dir < 4; dir++) {
          int xnew = dx[dir] + x;
          int ynew = dy[dir] + y;
          if (xnew >= n or xnew < 0 or ynew >= n or ynew < 0) {
            ans = dist[x][y];
            break;
          }
          if (cost[xnew][ynew] + dist[x][y] < dist[xnew][ynew]) {
            dist[xnew][ynew] = cost[xnew][ynew] + dist[x][y];
            if (cost[xnew][ynew] == 0) {
              dq.push_front({xnew, ynew});
            } else {
              dq.push_back({xnew, ynew});
            }
          }
        }
      }
      x = vals[i] / n;
      y = vals[i] % n;
      cost[x][y] = 1;
      total += ans;
    }

    cout << total << endl;
  }

  void solve() {
    int tc = 1;
    for (int it = 1; it <= tc; it++) {
      solveOne(it);
    }
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  TaskB solver;
  solver.solve();
  return 0;
}