Problem p02557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int N = 3e5 + 10;
const int M = 1e6 + 10;
const int mod = 1e9 + 7;
inline LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }
inline LL lcm(LL a, LL b) { return a * b / gcd(a, b); }
inline LL read() {
  LL x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
inline LL qpow(LL x, LL k = mod - 2, LL m = mod) {
  LL res = 1;
  while (k) {
    if (k & 1)
      res = res * x % m;
    x = x * x % m;
    k >>= 1;
  }
  return res;
}
/*=================================================================================================================*/
int n, a[N], b[N];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &b[i]);
  reverse(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++) {
    if (b[i] != a[i])
      continue;
    for (int k = 1; k <= n; k++)
      if (b[i] != a[k] && b[k] != a[i]) {
        swap(b[i], b[k]);
        break;
      }
  }
  for (int i = 1; i <= n; i++)
    if (a[i] == b[i])
      return printf("No\n"), 0;
  printf("Yes\n");
  for (int i = 1; i <= n; i++)
    printf("%d ", b[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int N = 3e5 + 10;
const int M = 1e6 + 10;
const int mod = 1e9 + 7;
inline LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }
inline LL lcm(LL a, LL b) { return a * b / gcd(a, b); }
inline LL read() {
  LL x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
inline LL qpow(LL x, LL k = mod - 2, LL m = mod) {
  LL res = 1;
  while (k) {
    if (k & 1)
      res = res * x % m;
    x = x * x % m;
    k >>= 1;
  }
  return res;
}
/*=================================================================================================================*/
int n, a[N], b[N];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &b[i]);
  reverse(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++) {
    if (b[i] != a[i])
      continue;
    for (int k = 1; k <= n; k++)
      if (b[i] != a[k] && b[k] != a[i]) {
        swap(b[i], b[k]);
        break;
      }
    if (b[i] == a[i])
      break;
  }
  for (int i = 1; i <= n; i++)
    if (a[i] == b[i])
      return printf("No\n"), 0;
  printf("Yes\n");
  for (int i = 1; i <= n; i++)
    printf("%d ", b[i]);
  return 0;
}