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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const long double pi = 3.141592653589793;

#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, j, n) for (int i = j; i < n; i++)
#define pb push_back
#define mp make_pair
// #define mod 1000000007
#define mod 998244353
#define F first
#define S second

// #define COMMENT

/*

const int NN = 200090;
ll fact[NN];
ll finv[NN];
ll inv[NN];

void inverse_modulao() {
  fact[0] = fact[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;

  for(int i = 2;i < NN;i++) {
    fact[i] = fact[i-1]*i%mod;
    inv[i] = mod-mod/i*inv[mod%i]%mod;
    finv[i] = finv[i-1]*inv[i]%mod;
  }
}

ll nCr(int n, int r) {
  return fact[n]*finv[r]%mod*finv[n-r]%mod;
}

*/

int N, P[259999];
int V[555][555];
int Hk[555][555];

int getx(int val) { return (val - 1) / N; }

int gety(int val) { return (val - 1) % N; }

int getid(int x, int y) { return N * x + y + 1; }

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

void dfs(int x, int y, int val) {
  rep(i, 4) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= N || nx < 0)
      continue;
    if (ny >= N || ny < 0)
      continue;

    if (val < Hk[nx][ny]) {
      Hk[nx][ny] = val;
      dfs(nx, ny, val + V[nx][ny]);
    }
  }
}

int main() {

  // freopen("input.in","r",stdin);
  // freopen("output.out","w",stdout);

  cin >> N;
  rep(i, N * N) cin >> P[i];
  rep(i, N) rep(j, N) {
    V[i][j] = 1;
    Hk[i][j] = min(min(i, j), N - 1 - max(i, j));
  }

  int ans = 0;
  rep(k, N * N) {
    int x = getx(P[k]), y = gety(P[k]);
    // debug(x);debug(y);
    ans += Hk[x][y];
    V[x][y] = 0;
    dfs(x, y, Hk[x][y]);
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const long double pi = 3.141592653589793;

#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, j, n) for (int i = j; i < n; i++)
#define pb push_back
#define mp make_pair
// #define mod 1000000007
#define mod 998244353
#define F first
#define S second

// #define COMMENT

/*

const int NN = 200090;
ll fact[NN];
ll finv[NN];
ll inv[NN];

void inverse_modulao() {
  fact[0] = fact[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;

  for(int i = 2;i < NN;i++) {
    fact[i] = fact[i-1]*i%mod;
    inv[i] = mod-mod/i*inv[mod%i]%mod;
    finv[i] = finv[i-1]*inv[i]%mod;
  }
}

ll nCr(int n, int r) {
  return fact[n]*finv[r]%mod*finv[n-r]%mod;
}

*/

int N, P[259999];
int V[555][555];
int Hk[555][555];

int getx(int val) { return (val - 1) / N; }

int gety(int val) { return (val - 1) % N; }

int getid(int x, int y) { return N * x + y + 1; }

unordered_set<int> vis;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

void dfs(int x, int y, int val) {
  vis.insert(getid(x, y));
  rep(i, 4) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= N || nx < 0)
      continue;
    if (ny >= N || ny < 0)
      continue;
    if (vis.count(getid(nx, ny)) == 1)
      continue;
    if (val < Hk[nx][ny]) {
      Hk[nx][ny] = val;
      dfs(nx, ny, val + V[nx][ny]);
    }
  }
}

int main() {

  // freopen("input.in","r",stdin);
  // freopen("output.out","w",stdout);

  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N;
  rep(i, N * N) cin >> P[i];
  rep(i, N) rep(j, N) {
    V[i][j] = 1;
    Hk[i][j] = min(min(i, j), N - 1 - max(i, j));
  }

  int ans = 0;
  rep(k, N * N) {
    int x = getx(P[k]), y = gety(P[k]);
    // debug(x);debug(y);
    ans += Hk[x][y];
    V[x][y] = 0;
    dfs(x, y, Hk[x][y]);
    vis.clear();
  }
  cout << ans << endl;

  return 0;
}