kyopro-lib

This documentation is automatically generated by online-judge-tools/verification-helper

View on GitHub

:heavy_check_mark: test/aoj/ALDS1_5_D/main.test.cpp

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_5_D"

#include <functional>
#include <iostream>
#include <vector>
#include "Mylib/IO/input_vector.cpp"
#include "Mylib/Typical/inversion_number.cpp"

namespace hl = haar_lib;

int main() {
  int n;
  std::cin >> n;

  auto a = hl::input_vector<int>(n);

  auto ans = hl::inversion_number(a, std::greater<int>());
  std::cout << ans << std::endl;

  return 0;
}
#line 1 "test/aoj/ALDS1_5_D/main.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_5_D"

#include <functional>
#include <iostream>
#include <vector>
#line 4 "Mylib/IO/input_vector.cpp"

namespace haar_lib {
  template <typename T>
  std::vector<T> input_vector(int N) {
    std::vector<T> ret(N);
    for (int i = 0; i < N; ++i) std::cin >> ret[i];
    return ret;
  }

  template <typename T>
  std::vector<std::vector<T>> input_vector(int N, int M) {
    std::vector<std::vector<T>> ret(N);
    for (int i = 0; i < N; ++i) ret[i] = input_vector<T>(M);
    return ret;
  }
}  // namespace haar_lib
#line 2 "Mylib/Typical/inversion_number.cpp"
#include <cstdint>
#line 4 "Mylib/Typical/inversion_number.cpp"

namespace haar_lib {
  namespace inversion_number_impl {
    template <typename T, typename Compare>
    int64_t rec(std::vector<T> &a, const Compare &compare) {
      const int n = a.size();
      if (n <= 1) return 0;

      int64_t ret = 0;

      std::vector<T> b(a.begin(), a.begin() + n / 2);
      std::vector<T> c(a.begin() + n / 2, a.end());

      ret += rec(b, compare);
      ret += rec(c, compare);

      int ai = 0, bi = 0, ci = 0;

      while (ai < n) {
        if (bi < (int) b.size() and (ci == (int) c.size() or not compare(b[bi], c[ci]))) {
          a[ai] = b[bi];
          ++bi;
        } else {
          ret += n / 2 - bi;
          a[ai] = c[ci];
          ++ci;
        }
        ++ai;
      }

      return ret;
    }
  }  // namespace inversion_number_impl

  template <typename T, typename Compare>
  int64_t inversion_number(std::vector<T> a, const Compare &compare) {
    return inversion_number_impl::rec(a, compare);
  }
}  // namespace haar_lib
#line 8 "test/aoj/ALDS1_5_D/main.test.cpp"

namespace hl = haar_lib;

int main() {
  int n;
  std::cin >> n;

  auto a = hl::input_vector<int>(n);

  auto ans = hl::inversion_number(a, std::greater<int>());
  std::cout << ans << std::endl;

  return 0;
}
Back to top page