Problem p03244 (C++)

Buggy submission (status: Runtime Error):


#if 1
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

auto &in = std::cin;
auto &out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI =
    3.141592653589793238462643383279502884197169399375105820974944;

int32_t n;

template <typename T, typename V> int indexOf(const T &c, const V &v) {
  using std::begin;
  using std::end;
  return std::find(begin(c), end(c), v) - begin(c);
}
template <typename T, typename V> int lastIndexOf(const T &c, const V &v) {
  using std::rbegin;
  using std::rend;
  return rend(c) - std::find(rbegin(c), rend(c), v);
}

int main() {
  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);
  in.tie(nullptr);
  out.tie(nullptr);

  in >> n;
  std::vector<int> a(n / 2);
  std::vector<int> b(n / 2);
  for (size_t i = 0; i < n / 2; i++) {
    in >> a[i] >> b[i];
  }

  std::sort(a.begin(), a.end());
  std::sort(b.begin(), b.end());
  int counta = 1;
  int countb = 1;
  std::vector<int> lista;
  std::vector<int> listacount;
  std::vector<int> listb;
  std::vector<int> listbcount;
  for (int i = 0; i < n / 2 - 1; i++) {
    if (a[i] != a[i + 1]) {
      lista.push_back(a[i]);
      listacount.push_back(counta);
      counta = 1;
    } else {
      counta++;
    }
    if (b[i] != b[i + 1]) {
      listb.push_back(b[i]);
      listbcount.push_back(countb);
      countb = 1;
    } else {
      countb++;
    }
  }

  lista.push_back(a[n / 2 - 1]);
  listacount.push_back(counta);

  listb.push_back(b[n / 2 - 1]);
  listbcount.push_back(countb);

  int maxa = 0;
  for (int i = 0; i < listacount.size(); i++) {
    maxa = std::max(listacount[i], maxa);
  }
  int maxb = 0;
  for (int i = 0; i < listbcount.size(); i++) {
    maxb = std::max(listbcount[i], maxb);
  }
  if (lista.at(indexOf(listacount, maxa)) !=
          listb.at(indexOf(listbcount, maxb)) ||
      lista.at(lastIndexOf(listacount, maxa)) !=
          listb.at(indexOf(listbcount, maxb)) ||
      lista.at(indexOf(listacount, maxa)) !=
          listb.at(lastIndexOf(listbcount, maxb))) {
    std::cout << (n - maxa - maxb) << endl;
  } else {
    // List<Integer> seta = new ArrayList<>();
    // Collections.addAll(seta, maxa);
    // listacount.removeAll(seta);
    // List<Integer> setb = new ArrayList<>();
    // Collections.addAll(setb, maxb);
    // listbcount.removeAll(setb);

    listacount.erase(std::remove(listacount.begin(), listacount.end(), maxa),
                     listacount.end());
    listbcount.erase(std::remove(listbcount.begin(), listbcount.end(), maxa),
                     listbcount.end());

    int maxas = 0;
    for (int i = 0; i < listacount.size(); i++) {
      maxas = std::max(listacount.at(i), maxas);
    }
    int maxbs = 0;
    for (int i = 0; i < listbcount.size(); i++) {
      maxbs = std::max(listbcount.at(i), maxbs);
    }
    std::cout << std::min(n - maxas - maxb, n - maxa - maxbs) << endl;
  }

  return 0;
}
#endif


Fixed submission:


#if 1
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

auto &in = std::cin;
auto &out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI =
    3.141592653589793238462643383279502884197169399375105820974944;

int32_t n;

template <typename T, typename V> int indexOf(const T &c, const V &v) {
  using std::begin;
  using std::end;
  return std::find(begin(c), end(c), v) - begin(c);
}
template <typename T, typename V> int lastIndexOf(const T &c, const V &v) {
  using std::rbegin;
  using std::rend;
  return (rend(c) - std::find(rbegin(c), rend(c), v)) - 1;
}

int main() {
  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);
  in.tie(nullptr);
  out.tie(nullptr);

  in >> n;
  std::vector<int> a(n / 2);
  std::vector<int> b(n / 2);
  for (size_t i = 0; i < n / 2; i++) {
    in >> a[i] >> b[i];
  }

  std::sort(a.begin(), a.end());
  std::sort(b.begin(), b.end());
  int counta = 1;
  int countb = 1;
  std::vector<int> lista;
  std::vector<int> listacount;
  std::vector<int> listb;
  std::vector<int> listbcount;
  for (int i = 0; i < n / 2 - 1; i++) {
    if (a[i] != a[i + 1]) {
      lista.push_back(a[i]);
      listacount.push_back(counta);
      counta = 1;
    } else {
      counta++;
    }
    if (b[i] != b[i + 1]) {
      listb.push_back(b[i]);
      listbcount.push_back(countb);
      countb = 1;
    } else {
      countb++;
    }
  }

  lista.push_back(a[n / 2 - 1]);
  listacount.push_back(counta);

  listb.push_back(b[n / 2 - 1]);
  listbcount.push_back(countb);

  int maxa = 0;
  for (int i = 0; i < listacount.size(); i++) {
    maxa = std::max(listacount[i], maxa);
  }
  int maxb = 0;
  for (int i = 0; i < listbcount.size(); i++) {
    maxb = std::max(listbcount[i], maxb);
  }
  if (lista.at(indexOf(listacount, maxa)) !=
          listb.at(indexOf(listbcount, maxb)) ||
      lista.at(lastIndexOf(listacount, maxa)) !=
          listb.at(indexOf(listbcount, maxb)) ||
      lista.at(indexOf(listacount, maxa)) !=
          listb.at(lastIndexOf(listbcount, maxb))) {
    std::cout << (n - maxa - maxb) << endl;
  } else {
    // List<Integer> seta = new ArrayList<>();
    // Collections.addAll(seta, maxa);
    // listacount.removeAll(seta);
    // List<Integer> setb = new ArrayList<>();
    // Collections.addAll(setb, maxb);
    // listbcount.removeAll(setb);

    listacount.erase(std::remove(listacount.begin(), listacount.end(), maxa),
                     listacount.end());
    listbcount.erase(std::remove(listbcount.begin(), listbcount.end(), maxa),
                     listbcount.end());

    int maxas = 0;
    for (int i = 0; i < listacount.size(); i++) {
      maxas = std::max(listacount.at(i), maxas);
    }
    int maxbs = 0;
    for (int i = 0; i < listbcount.size(); i++) {
      maxbs = std::max(listbcount.at(i), maxbs);
    }
    std::cout << std::min(n - maxas - maxb, n - maxa - maxbs) << endl;
  }

  return 0;
}
#endif