Problem p03496 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;

#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x, y) x##y

using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;

using f32 = float;
using f64 = double;

#define FOR(i, start, end)                                                     \
  for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end);                          \
       i < CPP_CAT(i, xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) for (i64 i = 1; i <= n; ++i)
#define RREP(i, n) for (i64 i = n - 1; i >= 0; --i)
#define RREPS(i, n) for (i64 i = n; i >= 1; --i)

#define ALL(f, c, ...)                                                         \
  (([&](decltype((c)) cccc) {                                                  \
    return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__);               \
  })(c))

#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define pb push_back
#define mp make_pair

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename C> i64 SIZE(const C &c) {
  return static_cast<i64>(c.size());
}

template <typename T, size_t N> i64 SIZE(const T (&)[N]) {
  return static_cast<i64>(N);
}

struct ProconInit {
  static constexpr int IOS_PREC = 15;
  static constexpr bool AUTOFLUSH = false;

  ProconInit() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(IOS_PREC);
    if (AUTOFLUSH)
      cout << unitbuf;
  }
} PROCON_INIT;

#define INF 1'000'000

int main() {
  i64 N;
  cin >> N;
  vector<i64> A(N);

  i64 maxi;
  i64 maxabs = 0;
  REP(i, N) {
    cin >> A[i];
    if (chmax(maxabs, abs(A[i])))
      maxi = i;
  }

  using P = pair<i64, i64>;
  vector<P> ops;

  REP(i, N) {
    if (maxi != i) {
      A[i] += A[maxi];
      ops.emplace_back(maxi, i);
    }
  }

  if (A[maxi] >= 0) {
    REP(i, N - 1) {
      A[i + 1] += A[i];
      ops.emplace_back(i, i + 1);
    }
  } else {
    for (i64 i = N - 1; i > 0; i--) {
      A[i - 1] += A[i];
      ops.emplace_back(i, i - 1);
    }
  }

  assert(ops.size() <= 2 * N);
  cout << ops.size() << endl;

  REP(i, ops.size()) {
    i64 x = ops[i].first + 1;
    i64 y = ops[i].second + 1;
    cout << x << " " << y << endl;
  }

  return 0;
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x, y) x##y

using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;

using f32 = float;
using f64 = double;

#define FOR(i, start, end)                                                     \
  for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end);                          \
       i < CPP_CAT(i, xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) for (i64 i = 1; i <= n; ++i)
#define RREP(i, n) for (i64 i = n - 1; i >= 0; --i)
#define RREPS(i, n) for (i64 i = n; i >= 1; --i)

#define ALL(f, c, ...)                                                         \
  (([&](decltype((c)) cccc) {                                                  \
    return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__);               \
  })(c))

#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define pb push_back
#define mp make_pair

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename C> i64 SIZE(const C &c) {
  return static_cast<i64>(c.size());
}

template <typename T, size_t N> i64 SIZE(const T (&)[N]) {
  return static_cast<i64>(N);
}

struct ProconInit {
  static constexpr int IOS_PREC = 15;
  static constexpr bool AUTOFLUSH = false;

  ProconInit() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(IOS_PREC);
    if (AUTOFLUSH)
      cout << unitbuf;
  }
} PROCON_INIT;

#define INF 1'000'000

int main() {
  i64 N;
  cin >> N;
  vector<i64> A(N);

  i64 maxi;
  i64 maxabs = 0;
  REP(i, N) {
    cin >> A[i];
    if (chmax(maxabs, abs(A[i])))
      maxi = i;
  }

  bool ok = true;
  REP(i, N - 1) {
    if (A[i] > A[i + 1]) {
      ok = false;
      break;
    }
  }
  if (ok) {
    cout << 0 << endl;
    return 0;
  }

  using P = pair<i64, i64>;
  vector<P> ops;

  REP(i, N) {
    if (maxi != i) {
      A[i] += A[maxi];
      ops.emplace_back(maxi, i);
    }
  }

  if (A[maxi] >= 0) {
    REP(i, N - 1) {
      A[i + 1] += A[i];
      ops.emplace_back(i, i + 1);
    }
  } else {
    for (i64 i = N - 1; i > 0; i--) {
      A[i - 1] += A[i];
      ops.emplace_back(i, i - 1);
    }
  }

  assert(ops.size() <= 2 * N);
  cout << ops.size() << endl;

  REP(i, ops.size()) {
    i64 x = ops[i].first + 1;
    i64 y = ops[i].second + 1;
    cout << x << " " << y << endl;
  }

  return 0;
}