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

// Beginner160.cpp : This file contains the 'main' function. Program execution
// begins and ends there.
//

#include <bits/stdc++.h>
#include <iostream>

using namespace std;
typedef long long ll;

int main() {

  // A
  /*string s;
  cin >> s;
  cout << (s[2] == s[3] && s[4] == s[5] ? "Yes" : "No") << '\\n';*/

  // B
  /*int x;
  cin >> x;

  int y500 = x / 500;
  x -= y500 * 500;
  int y5 = x / 5;

  ll resut = y500 * 1000 + y5 * 5;

  cout << resut  << '\\n';*/

  // C
  /*int k, n;
  cin >> k >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
          cin >> a[i];


  ll result = 0;
  int max_dist = -1;
  int max_dist_i = -1;
  for (int i =0; i < n-1; ++i)
  {
          int dist = a[i + 1] - a[i];
          if (dist > max_dist)
          {
                  max_dist = dist;
                  max_dist_i = i;
          }
  }

  int last_dist = k - a[n - 1] + a[0];
  if (last_dist > max_dist)
  {
          max_dist = last_dist;
          max_dist_i = n - 1;
  }

  for (int i = 0; i < n; ++i)
  {
          if (i == max_dist_i) continue;
          if (i == n - 1) result += last_dist;
          else result += a[i + 1] - a[i];
  }

  cout << result << '\\n';*/

  // D
  int n, x, y;
  cin >> n >> x >> y;

  /*vector<vector<int>> dist(n+1, vector<int>(n+1));
  for (int i = 1; i <= n; ++i)
  {
          for (int j = i + 1; j <= n; ++j)
          {
                  dist[i][j] = abs(i - j);
          }
  }*/

  /*dist[x][y] = 0;
  dist[y][x] = 0;*/

  /*map<int, vector<int>> children;
  for (int i = 1; i < n;++i)
  {
          children[i].push_back(i + 1);
  }

  int min_x_y = min(x, y);
  int max_x_y = max(x, y);
  children[min_x_y].push_back(max_x_y);

  map<int, int> distances;*/

  map<int, map<int, int>> distances;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      distances[i][j] = abs(i - j);
    }
  }

  distances[x][y] = 1;
  distances[y][x] = 1;

  int counter = 1;
  int min_x_y = min(x, y);
  int max_x_y = max(x, y);

  /*while (min_x_y - counter >= 0 || max_x_y + counter <= n || min_x_y + counter
  < max_x_y - counter)
  {
          if (min_x_y - counter >= 0)
          {
                  distances[min_x_y - counter][max_x_y] = min(distances[min_x_y
  - counter][max_x_y], counter + distances[x][y]); distances[max_x_y][min_x_y -
  counter] = distances[min_x_y - counter][max_x_y];
          }

          if (max_x_y + counter <= n)
          {
                  distances[max_x_y + counter][min_x_y] = min(distances[max_x_y
  + counter][min_x_y], counter + distances[x][y]); distances[min_x_y][max_x_y +
  counter] = distances[max_x_y + counter][min_x_y];
          }
          counter++;
  }*/

  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      if (i == j)
        continue;
      /*distances[i][j] = min(distances[i][j], distances[i][x] + 1 +
      distances[y][j]); distances[i][j] = min(distances[i][j], distances[i][y] +
      1 + distances[x][j]);*/
      if (i <= min_x_y && j <= min_x_y || i > max_x_y && j > max_x_y)
        continue;
      if (i <= min_x_y && j >= max_x_y)
        distances[i][j] = distances[i][min_x_y] + 1 + distances[max_x_y][j];
      else
        distances[i][j] = min(distances[i][j], distances[i][min_x_y] + 1 +
                                                   distances[max_x_y][j]);
    }
  }

  vector<int> result(n + 1);
  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      int res = distances[i][j];
      result[res]++;
    }
  }

  for (int k = 1; k < n; ++k) {
    cout << result[k] << '\\n';
  }

  // BFS
  /*vector<bool> visited(n + 1, false);
  queue<int> q;
  q.push(1);
  visited[1] = true;


  int depth = 0;

  while (!q.empty())
  {
          int q_next = q.front();
          q.pop();

          for (int i = 0; i < children[q_next].size(); ++i)
          {
                  int child = children[q_next][i];
                  if (visited[child]) continue;

                  visited[child] = true;
                  q.push(child);

                  for (int d = 0; d < depth; ++d)
                  {

                  }
          }
  }*/

  /*vector<int> res(n);
  for (int k = 1; k<n; ++k)
  {
          res[k] = n - k;
  }

  int dxy = abs(y - x);
  for (int k = 1; k <n;++k)
  {
          if (k <= dxy) res[k]++;
          else res[k]--;
  }

  for (int k = 1; k < n; ++k)
  {
          cout << res[k] << '\\n';
  }*/
}


Error: TLE

Fixed submission:

// Beginner160.cpp : This file contains the 'main' function. Program execution
// begins and ends there.
//

#include <bits/stdc++.h>
#include <iostream>

using namespace std;
typedef long long ll;

int main() {

  // A
  /*string s;
  cin >> s;
  cout << (s[2] == s[3] && s[4] == s[5] ? "Yes" : "No") << '\\n';*/

  // B
  /*int x;
  cin >> x;

  int y500 = x / 500;
  x -= y500 * 500;
  int y5 = x / 5;

  ll resut = y500 * 1000 + y5 * 5;

  cout << resut  << '\\n';*/

  // C
  /*int k, n;
  cin >> k >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
          cin >> a[i];


  ll result = 0;
  int max_dist = -1;
  int max_dist_i = -1;
  for (int i =0; i < n-1; ++i)
  {
          int dist = a[i + 1] - a[i];
          if (dist > max_dist)
          {
                  max_dist = dist;
                  max_dist_i = i;
          }
  }

  int last_dist = k - a[n - 1] + a[0];
  if (last_dist > max_dist)
  {
          max_dist = last_dist;
          max_dist_i = n - 1;
  }

  for (int i = 0; i < n; ++i)
  {
          if (i == max_dist_i) continue;
          if (i == n - 1) result += last_dist;
          else result += a[i + 1] - a[i];
  }

  cout << result << '\\n';*/

  // D
  int n, x, y;
  cin >> n >> x >> y;

  /*vector<vector<int>> dist(n+1, vector<int>(n+1));
  for (int i = 1; i <= n; ++i)
  {
          for (int j = i + 1; j <= n; ++j)
          {
                  dist[i][j] = abs(i - j);
          }
  }*/

  /*dist[x][y] = 0;
  dist[y][x] = 0;*/

  /*map<int, vector<int>> children;
  for (int i = 1; i < n;++i)
  {
          children[i].push_back(i + 1);
  }

  int min_x_y = min(x, y);
  int max_x_y = max(x, y);
  children[min_x_y].push_back(max_x_y);

  map<int, int> distances;*/

  map<int, map<int, int>> distances;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      distances[i][j] = abs(i - j);
    }
  }

  distances[x][y] = 1;
  distances[y][x] = 1;

  int counter = 1;
  int min_x_y = min(x, y);
  int max_x_y = max(x, y);

  /*while (min_x_y - counter >= 0 || max_x_y + counter <= n || min_x_y + counter
  < max_x_y - counter)
  {
          if (min_x_y - counter >= 0)
          {
                  distances[min_x_y - counter][max_x_y] = min(distances[min_x_y
  - counter][max_x_y], counter + distances[x][y]); distances[max_x_y][min_x_y -
  counter] = distances[min_x_y - counter][max_x_y];
          }

          if (max_x_y + counter <= n)
          {
                  distances[max_x_y + counter][min_x_y] = min(distances[max_x_y
  + counter][min_x_y], counter + distances[x][y]); distances[min_x_y][max_x_y +
  counter] = distances[max_x_y + counter][min_x_y];
          }
          counter++;
  }*/

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (i == j)
        continue;
      distances[i][j] =
          min(distances[i][j], distances[i][x] + 1 + distances[y][j]);
      distances[i][j] =
          min(distances[i][j], distances[i][y] + 1 + distances[x][j]);
    }
  }

  vector<int> result(n + 1);
  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      int res = distances[i][j];
      result[res]++;
    }
  }

  for (int k = 1; k < n; ++k) {
    cout << result[k] << '\\n';
  }

  // BFS
  /*vector<bool> visited(n + 1, false);
  queue<int> q;
  q.push(1);
  visited[1] = true;


  int depth = 0;

  while (!q.empty())
  {
          int q_next = q.front();
          q.pop();

          for (int i = 0; i < children[q_next].size(); ++i)
          {
                  int child = children[q_next][i];
                  if (visited[child]) continue;

                  visited[child] = true;
                  q.push(child);

                  for (int d = 0; d < depth; ++d)
                  {

                  }
          }
  }*/

  /*vector<int> res(n);
  for (int k = 1; k<n; ++k)
  {
          res[k] = n - k;
  }

  int dxy = abs(y - x);
  for (int k = 1; k <n;++k)
  {
          if (k <= dxy) res[k]++;
          else res[k]--;
  }

  for (int k = 1; k < n; ++k)
  {
          cout << res[k] << '\\n';
  }*/
}