Problem p03952 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int med(int a, int b, int c) {
  int v[3] = {a, b, c};
  sort(v, v + 3);
  return v[1];
}
int check(int n, int x, vector<int> v) {
  vector<int> nv[2];
  nv[0] = v;
  int s = 0;
  while (nv[s].size() > 1) {
    //    cout << nv[s].size() << endl;
    int o = s ^ 1;
    nv[o].clear();
    for (int i = 1; i < nv[s].size() - 1; i++) {
      nv[o].push_back(med(nv[s][i - 1], nv[s][i], nv[s][i + 1]));
    }
    s ^= 1;
  }
  return nv[s][0] == x;
}
int main() {
  int n;
  cin >> n;
  int x;
  cin >> x;
  vector<int> res;
  if (x == 2 * n - 1 || x == 1) {
    cout << "No";
  } else if (x == n) {
    cout << "Yes\n";
    for (int i = 1; i <= 2 * n - 1; i++)
      res.push_back(i);
    assert(check(n, x, res));
    for (auto el : res)
      cout << el << endl;
  } else {
    if (x == 2) {
      cout << "Yes\n";
      int cnt = 0;
      int i = 5;

      for (; cnt < n - 3; i++, cnt++)
        res.push_back(i);
      res.push_back(3);
      res.push_back(2);
      res.push_back(1);
      res.push_back(4);
      //      cout << 3 << endl;
      //      cout << 2 << endl;
      //      cout << 1 << endl;
      //      cout << 4 << endl;
      for (; i <= 2 * n - 1; i++)
        res.push_back(i);
      for (auto el : res)
        cout << el << endl;
      //        cout << i <<endl;
      assert(check(n, x, res));
    } else {
      cout << "Yes\n";
      int cnt = 0;
      int i = 1;
      for (; cnt < n - 3; i++) {
        if (i != x && i != x - 1 && i != x - 2 && i != x + 1) {
          res.push_back(i);
          //          cout << i << endl;
          cnt++;
        }
      }

      //      cout << x - 1 << endl;
      //      cout << x << endl;
      //      cout << x + 1 << endl;
      //      cout << x - 2 << endl;
      res.push_back(x - 1);
      res.push_back(x);
      res.push_back(x + 1);
      res.push_back(x - 2);
      for (; i <= 2 * n - 1; i++) {
        if (i != x && i != x - 1 && i != x - 2 && i != x + 1) {
          res.push_back(i);
          //          cout << i << endl;
        }
      }
      for (auto el : res)
        cout << el << endl;
      assert(check(n, x, res));
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int med(int a, int b, int c) {
  int v[3] = {a, b, c};
  sort(v, v + 3);
  return v[1];
}
int check(int n, int x, vector<int> v) {
  return 1;
  vector<int> nv[2];
  nv[0] = v;
  int s = 0;
  while (nv[s].size() > 1) {
    //    cout << nv[s].size() << endl;
    int o = s ^ 1;
    nv[o].clear();
    for (int i = 1; i < nv[s].size() - 1; i++) {
      nv[o].push_back(med(nv[s][i - 1], nv[s][i], nv[s][i + 1]));
    }
    s ^= 1;
  }
  return nv[s][0] == x;
}
int main() {
  int n;
  cin >> n;
  int x;
  cin >> x;
  vector<int> res;
  if (x == 2 * n - 1 || x == 1) {
    cout << "No";
  } else if (x == n) {
    cout << "Yes\n";
    for (int i = 1; i <= 2 * n - 1; i++)
      res.push_back(i);
    assert(check(n, x, res));
    for (auto el : res)
      cout << el << endl;
  } else {
    if (x == 2) {
      cout << "Yes\n";
      int cnt = 0;
      int i = 5;

      for (; cnt < n - 3; i++, cnt++)
        res.push_back(i);
      res.push_back(3);
      res.push_back(2);
      res.push_back(1);
      res.push_back(4);
      //      cout << 3 << endl;
      //      cout << 2 << endl;
      //      cout << 1 << endl;
      //      cout << 4 << endl;
      for (; i <= 2 * n - 1; i++)
        res.push_back(i);
      for (auto el : res)
        cout << el << endl;
      //        cout << i <<endl;
      assert(check(n, x, res));
    } else {
      cout << "Yes\n";
      int cnt = 0;
      int i = 1;
      for (; cnt < n - 3; i++) {
        if (i != x && i != x - 1 && i != x - 2 && i != x + 1) {
          res.push_back(i);
          //          cout << i << endl;
          cnt++;
        }
      }

      //      cout << x - 1 << endl;
      //      cout << x << endl;
      //      cout << x + 1 << endl;
      //      cout << x - 2 << endl;
      res.push_back(x - 1);
      res.push_back(x);
      res.push_back(x + 1);
      res.push_back(x - 2);
      for (; i <= 2 * n - 1; i++) {
        if (i != x && i != x - 1 && i != x - 2 && i != x + 1) {
          res.push_back(i);
          //          cout << i << endl;
        }
      }
      for (auto el : res)
        cout << el << endl;
      assert(check(n, x, res));
    }
  }
  return 0;
}