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


using namespace std;

// #define fileio

#ifndef fileio
#include <iostream>
#endif
#include <algorithm>
#include <assert.h>
#include <ctime>
#include <fstream>
#include <functional>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>

typedef long long ll;

#ifdef fileio
ifstream cin;
ofstream cout;
#endif

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using mint = ModInt<1000000007>;

mint power(mint a, int b) {
  if (b == 0)
    return 1;
  mint y = power(a, b / 2);
  if (b % 2 == 0)
    return y * y;
  return y * y * a;
}

mint fct[500005];
mint ifct[5000005];

mint choose(int a, int b) { return fct[a] * ifct[b] * ifct[a - b]; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef fileio
  cin.open("Text.txt");
  cout.open("Output.txt");
#endif

  int n;
  cin >> n;

  vector<int> bs;

  map<int, vector<int>> a;
  map<int, int> b;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a[x].push_back(i);
  }

  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    bs.push_back(x);
    b[x]++;
  }

  int mx = 0;

  for (auto p : a) {
    if (a[p.first].size() + b[p.first] > n) {
      cout << "No" << endl;
      return 0;
    };
    mx = max(mx, (int)a[p.first].size());
  }

  set<pair<int, int>> all;
  set<pair<int, int>> bll;
  for (auto p : a) {
    if (p.second.size() == 0)
      continue;
    all.insert({a[p.first].size() + b[p.first], p.first});
  }
  for (auto p : b) {
    if (p.second == 0)
      continue;
    bll.insert({a[p.first].size() + b[p.first], p.first});
  }

  vector<int> ans(n);
  for (int _ = 0; _ < n; _++) {
    auto i = *(all.rbegin());
    auto j = *(bll.rbegin());
    if (i.second == j.second) {
      auto k = bll.end();
      k--;
      k--;
      j = *k;
    }
    ans[a[i.second].back()] = j.second;
    a[i.second].pop_back();
    b[j.second]--;
    all.erase(i);
    bll.erase(j);
    if (a[i.second].size() > 0) {
      all.insert({i.first - 1, i.second});
    }
    if (a[j.second].size() > 0) {
      all.erase(j);
      all.insert({j.first - 1, j.second});
    }
    if (b[i.second] > 0) {
      bll.erase(i);
      bll.insert({i.first - 1, i.second});
    }
    if (b[j.second] > 0) {
      bll.insert({j.first - 1, j.second});
    }
  }

  cout << "Yes" << endl;
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
}


Error: WA

Fixed submission:


using namespace std;

// #define fileio

#ifndef fileio
#include <iostream>
#endif
#include <algorithm>
#include <assert.h>
#include <ctime>
#include <fstream>
#include <functional>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>

typedef long long ll;

#ifdef fileio
ifstream cin;
ofstream cout;
#endif

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using mint = ModInt<1000000007>;

mint power(mint a, int b) {
  if (b == 0)
    return 1;
  mint y = power(a, b / 2);
  if (b % 2 == 0)
    return y * y;
  return y * y * a;
}

mint fct[500005];
mint ifct[5000005];

mint choose(int a, int b) { return fct[a] * ifct[b] * ifct[a - b]; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef fileio
  cin.open("Text.txt");
  cout.open("Output.txt");
#endif

  int n;
  cin >> n;

  vector<int> bs;

  map<int, vector<int>> a;
  map<int, int> b;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a[x].push_back(i);
  }

  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    bs.push_back(x);
    b[x]++;
  }

  int mx = 0;

  for (auto p : a) {
    if (a[p.first].size() + b[p.first] > n) {
      cout << "No" << endl;
      return 0;
    };
    mx = max(mx, (int)a[p.first].size());
  }

  set<pair<int, int>> all;
  set<pair<int, int>> bll;
  for (auto p : a) {
    all.insert({a[p.first].size() + b[p.first], p.first});
  }
  for (auto p : b) {
    bll.insert({a[p.first].size() + b[p.first], p.first});
  }

  vector<int> ans(n);
  for (int _ = 0; _ < n; _++) {
    auto i = *(all.rbegin());
    auto j = *(bll.rbegin());
    if (i.second == j.second) {
      auto k = bll.end();
      k--;
      k--;
      j = *k;
    }
    ans[a[i.second].back()] = j.second;
    a[i.second].pop_back();
    b[i.second]--;
    all.erase(i);
    bll.erase(j);
    if (a[i.second].size() > 0) {
      all.insert({i.first - 1, i.second});
    }
    if (a[j.second].size() > 0) {
      all.erase(j);
      all.insert({j.first - 1, j.second});
    }
    if (b[i.second] > 0) {
      bll.erase(i);
      bll.insert({i.first - 1, i.second});
    }
    if (b[j.second] > 0) {
      bll.insert({j.first - 1, j.second});
    }
  }

  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
}