Problem p02798 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int A[18];
int B[18];
int dp[1 << 18][18];

bool in_bit(int bit_set, int ind) {
  if (((bit_set >> ind) & 1) == 1) {
    return true;
  }
  return false;
}

int add_bit(int bit_set, int ind) { return bit_set ^ (1 << ind); }

int bit_set_size(int bit_set, int N) {
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (((bit_set >> i) & 1) == 1) {
      ans++;
    }
  }
  return ans;
}

int get_swap(int bit_set, int ind, int N) {
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (((bit_set >> i) & 1) == 1) {
      if (i > ind) {
        ans++;
      }
    }
  }
  return ans;
}
int get_card_num(int ind, int org_ind) {
  if (ind % 2 == org_ind % 2) {
    return A[org_ind];
  }
  return B[org_ind];
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
  }

  for (int i = 0; i < (1 << N); i++) {
    for (int j = 0; j < N; j++) {
      dp[i][j] = INT_MAX;
    }
  }
  for (int i = 0; i < N; i++) {
    dp[0][i] = 0;
  }

  for (int bit_set = 0; bit_set < (1 << N) - 1; bit_set++) {
    for (int i = 0; i < N; i++) {
      if (dp[bit_set][i] != INT_MAX) {
        for (int j = 0; j < N; j++) {
          if (!in_bit(bit_set, j)) {
            int cur_ind = bit_set_size(bit_set, N) + 1;
            int cur_card_num = get_card_num(cur_ind, j);
            int prev_card_num = get_card_num(cur_ind - 1, i);
            if (prev_card_num <= cur_card_num) {
              int next_bit_set = add_bit(bit_set, i);
              dp[next_bit_set][j] =
                  min(dp[next_bit_set][j],
                      dp[bit_set][i] + get_swap(next_bit_set, j, N));
            }
          }
        }
      }
    }
  }

  int ans = INT_MAX;
  for (int i = 0; i < N; i++) {
    int tmp = ((1 << N) - 1) ^ (1 << i);
    ans = min(ans, dp[tmp][i]);
  }

  if (ans == INT_MAX) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int A[18];
int B[18];
int dp[1 << 18][18];

bool in_bit(int bit_set, int ind) {
  if (((bit_set >> ind) & 1) == 1) {
    return true;
  }
  return false;
}

int add_bit(int bit_set, int ind) { return bit_set ^ (1 << ind); }

int bit_set_size(int bit_set, int N) {
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (((bit_set >> i) & 1) == 1) {
      ans++;
    }
  }
  return ans;
}

int get_swap(int bit_set, int ind, int N) {
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (((bit_set >> i) & 1) == 1) {
      if (i > ind) {
        ans++;
      }
    }
  }
  return ans;
}
int get_card_num(int ind, int org_ind) {
  if (ind % 2 == org_ind % 2) {
    return A[org_ind];
  }
  return B[org_ind];
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
  }

  for (int i = 0; i < (1 << N); i++) {
    for (int j = 0; j < N; j++) {
      dp[i][j] = INT_MAX;
    }
  }
  for (int i = 0; i < N; i++) {
    dp[0][i] = 0;
  }

  for (int bit_set = 0; bit_set < (1 << N) - 1; bit_set++) {
    for (int i = 0; i < N; i++) {
      if (dp[bit_set][i] != INT_MAX) {
        for (int j = 0; j < N; j++) {
          if (i == j) {
            continue;
          }
          if (!in_bit(bit_set, j)) {
            int cur_ind = bit_set_size(bit_set, N) + 1;
            int cur_card_num = get_card_num(cur_ind, j);
            int prev_card_num = get_card_num(cur_ind - 1, i);
            if (prev_card_num <= cur_card_num) {
              int next_bit_set = add_bit(bit_set, i);
              dp[next_bit_set][j] =
                  min(dp[next_bit_set][j],
                      dp[bit_set][i] + get_swap(next_bit_set, j, N));
            }
          }
        }
      }
    }
  }

  int ans = INT_MAX;
  for (int i = 0; i < N; i++) {
    int tmp = ((1 << N) - 1) ^ (1 << i);
    ans = min(ans, dp[tmp][i]);
  }

  if (ans == INT_MAX) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}