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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;
ll INF = 1001001001001001001;
long double EPS = 1e-9;
long double PI = 3.141592653589793238;
template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<pair<ll, ll>>>;

// MOD確認

ll N;
ll A[200010];
ll B[200010];
ll C[200010];
ll cnta[200010];
ll cntb[200010];

int main() {
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    cnta[A[i]]++;
  }
  rep(i, N) {
    cin >> B[i];
    cntb[B[i]]++;
  }
  rep(i, N + 1) {
    if (cnta[i] + cntb[i] > N) {
      cout << "No" << endl;
      return 0;
    }
  }
  reverse(B, B + N);
  ll Al = 0, Ar = 0, Bl = 0, Br = 0;
  ll p = 0;
  rep(i, N) {
    if (A[i] == B[i]) {
      p = A[i];
      break;
    }
  }
  bool flag = 0;
  rep(i, N) {
    if (A[i] == p)
      flag = 1;
    if (flag && A[i] != p)
      break;
    Ar++;
    if (!flag) {
      Al++;
    }
  }
  flag = 0;
  rep(i, N) {
    if (B[i] == p)
      flag = 1;
    if (flag && B[i] != p)
      break;
    Br++;
    if (!flag) {
      Bl++;
    }
  }
  ll now = 0;
  FOR(i, max(Al, Bl), min(Ar, Br)) {
    if (now < min(Al, Bl)) {
      swap(B[i], B[now]);
      now++;
    } else if (now == min(Al, Bl)) {
      now = max(Ar, Br);
      i--;
    } else {
      if (now >= N) {
        cout << "No" << endl;
        return 0;
      }
      swap(B[i], B[now]);
      now++;
    }
  }
  cout << "Yes" << endl;
  rep(i, N) cout << B[i] << ' ';
  cout << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;
ll INF = 1001001001001001001;
long double EPS = 1e-9;
long double PI = 3.141592653589793238;
template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<pair<ll, ll>>>;

// MOD確認

ll N;
ll A[200010];
ll B[200010];
ll C[200010];
ll cnt[200010];

int main() {
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    cnt[A[i]]++;
  }
  rep(i, N) {
    cin >> B[i];
    cnt[B[i]]++;
  }
  rep(i, N + 1) {
    if (cnt[i] > N) {
      cout << "No" << endl;
      return 0;
    }
  }
  reverse(B, B + N);
  rep(i, min((ll)10, N)) {
    rep(j, N) {
      if (A[j] == B[j]) {
        rep(k, (N + 1) / 2) {
          if (B[k] != B[j] && A[k] != B[j]) {
            swap(B[k], B[j]);
            break;
          }
          if (B[N - k - 1] != B[j] && A[N - k - 1] != B[j]) {
            swap(B[N - k - 1], B[j]);
          }
        }
      }
    }
    bool flag = 1;
    rep(j, N) if (A[j] == B[j]) flag = 0;
    if (flag) {
      cout << "Yes" << endl;
      rep(j, N) cout << B[j] << ' ';
      cout << endl;
      return 0;
    }
  }
  cout << "No" << endl;
}