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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_N = 500;

int grid[MAX_N][MAX_N];

struct Node {
  int y;
  int x;
  int dist;

  Node(int y = -1, int x = -1, int dist = -1) {
    this->y = y;
    this->x = x;
    this->dist = dist;
  }
};

const int DY[4] = {-1, 0, 1, 0};
const int DX[4] = {0, 1, 0, -1};

int main() {
  int N;
  cin >> N;
  vector<int> P;

  for (int i = 0; i < N * N; ++i) {
    int p;
    cin >> p;

    P.push_back(p);
  }

  for (int y = 0; y < N; ++y) {
    for (int x = 0; x < N; ++x) {
      int my = min(y, N - y - 1);
      int mx = min(x, N - x - 1);

      grid[y][x] = min(my, mx);
    }
  }

  int ans = 0;
  bool checked[N][N];
  memset(checked, false, sizeof(checked));

  for (int i = 0; i < N * N; ++i) {
    int p = P[i];
    int y = (p - 1) / N;
    int x = (p - 1) % N;
    int v = grid[y][x];

    checked[y][x] = true;
    ans += v;

    queue<Node> que;

    for (int i = 0; i < 4; ++i) {
      int ny = y + DY[i];
      int nx = x + DX[i];

      if (ny < 0 || nx < 0 || N <= ny || N <= nx)
        continue;

      que.push(Node(ny, nx, v));
    }

    /*
       cout << endl;

       fprintf(stderr, "p: %d\\n", p);
       for (int y = 0; y < N; ++y) {
       for (int x = 0; x < N; ++x) {
       cerr << grid[y][x] << " ";
       }
       cerr << endl;
       }
     */

    while (!que.empty()) {
      Node node = que.front();
      que.pop();

      if (grid[node.y][node.x] <= node.dist)
        continue;

      // fprintf(stderr, "[%d, %d, %d]\\n", node.y, node.x, node.dist);
      grid[node.y][node.x] = min(grid[node.y][node.x], node.dist);

      for (int i = 0; i < 4; ++i) {
        int ny = node.y + DY[i];
        int nx = node.x + DX[i];

        if (ny < 0 || nx < 0 || N <= ny || N <= nx)
          continue;

        if (checked[node.y][node.x]) {
          que.push(Node(ny, nx, node.dist));
        } else {
          que.push(Node(ny, nx, node.dist + 1));
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_N = 500;

struct Node {
  int y;
  int x;
  int dist;

  Node(int y = -1, int x = -1, int dist = -1) {
    this->y = y;
    this->x = x;
    this->dist = dist;
  }
};

const int DY[4] = {-1, 0, 1, 0};
const int DX[4] = {0, 1, 0, -1};
int grid[MAX_N][MAX_N];
bool checked[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
int N;

void dfs(int y, int x, int dist) {
  if (grid[y][x] <= dist)
    return;
  if (visited[y][x])
    return;
  visited[y][x] = true;

  grid[y][x] = max(0, min(grid[y][x], dist));

  for (int i = 0; i < 4; ++i) {
    int ny = y + DY[i];
    int nx = x + DX[i];

    if (ny < 0 || nx < 0 || N <= ny || N <= nx)
      continue;

    if (checked[ny][nx]) {
      dfs(ny, nx, dist);
    } else {
      dfs(ny, nx, dist + 1);
    }
  }
}

int main() {
  cin >> N;
  vector<int> P;

  for (int i = 0; i < N * N; ++i) {
    int p;
    cin >> p;

    P.push_back(p);
  }

  for (int y = 0; y < N; ++y) {
    for (int x = 0; x < N; ++x) {
      int my = min(y, N - y - 1);
      int mx = min(x, N - x - 1);

      grid[y][x] = min(my, mx);
    }
  }

  int ans = 0;
  memset(checked, false, sizeof(checked));

  for (int i = 0; i < N * N; ++i) {
    int p = P[i];
    int y = (p - 1) / N;
    int x = (p - 1) % N;
    int v = grid[y][x];

    checked[y][x] = true;
    ans += v;
    memset(visited, false, sizeof(visited));

    /*
    cout << endl;
    fprintf(stderr, "p: %d\\n", p);
    for (int y = 0; y < N; ++y) {
      for (int x = 0; x < N; ++x) {
        cerr << grid[y][x] << " ";
      }
      cerr << endl;
    }
    */
    dfs(y, x, v - 1);
  }

  cout << ans << endl;

  return 0;
}