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

#include <bits/stdc++.h>
using namespace std;
const int p = 31;
const int m = 1e9 + 7;
typedef long long ll;
typedef int in;
#define ff first
#define ss second
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif
// #define int ll
in main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  unordered_map<int, pair<int, int>> ct;

  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    ct[a[i]].ff += 1;
  }

  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    ct[b[i]].ss += 1;
  }
  bool hai = true;
  for (int i = 0; i < n; ++i) {
    if (ct[a[i]].ff + ct[a[i]].ss > n) {
      hai = false;
      break;
    }
  }

  if (!hai) {
    cout << "No";
    return 0;
  }
  cout << "Yes\\n";
  vector<int> r(n);
  r = b;
  reverse(r.begin(), r.end());
  int kha = -1, val = -1;
  for (int i = 0; i < n; ++i) {
    if (r[i] == a[i]) {
      kha = i;
      val = r[kha];
      break;
    }
  }
  int l = -1;
  for (int i = n - 1; i >= 0; --i) {
    if (a[i] == val && r[i] == val) {
      l = i;
      break;
    }
  }
  if (kha != -1)
    for (int i = 0; i < n && kha < n; ++i) {
      if (a[i] != val && r[i] != val && kha <= l) {
        swap(r[i], r[kha]);
        ++kha;
      }
      // if(kha<n && r[kha]!=val)break;
    }
  // for(int i=0;i<n && kha<n;++i){
  // 	if(a[i]==val || r[i]==val|| i==kha)continue;
  // 	swap(r[i],r[kha]);
  // 	++kha;
  // 	if(kha<n && r[kha]!=val)break;
  // }
  for (int i = 0; i < n; ++i) {
    cout << r[i] << ' ';
  }

  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n"; // check time
}

/*
12
1 1 2 3 3 4 4 5 6 7 8 9
1 3 3 3 3 3 5 6 6 6 6 6
*/

Error: -6
Time : 34.078ms
double free or corruption (out)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int p = 31;
const int m = 1e9 + 7;
typedef long long ll;
typedef int in;
#define ff first
#define ss second
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif
// #define int ll
in main() {

  int n;
  cin >> n;
  vector<int> a(n), b(n);
  unordered_map<int, pair<int, int>> ct;

  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    ct[a[i]].ff += 1;
  }

  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    ct[b[i]].ss += 1;
  }
  bool hai = true;
  int mx = -1;
  int mxv = -1;
  for (int i = 0; i < n; ++i) {
    if (ct[a[i]].ff + ct[a[i]].ss > n) {
      hai = false;
      break;
    }
    if (mx < min(ct[a[i]].ff, ct[a[i]].ss)) {
      mx = max(mx, min(ct[a[i]].ff, ct[a[i]].ss));
      mxv = a[i];
    }
  }

  if (!hai) {
    cout << "No";
    return 0;
  }
  cout << "Yes\\n";
  vector<int> r(n);
  r = b;
  sort(r.begin(), r.end(), greater<int>());
  // sort(r.begin(),r.begin()+n/2);
  // sort(r.begin()+n/2,r.end());
  int kha = -1, val = -1, num = -1;
  for (int i = 0; i < n; ++i) {
    if (r[i] == a[i]) {
      kha = i;
      val = r[kha];
      break;
    }
  }

  // for(int i=0;i<n;++i){
  // 	cout<<r[i]<<' ';
  // }
  // cout<<'\\n';
  for (int i = 0; i < n; ++i) {
    if (a[i] == val || r[i] == val)
      continue;

    // cout<<"this is val "<<val<<" "<<"this is i "<<i<<"  this is value there
    // "<<a[i]<<'\\n';
    swap(r[i], r[kha]);
    ++kha;
    if (kha < n && r[kha] != val)
      break;
  }
  for (int i = 0; i < n; ++i) {
    cout << r[i] << ' ';
  }

  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n"; // check time
}

/*
12
1 1 2 3 3 4 4 5 6 7 8 9
1 3 3 3 3 3 5 6 6 6 6 6
*/