Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

int main() {
  int n;
  cin >> n;
  set<int> sss;
  vector<int> a(n);
  cin >> a;
  vector<int> vb(n);
  cin >> vb;
  deque<int> b;
  for (int i = 0; i < n; ++i) {
    sss.insert(vb[i]);
    b.push_back(vb[i]);
  }
  vector<int> v;
  bool f = true;
  for (int j = 0; j < 200; ++j) {
    v.clear();
    f = true;
    if (j > 0) {
      if (b.front() == b.back()) {
        dline;
        f = false;
        break;
      }
      int x = b.front();
      while (b.front() == x) {
        b.pop_front();
        b.push_back(x);
      }
    }
    deque<int> bb = b;
    for (int i = 0; i < n; ++i) {
      if (j >= 100) {
        if (bb.back() != a[i]) {
          v.push_back(bb.back());
          bb.pop_back();
        } else if (bb.front() != a[i]) {
          v.push_back(bb.front());
          bb.pop_front();
        } else {
          f = false;
          break;
        }
      } else {
        if (bb.front() != a[i]) {
          v.push_back(bb.front());
          bb.pop_front();
        } else if (bb.back() != a[i]) {
          v.push_back(bb.back());
          bb.pop_back();
        } else {
          f = false;
          break;
        }
      }
    }
    if (f)
      break;
  }
  if (f) {
    cout << "Yes" << endl;
    for (int i = 0; i < n; ++i) {
      if (i)
        cout << ' ';
      cout << v[i];
    }
    cout << endl;
    return 0;
  } else {
    cout << "No" << endl;
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  cin >> a;
  vector<int> vb(n);
  cin >> vb;
  deque<int> b;
  for (int i = 0; i < n; ++i) {
    b.push_back(vb[i]);
  }
  vector<int> v;
  bool f = true;
  for (int i = 0; i < n; ++i) {
    if (b.back() != a[i]) {
      v.push_back(b.back());
      b.pop_back();
    } else if (b.front() != a[i]) {
      v.push_back(b.front());
      b.pop_front();
    } else {
      f = false;
      break;
    }
  }
  if (f) {
    cout << "Yes" << endl;
    for (int i = 0; i < n; ++i) {
      if (i)
        cout << ' ';
      cout << v[i];
    }
    cout << endl;
    return 0;
  }
  multiset<int> st;
  for (int i = 0; i < n; ++i)
    st.insert(b[i]);
  {
    vector<int> vv;
    bool ff = true;
    for (int i = 0; i < n; ++i) {
      auto it = st.begin();
      if (*it == a[i]) {
        auto iit = st.upper_bound(a[i]);
        if (iit == st.end()) {
          ff = false;
          break;
        }
        it = iit;
      }
      vv.push_back(*it);
      st.erase(it);
    }
    if (ff) {
      cout << "Yes" << endl;
      for (int i = 0; i < n; ++i) {
        if (i)
          cout << ' ';
        cout << vv[i];
      }
      cout << endl;
    } else {
      cout << "No" << endl;
    }
  }
}