Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rrep(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (s); i < (t); ++i)
#define rng(obj) (obj).begin(), (obj).end()
#define rrng(obj) (obj).rbegin(), (obj).rend()
#define maxs(x, y) (x = max((x), (y)))
#define mins(x, y) (x = min((x), (y)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define uni(x) x.erase(unique(rng(x)), x.end())
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define dup(x, y) (((x) + (y)-1) / (y))
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vp = vector<pii>;
template <class T> inline istream &operator>>(istream &i, v(T) & v) {
  rep(j, v.size()) i >> v[j];
  return i;
}
template <class T> string join(const v(T) & v) {
  stringstream s;
  rep(i, v.size()) s << ' ' << v[i];
  return s.str().substr(1);
}
template <class T> inline ostream &operator<<(ostream &o, const v(T) & v) {
  if (v.size())
    o << join(v);
  return o;
}
template <class T1, class T2>
inline istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.fi >> v.se;
}
template <class T1, class T2>
inline ostream &operator<<(ostream &o, pair<T1, T2> &v) {
  return o << v.fi << "," << v.se;
}
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const double eps = 1e-10;
bool comp(pii a, pii b) { return a.se < b.se; }
//----------------------------------------------------------------

#define fileio                                                                 \
  ifstream fio;                                                                \
  if (argc >= 2) {                                                             \
    fio.open(argv[1]);                                                         \
    std::cin.rdbuf(fio.rdbuf());                                               \
  }
#define fastio ios_base::sync_with_stdio(false);
#define MOD 1000000007
//----------------------------------------------------------------
void Main() {
  int n;
  cin >> n;
  vl a(1);
  cin >> a[0];
  rep(i, n - 1) {
    ll aa;
    cin >> aa;
    if (aa != a.back())
      a.pb(aa);
  }
  ll ans = 1;
  if (a.size() > 2) {
    rep(i, n - 2) {
      if ((a[i + 2] - a[i + 1]) * (a[i + 1] - a[i]) < 0) {
        ans++;
        ++i;
      }
    }
  }
  cout << ans << endl;
}
int main(int argc, char *argv[]) {
  fastio;
  fileio;
  Main();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rrep(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (s); i < (t); ++i)
#define rng(obj) (obj).begin(), (obj).end()
#define rrng(obj) (obj).rbegin(), (obj).rend()
#define maxs(x, y) (x = max((x), (y)))
#define mins(x, y) (x = min((x), (y)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define uni(x) x.erase(unique(rng(x)), x.end())
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define dup(x, y) (((x) + (y)-1) / (y))
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vp = vector<pii>;
template <class T> inline istream &operator>>(istream &i, v(T) & v) {
  rep(j, v.size()) i >> v[j];
  return i;
}
template <class T> string join(const v(T) & v) {
  stringstream s;
  rep(i, v.size()) s << ' ' << v[i];
  return s.str().substr(1);
}
template <class T> inline ostream &operator<<(ostream &o, const v(T) & v) {
  if (v.size())
    o << join(v);
  return o;
}
template <class T1, class T2>
inline istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.fi >> v.se;
}
template <class T1, class T2>
inline ostream &operator<<(ostream &o, pair<T1, T2> &v) {
  return o << v.fi << "," << v.se;
}
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const double eps = 1e-10;
bool comp(pii a, pii b) { return a.se < b.se; }
//----------------------------------------------------------------

#define fileio                                                                 \
  ifstream fio;                                                                \
  if (argc >= 2) {                                                             \
    fio.open(argv[1]);                                                         \
    std::cin.rdbuf(fio.rdbuf());                                               \
  }
#define fastio ios_base::sync_with_stdio(false);
#define MOD 1000000007
//----------------------------------------------------------------
void Main() {
  int n;
  cin >> n;
  vl a(1);
  cin >> a[0];
  rep(i, n - 1) {
    ll aa;
    cin >> aa;
    if (aa != a.back())
      a.pb(aa);
  }
  ll ans = 1;
  if (a.size() > 2) {
    rep(i, a.size() - 2) {
      if ((a[i + 2] - a[i + 1]) * (a[i + 1] - a[i]) < 0) {
        ans++;
        ++i;
      }
    }
  }
  cout << ans << endl;
}
int main(int argc, char *argv[]) {
  fastio;
  fileio;
  Main();
}