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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

struct Item {
  int numA, numB, val;
  int size() const { return numA + numB; }
  bool operator<(const Item &o) const { return size() < o.size(); }
};

int main2() {
  int N = nextLong();

  vector<int> a(N), b(N);
  REP(i, N) a[i] = nextLong();
  REP(i, N) b[i] = nextLong();

  map<int, int> A, B;
  REP(i, N) A[a[i]]++;
  REP(i, N) B[b[i]]++;

  for (int i = 1; i <= N; i++) {
    if (A[i] + B[i] > N) {
      cout << "No" << endl;
      return 0;
    }
  }

  vector<int> aa, bb;
  priority_queue<Item> pq;
  for (int i = 1; i <= N; i++) {
    if (A[i] + B[i] > 0) {

      if (min(A[i], B[i]) > 0) {
        pq.push((Item){A[i], B[i], i});
      } else {
        REP(_, A[i]) aa.push_back(i);
        REP(_, B[i]) bb.push_back(i);
      }
    }
  }

  vector<pair<int, int>> ps;
  for (; pq.size() >= 2;) {
    Item x = pq.top();
    pq.pop();
    Item y = pq.top();
    pq.pop();
    if (y.numA > 0) {
      ps.push_back({y.val, x.val});
      x.numB--;
      y.numA--;
    } else {
      ps.push_back({x.val, y.val});
      x.numA--;
      y.numB--;
    }

    if (x.numA > 0 && x.numB > 0) {
      pq.push(x);
    } else {
      REP(_, x.numA) aa.push_back(x.val);
      REP(_, x.numB) bb.push_back(x.val);
    }
    if (y.numA > 0 && y.numB > 0) {
      pq.push(y);
    } else {
      REP(_, y.numA) aa.push_back(y.val);
      REP(_, y.numB) bb.push_back(y.val);
    }
  }

  if (pq.size() > 0) {
    Item x = pq.top();
    pq.pop();

    while (x.numA > 0) {
      x.numA--;
      ps.push_back({x.val, bb.back()});
      bb.pop_back();
    }
    while (x.numB > 0) {
      x.numB--;
      ps.push_back({aa.back(), x.val});
      aa.pop_back();
    }
  }

  while (aa.size()) {
    ps.push_back({aa.back(), bb.back()});
    aa.pop_back();
    bb.pop_back();
  }
  sort(ALL(ps));

  vector<int> ans(N);
  REP(i, N) ans[i] = ps[i].second;

  cout << "Yes" << endl;
  REP(i, N) {
    if (i)
      cout << ' ';
    cout << ans[i];
  }
  cout << endl;

  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

int main2() {
  int N = nextLong();

  vector<int> a(N), b(N);
  REP(i, N) a[i] = nextLong();
  REP(i, N) b[i] = nextLong();

  map<int, int> A, B;
  REP(i, N) A[a[i]]++;
  REP(i, N) B[b[i]]++;

  int c = -1, val = 0;
  for (int i = 1; i <= N; i++) {
    if (A[i] + B[i] > c) {
      c = A[i] + B[i];
      val = i;
    }
    if (A[i] + B[i] > N) {
      cout << "No" << endl;
      return 0;
    }
  }

  int D = 0;
  if (c != -1) {
    {
      int posA = lower_bound(ALL(a), val) - a.begin();
      int posB = lower_bound(ALL(b), val) - b.begin();
      D = posA - posB;
    }
    D += N;
    D += N / 2;
  }

  cout << "Yes" << endl;
  vector<int> ans;
  REP(i, N) ans.push_back(b[(i + D) % N]);

  for (;;) {
    int same = -1;
    REP(i, N) if (ans[i] == a[i]) {
      same = i;
      break;
    }
    if (same == -1)
      break;

    int j = -1;
    REP(i, N) if (a[same] != b[i] && a[i] != b[same]) {
      j = i;
      break;
    }
    assert(j != -1);
    swap(ans[same], ans[j]);
  }
  REP(i, N) {
    if (i)
      cout << ' ';
    cout << ans[i];
  }
  cout << endl;

  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}