Problem p02798 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast,inline,unroll-loops,fast-math")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

int f(vector<int> v) {
  int res = 0;
  for (int i = 0; i < v.size(); i++) {
    // cout << i << " ";
    for (int j = i + 1; j < v.size(); j++) {
      if (v[i] > v[j])
        res++;
    }
  }
  // cout << endl;
  return res;
}

void dfs(const vector<pair<int, int>> &vp1, const vector<pair<int, int>> &vp2,
         int index1, int index2, int last, vector<int> seq, vector<int> v,
         int &res) {
  if (vp1.size() - index1 + vp2.size() - index2 + seq.size() < vp1.size())
    return;
  if (seq.size() == vp1.size()) {
    int r = f(seq);
    if (res == -1 || r < res)
      res = r;
    return;
  }

  if (seq.size() % 2 == 0) {
    while (index1 < vp1.size()) {
      if (vp1[index1].first >= last && v[vp1[index1].second] == 0) {
        seq.push_back(vp1[index1].second);
        v[vp1[index1].second] = 1;
        dfs(vp1, vp2, index1 + 1, index2, vp1[index1].first, seq, v, res);
        seq.pop_back();
        v[vp1[index1].second] = 0;
      }
      index1++;
    }
  } else {
    while (index2 < vp2.size()) {
      if (vp2[index2].first >= last && v[vp2[index2].second] == 0) {
        seq.push_back(vp2[index2].second);
        v[vp2[index2].second] = 1;
        dfs(vp1, vp2, index1, index2 + 1, vp2[index2].first, seq, v, res);
        seq.pop_back();
        v[vp2[index2].second] = 0;
      }
      index2++;
    }
  }
}

int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  vector<pair<int, int>> vp1, vp2;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (i % 2 == 0)
      vp1.push_back(make_pair(a, i));
    else
      vp2.push_back(make_pair(a, i));
  }
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    if (i % 2 == 0)
      vp2.push_back(make_pair(b, i));
    else
      vp1.push_back(make_pair(b, i));
  }

  sort(vp1.begin(), vp1.end());
  sort(vp2.begin(), vp2.end());
  int res = -1;
  int index1 = 0, index2 = 0;
  int last = 0;
  vector<int> seq;
  vector<int> v(n, 0);

  dfs(vp1, vp2, index1, index2, last, seq, v, res);

  cout << res << endl;
}


Fixed submission:
#pragma GCC optimize("Ofast,inline,unroll-loops,fast-math")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

int f(vector<int> v) {
  int res = 0;
  for (int i = 0; i < v.size(); i++) {
    // cout << i << " ";
    for (int j = i + 1; j < v.size(); j++) {
      if (v[i] > v[j])
        res++;
    }
  }
  // cout << endl;
  return res;
}

void dfs(const vector<pair<int, int>> &vp1, const vector<pair<int, int>> &vp2,
         int index1, int index2, int last, vector<int> seq, vector<int> v,
         int &res) {
  if (vp1.size() - index1 + vp2.size() - index2 + seq.size() < vp1.size())
    return;
  if (seq.size() == vp1.size()) {
    int r = f(seq);
    if (res == -1 || r < res)
      res = r;
    return;
  }

  if (res != -1) {
    int r = f(seq);
    if (r >= res)
      return;
  }

  if (seq.size() % 2 == 0) {
    while (index1 < vp1.size()) {
      if (vp1[index1].first >= last && v[vp1[index1].second] == 0) {
        seq.push_back(vp1[index1].second);
        v[vp1[index1].second] = 1;
        dfs(vp1, vp2, index1 + 1, index2, vp1[index1].first, seq, v, res);
        seq.pop_back();
        v[vp1[index1].second] = 0;
      }
      index1++;
    }
  } else {
    while (index2 < vp2.size()) {
      if (vp2[index2].first >= last && v[vp2[index2].second] == 0) {
        seq.push_back(vp2[index2].second);
        v[vp2[index2].second] = 1;
        dfs(vp1, vp2, index1, index2 + 1, vp2[index2].first, seq, v, res);
        seq.pop_back();
        v[vp2[index2].second] = 0;
      }
      index2++;
    }
  }
}

int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  vector<pair<int, int>> vp1, vp2;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (i % 2 == 0)
      vp1.push_back(make_pair(a, i));
    else
      vp2.push_back(make_pair(a, i));
  }
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    if (i % 2 == 0)
      vp2.push_back(make_pair(b, i));
    else
      vp1.push_back(make_pair(b, i));
  }

  sort(vp1.begin(), vp1.end());
  sort(vp2.begin(), vp2.end());
  int res = -1;
  int index1 = 0, index2 = 0;
  int last = 0;
  vector<int> seq;
  vector<int> v(n, 0);

  dfs(vp1, vp2, index1, index2, last, seq, v, res);

  cout << res << endl;
}