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

#include <bits/stdc++.h>

using namespace std;

// int func(int kyori);
// tikamiti -> x->yの道を使ったらtrue;
// int w_func(int start,int kyori,bool tikamiti,int saisyo);

int serch(int num);
int n, x, y;
int dp[2001][2001];

int main(void) {
  cin >> n >> x >> y;
  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < n + 1; j++) {
      if (i <= j)
        dp[i][j] = j - i;
      else
        dp[i][j] = i - j;
    }
  }
  for (int i = 1; i < n + 1; i++) {
    if (dp[i][y] > dp[i][x] + 1)
      dp[i][y] = dp[i][x] + 1;
  }

  int ans[n + 1];
  for (int i = 0; i < n + 1; i++)
    ans[i] = 0;
  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < n + 1; j++) {
      if (i == j)
        dp[i][j] = 0;
      else if (i > j) {
        dp[i][j] = dp[j][i];
      } else {
        int migi = j;
        int hidari = j;
        while (dp[i][migi] > dp[i][migi - 1] + 1) {
          dp[i][migi] = dp[i][migi - 1] + 1;
          migi++;
          if (migi == n)
            break;
        }
        while (dp[i][hidari] > dp[i][hidari + 1] + 1 && hidari < n) {
          // cout<<"hidari"<<endl;
          // cout<<"i = "<<i<<", hidari = "<<hidari<<endl;
          dp[i][hidari] = dp[i][hidari + 1] + 1;
          hidari--;
          if (hidari == 1)
            break;
        }
      }
    }
  }
  for (int i = 1; i < n + 1; i++) {
    for (int j = i; j < n + 1; j++) {
      ans[dp[i][j]]++;
    }
  }
  /*
  cout<<"\\n"<<endl;
  cout<<"dp"<<endl;
  for(int i = 1; i < n+1; i++){
          for(int j = 1; j < n+1; j++){
                  cout<<dp[i][j]<<" ";
          }
          cout<<endl;
  }
  */
  for (int i = 1; i < n; i++) {
    cout << ans[i] << endl;
  }

  /*
  int ans[n+1];
  for(int i = 1; i < n; i++){
          ans[i] = func(i);
  }
  for(int i = 1; i < n; i++){
          cout<<"i = "<<i <<", func(i) = "<<ans[i]<<endl;
          cout<<endl;
  }
  */
  return 0;
}

int serch(int num) {
  int re = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      if (dp[i][j] == num)
        re++;
    }
  }
  return re;
}

/*
int func(int kyori){
        int re = 0;
        if(kyori == 1){
                return n;
        }
        else if(kyori == n-1){
                return 0;
        }
        int num;
        for(int i = 1; i <= n-kyori; i++){
                num = w_func(i,kyori,false,i);
                cout<<"i = "<<i<<", kyori = "<<kyori<<", num = "<<num<<endl;
                re+=num;
        }
        return re;
}


int w_func(int start,int kyori,bool tikamiti,int saisyo){
        int re;
        if(kyori == 0){
                if(start <= n){
                        if(tikamiti == false && start >= y && saisyo<=x) return
0; else return 1;
                }
                else return 0;

        }

        if(start == x){
                int re1 = w_func(start+1,kyori-1,tikamiti,saisyo);
                int re2 = w_func(y,kyori-1,true,saisyo);
                re = re1+re2;
        }
        else{
                re = w_func(start+1,kyori-1,tikamiti,saisyo);
        }
        return re;
}

*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

// int func(int kyori);
// tikamiti -> x->yの道を使ったらtrue;
// int w_func(int start,int kyori,bool tikamiti,int saisyo);

int serch(int num);
int n, x, y;
int dp[2001][2001];

int main(void) {
  cin >> n >> x >> y;
  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < n + 1; j++) {
      if (i <= j)
        dp[i][j] = j - i;
      else
        dp[i][j] = i - j;
    }
  }
  for (int i = 1; i < n + 1; i++) {
    if (dp[i][y] > dp[i][x] + 1)
      dp[i][y] = dp[i][x] + 1;
  }

  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < n + 1; j++) {
      if (i == j)
        dp[i][j] = 0;
      else if (i > j) {
        dp[i][j] = dp[j][i];
      } else {
        int migi = j;
        int hidari = j;
        while (dp[i][migi] > dp[i][migi - 1] + 1) {
          dp[i][migi] = dp[i][migi - 1] + 1;
          migi++;
          if (migi == n)
            break;
        }
        while (dp[i][hidari] > dp[i][hidari + 1] + 1 && hidari < n) {
          // cout<<"hidari"<<endl;
          // cout<<"i = "<<i<<", hidari = "<<hidari<<endl;
          dp[i][hidari] = dp[i][hidari + 1] + 1;
          hidari--;
          if (hidari == 1)
            break;
        }
      }
    }
  }

  /*
  cout<<"\\n"<<endl;
  cout<<"dp"<<endl;
  for(int i = 1; i < n+1; i++){
          for(int j = 1; j < n+1; j++){
                  cout<<dp[i][j]<<" ";
          }
          cout<<endl;
  }
  */
  for (int i = 1; i < n; i++) {
    int num = serch(i);
    cout << num << endl;
  }

  /*
  int ans[n+1];
  for(int i = 1; i < n; i++){
          ans[i] = func(i);
  }
  for(int i = 1; i < n; i++){
          cout<<"i = "<<i <<", func(i) = "<<ans[i]<<endl;
          cout<<endl;
  }
  */
  return 0;
}

int serch(int num) {
  int re = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      if (dp[i][j] == num)
        re++;
    }
  }
  return re;
}

/*
int func(int kyori){
        int re = 0;
        if(kyori == 1){
                return n;
        }
        else if(kyori == n-1){
                return 0;
        }
        int num;
        for(int i = 1; i <= n-kyori; i++){
                num = w_func(i,kyori,false,i);
                cout<<"i = "<<i<<", kyori = "<<kyori<<", num = "<<num<<endl;
                re+=num;
        }
        return re;
}


int w_func(int start,int kyori,bool tikamiti,int saisyo){
        int re;
        if(kyori == 0){
                if(start <= n){
                        if(tikamiti == false && start >= y && saisyo<=x) return
0; else return 1;
                }
                else return 0;

        }

        if(start == x){
                int re1 = w_func(start+1,kyori-1,tikamiti,saisyo);
                int re2 = w_func(y,kyori-1,true,saisyo);
                re = re1+re2;
        }
        else{
                re = w_func(start+1,kyori-1,tikamiti,saisyo);
        }
        return re;
}

*/