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


#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_) : i(i_) {}
    constexpr void operator++() { ++i; }
    constexpr int operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_) : l(l_), r(std::max(l_, r_)) {}
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_) : i(i_) {}
    constexpr void operator++() { --i; }
    constexpr int operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr revrange(int l_, int r_) : l(l_ - 1), r(std::max(l_, r_) - 1) {}
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

int main() {
  int N, M;
  std::cin >> N >> M;
  if (N & 1) {
    for (int i : range(0, M)) {
      std::cout << i + 1 << ' ' << N - i << '\\n';
    }
  } else {
    int half = (N / 2) - 1;
    for (int i : range(0, std::min((half + 1) / 2, M))) {
      std::cout << i + 1 << ' ' << N - i << '\\n';
    }
    for (int i : range((half + 1) / 2, M)) {
      std::cout << i + 1 << ' ' << N - i - 1 << '\\n';
    }
  }
  return 0;
}


Error: WA

Fixed submission:


#include <algorithm>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_) : i(i_) {}
    constexpr void operator++() { ++i; }
    constexpr int operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_) : l(l_), r(std::max(l_, r_)) {}
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_) : i(i_) {}
    constexpr void operator++() { --i; }
    constexpr int operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr revrange(int l_, int r_) : l(l_ - 1), r(std::max(l_, r_) - 1) {}
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

int main() {
  int N, M;
  std::cin >> N >> M;
  if (N & 1) {
    for (int i : range(0, M)) {
      std::cout << i + 1 << ' ' << N - i << '\\n';
    }
    std::exit(1);
  } else {
    int half = (N / 2) - 1;
    for (int i : range(0, (half + 1) / 2)) {
      std::cout << i + 1 << ' ' << N - i << '\\n';
    }
    for (int i : range((half + 1) / 2, M)) {
      std::cout << i + 1 << ' ' << N - i - 1 << '\\n';
    }
  }
  return 0;
}