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


/*
    ID: sixathofpain
    TASK: friday
    LANG: C++

*/

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define setbits(x) __builtin_popcount(x)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define in insert
#define pb push_back
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

inline ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
inline ll power(ll a, ll n, ll m) {
  if (n == 0)
    return 1;
  ll p = power(a, n / 2, m);
  p = (p * p) % m;
  if (n % 2)
    return (p * a) % m;
  else
    return p;
}
const ll MOD = 1000000007;
const int N = int(3e5) + 999;
#define int long long int

typedef pair<int, int> pii;

bool comp(pii &a, pii &b) { return (a.ff + a.ss) < (b.ff + b.ss); }
bool comp2(pii &a, pii &b) { return (a.ff - a.ss) < (b.ff - b.ss); }

int32_t main() {
  IOS;

  // freopen("friday.in","r",stdin);
  // freopen("friday.out","w",stdout);

  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (auto &it : a)
    cin >> it;
  for (auto &it : b)
    cin >> it;
  map<int, int> cnta, cntb;

  for (int i = 0; i < n; i++) {
    cnta[a[i]]++;
    cntb[b[i]]++;
  }

  for (int i = 0; i < n; i++) {
    if (cntb.count(a[i]) == 0)
      continue;
    if (min(cnta[a[i]], cntb[a[i]]) > n / 2) {
      cout << "No" << endl;
      return 0;
    }
  }

  reverse(all(b));
  int l = -1, r = -1;
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      l = i;
      break;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (a[i] == b[i]) {
      r = i;
      break;
    }
  }
  if (l == -1) {
    cout << "Yes" << endl;
    for (int i : b)
      cout << i << " ";
    return 0;
  }
  int x = a[l];
  for (int i = 0; i < n; i++) {
    if (a[i] != x and b[i] != x and l <= r) {
      swap(b[i], b[l]);
      l++;
    }
  }
  if (l <= r) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  for (int i : b)
    cout << i << " ";

  return 0;
}

Error: WA

Fixed submission:



/*
        ID: sixathofpain
        TASK: friday
        LANG: C++

*/

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define setbits(x) __builtin_popcount(x)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define in insert
#define pb push_back
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

inline ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
inline ll power(ll a, ll n, ll m) {
  if (n == 0)
    return 1;
  ll p = power(a, n / 2, m);
  p = (p * p) % m;
  if (n % 2)
    return (p * a) % m;
  else
    return p;
}
const ll MOD = 1000000007;
const int N = int(3e5) + 999;
#define int long long int

typedef pair<int, int> pii;

bool comp(pii &a, pii &b) { return (a.ff + a.ss) < (b.ff + b.ss); }
bool comp2(pii &a, pii &b) { return (a.ff - a.ss) < (b.ff - b.ss); }

int32_t main() {
  IOS;

  // freopen("friday.in","r",stdin);
  // freopen("friday.out","w",stdout);

  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (auto &it : a)
    cin >> it;
  for (auto &it : b)
    cin >> it;
  map<int, int> cnta, cntb;

  for (int i = 0; i < n; i++) {
    cnta[a[i]]++;
    cntb[b[i]]++;
  }

  for (int i = 0; i < n; i++) {
    if (cntb.count(a[i]) == 0)
      continue;
    if (min(cnta[a[i]], cntb[a[i]]) > n / 2) {
      cout << "No" << endl;
      return 0;
    }
  }

  vector<int> notsame;
  deque<int> same;
  for (int i = 0; i < n; i++) {
    if (cnta.count(b[i]) == 0) {
      notsame.pb(b[i]);
    } else {
      same.pb(b[i]);
    }
  }
  if (notsame.size() == 0) {
    int mx = -1;
    reverse(all(b));
    for (int i = 0; i < n; i++) {
      if (a[i] == b[i]) {
        mx = i;
      }
    }
    if (mx != -1)
      reverse(b.begin(), b.begin() + mx + 1);
    cout << "Yes" << endl;
    for (int i = 0; i < n; i++) {
      assert(a[i] != b[i]);
      cout << b[i] << " ";
    }
    cout << endl;
    return 0;
  }

  for (int i = 0; i < n; i++) {
    b[i] = -1;
    if (same.size()) {
      if (a[i] != same.back()) {
        b[i] = same.back();
        same.pop_back();
      } else if (a[i] != same.front()) {
        b[i] = same.front();
        same.pop_front();
      }
    }
    if (b[i] == -1 and notsame.size()) {
      b[i] = notsame.back();
      notsame.pop_back();
    }
    if (b[i] == -1) {
      cout << "No";
      cout << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    assert(a[i] != b[i]);
    cout << b[i] << " ";
  }
  cout << endl;

  return 0;
}