prithivMLmods/Coder-Stat huggingface dataset:
Problem p03744 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long

void read(ll &rem) {
  rem = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    rem = rem * 10 + c - 48, c = getchar();
}

double tem[100050];
ll v[100050], cnt, head = 1;

int main() {
  ll T, L, nt, nv, ans = 0;
  bool flag = 0;
  read(T), read(L);
  while (T--) {
    scanf("%lld %lld", &nt, &nv);
    tem[++cnt] = nt;
    v[cnt] = nv;
    ans += nt * nv;
    ll p = 0;
    if (flag) {
      while (p + v[head] <= nv) {
        p += v[head], ans -= tem[head] * v[head], head++;
      }
      if (p < nv) {
        v[head] -= nv - p;
        ans -= (nv - p) * tem[head];
      }
    }
    printf("%.8f\n", 1.0 * ans / L);
    while (tem[cnt] <= tem[cnt - 1] && cnt - 1 >= head) {
      tem[cnt - 1] = 1.0 * (v[cnt] * tem[cnt] + v[cnt - 1] * tem[cnt - 1]) /
                     (v[cnt] + v[cnt - 1]);
      v[cnt - 1] += v[cnt];
      cnt--;
    }
    flag = 1;
  }
}

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

#define ll long long

void read(ll &rem) {
  rem = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    rem = rem * 10 + c - 48, c = getchar();
}

double tem[500050];
ll v[500050], cnt, head = 1;

int main() {
  ll T, L, nt, nv, ans = 0;
  bool flag = 0;
  read(T), read(L);
  while (T--) {
    scanf("%lld %lld", &nt, &nv);
    tem[++cnt] = nt;
    v[cnt] = nv;
    ans += nt * nv;
    ll p = 0;
    if (flag) {
      while (p + v[head] <= nv) {
        p += v[head], ans -= tem[head] * v[head], head++;
      }
      if (p < nv) {
        v[head] -= nv - p;
        ans -= (nv - p) * tem[head];
      }
    }
    printf("%.8f\n", 1.0 * ans / L);
    while (tem[cnt] <= tem[cnt - 1] && cnt - 1 >= head) {
      tem[cnt - 1] = 1.0 * (v[cnt] * tem[cnt] + v[cnt - 1] * tem[cnt - 1]) /
                     (v[cnt] + v[cnt - 1]);
      v[cnt - 1] += v[cnt];
      cnt--;
    }
    flag = 1;
  }
}
Problem p03744 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

#define fo(i, j, l) for (int i = j; i <= l; ++i)
#define fd(i, j, l) for (int i = j; i >= l; --i)

using namespace std;
typedef long long ll;
typedef double db;
const ll N = 1e5 + 7;

ll d[N], M;
db t[N];
int n;

inline ll max(ll a, ll b) { return a > b ? a : b; }

inline ll read() {
  ll o = 0;
  char ch = ' ';
  for (; ch < '0' || ch > '9'; ch = getchar())
    ;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    o = o * 10 + ch - 48;
  return o;
}

int main() {
  cin >> n >> M;
  int r = 0, l = 1, ok = 0;
  db zl = 0;
  fo(i, 1, n) {
    ll T = read(), V = read();
    d[++r] = V;
    t[r] = (db)T * V;
    zl += t[r];
    ok += d[r];
    for (; t[r] / d[r] < t[r - 1] / d[r - 1] && l < r; --r)
      if (d[r - 1] + d[r] <= M)
        t[r - 1] = t[r - 1] + t[r], d[r - 1] += d[r];
      else {
        t[r - 1] = t[r] + (t[r - 1] / d[r - 1]) * (M - d[r]);
        ok = d[r - 1] = M;
        zl = t[r - 1];
        --r;
        l = r;
        break;
      }
    for (; ok > M; ++l)
      if (ok - d[l] >= M)
        ok -= d[l], zl -= t[l];
      else {
        db every = t[l] / d[l];
        zl -= every * (ok - M);
        t[l] -= every * (ok - M);
        d[l] = d[l] - (ok - M);
        ok = M;
        break;
      }
    printf("%.7lf\n", zl / M);
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

#define fo(i, j, l) for (int i = j; i <= l; ++i)
#define fd(i, j, l) for (int i = j; i >= l; --i)

using namespace std;
typedef long long ll;
typedef double db;
const ll N = 6e5 + 7;

ll d[N], M;
db t[N];
int n;

inline ll max(ll a, ll b) { return a > b ? a : b; }

inline ll read() {
  ll o = 0;
  char ch = ' ';
  for (; ch < '0' || ch > '9'; ch = getchar())
    ;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    o = o * 10 + ch - 48;
  return o;
}

int main() {
  cin >> n >> M;
  int r = 0, l = 1, ok = 0;
  db zl = 0;
  fo(i, 1, n) {
    ll T = read(), V = read();
    d[++r] = V;
    t[r] = (db)T * V;
    zl += t[r];
    ok += d[r];
    for (; t[r] / d[r] < t[r - 1] / d[r - 1] && l < r; --r)
      if (d[r - 1] + d[r] <= M)
        t[r - 1] = t[r - 1] + t[r], d[r - 1] += d[r];
      else {
        t[r - 1] = t[r] + (t[r - 1] / d[r - 1]) * (M - d[r]);
        ok = d[r - 1] = M;
        zl = t[r - 1];
        --r;
        l = r;
        break;
      }
    for (; ok > M; ++l)
      if (ok - d[l] >= M)
        ok -= d[l], zl -= t[l];
      else {
        db every = t[l] / d[l];
        zl -= every * (ok - M);
        t[l] -= every * (ok - M);
        d[l] = d[l] - (ok - M);
        ok = M;
        break;
      }
    printf("%.7lf\n", zl / M);
  }
}
Problem p03744 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 100005
#define LL long long
using namespace std;
int n, l, r, L;
struct dot {
  LL x;
  double y;
  dot(LL _x = 0, double _y = 0) { x = _x, y = _y; }
} A[N], p;
dot operator+(const dot &p, const dot &q) { return dot(p.x + q.x, p.y + q.y); }
dot operator-(const dot &p, const dot &q) { return dot(p.x - q.x, p.y - q.y); }
dot operator*(const dot &p, double r) { return dot(p.x * r, p.y * r); }
dot operator*(double r, const dot &p) { return dot(p.x * r, p.y * r); }
double cj(dot p, dot q) { return p.x * q.y - p.y * q.x; }
dot Get(dot p, dot q, double x) {
  double k = (q.y - p.y) / (q.x - p.x);
  return dot(x, k * x + (p.y - k * p.x));
}
int main() {
  scanf("%d %d", &n, &L);
  A[l = r = 1] = dot(0, 0);
  while (n--) {
    scanf("%lf %lld", &p.y, &p.x);
    p.y *= p.x, A[r + 1] = A[r] + p, r++;
    while (l < r && A[r].x - A[l + 1].x > L)
      l++;
    if (A[r].x - A[l].x > L)
      A[l] = Get(A[l], A[l + 1], A[r].x - L);
    while (l + 1 < r && cj(A[r] - A[r - 1], A[r] - A[r - 2]) >= 0)
      A[r - 1] = A[r], r--;
    printf("%.7lf\n", (A[r].y - A[l].y) / L);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define N 500005
#define LL long long
using namespace std;
int n, l, r, L;
struct dot {
  LL x;
  double y;
  dot(LL _x = 0, double _y = 0) { x = _x, y = _y; }
} A[N], p;
dot operator+(const dot &p, const dot &q) { return dot(p.x + q.x, p.y + q.y); }
dot operator-(const dot &p, const dot &q) { return dot(p.x - q.x, p.y - q.y); }
dot operator*(const dot &p, double r) { return dot(p.x * r, p.y * r); }
dot operator*(double r, const dot &p) { return dot(p.x * r, p.y * r); }
double cj(dot p, dot q) { return p.x * q.y - p.y * q.x; }
dot Get(dot p, dot q, double x) {
  double k = (q.y - p.y) / (q.x - p.x);
  return dot(x, k * x + (p.y - k * p.x));
}
int main() {
  scanf("%d %d", &n, &L);
  A[l = r = 1] = dot(0, 0);
  while (n--) {
    scanf("%lf %lld", &p.y, &p.x);
    p.y *= p.x, A[r + 1] = A[r] + p, r++;
    while (l < r && A[r].x - A[l + 1].x > L)
      l++;
    if (A[r].x - A[l].x > L)
      A[l] = Get(A[l], A[l + 1], A[r].x - L);
    while (l + 1 < r && cj(A[r] - A[r - 1], A[r] - A[r - 2]) >= 0)
      A[r - 1] = A[r], r--;
    printf("%.7lf\n", (A[r].y - A[l].y) / L);
  }
  return 0;
}
Problem p03744 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define mset(x, y) memset(x, y, sizeof x)
#define mcpy(x, y) memcpy(x, y, sizeof x)
using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

inline int Read() {
  int x = 0, f = 1, c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  return x * f;
}

const int MAXN = 200005;

int n, ql, qr, cur, lim, x[MAXN];
double ret, y[MAXN];

int main() {
#ifdef wxh010910
  freopen("data.in", "r", stdin);
#endif
  n = Read(), lim = Read(), ql = 1;
  for (int i = 1; i <= n; i++) {
    int t = Read(), v = Read();
    while (cur + v > lim)
      if (cur + v - x[ql] >= lim)
        cur -= x[ql], ret -= y[ql], ql++;
      else {
        int t = cur + v - lim;
        double r = y[ql] / x[ql];
        x[ql] -= t, y[ql] -= t * r;
        cur -= t, ret -= t * r;
      }
    cur += v;
    ret += 1LL * v * t;
    printf("%lf\n", ret / cur);
    qr++, x[qr] = v, y[qr] = 1LL * v * t;
    while (ql < qr && y[qr] * x[qr - 1] < y[qr - 1] * x[qr])
      x[qr - 1] += x[qr], y[qr - 1] += y[qr], qr--;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define mset(x, y) memset(x, y, sizeof x)
#define mcpy(x, y) memcpy(x, y, sizeof x)
using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

inline int Read() {
  int x = 0, f = 1, c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  return x * f;
}

const int MAXN = 500005;

int n, ql, qr, cur, lim, x[MAXN];
double ret, y[MAXN];

int main() {
#ifdef wxh010910
  freopen("data.in", "r", stdin);
#endif
  n = Read(), lim = Read(), ql = 1;
  for (int i = 1; i <= n; i++) {
    int t = Read(), v = Read();
    while (cur + v > lim)
      if (cur + v - x[ql] >= lim)
        cur -= x[ql], ret -= y[ql], ql++;
      else {
        int t = cur + v - lim;
        double r = y[ql] / x[ql];
        x[ql] -= t, y[ql] -= t * r;
        cur -= t, ret -= t * r;
      }
    cur += v;
    ret += 1LL * v * t;
    printf("%lf\n", ret / cur);
    qr++, x[qr] = v, y[qr] = 1LL * v * t;
    while (ql < qr && y[qr] * x[qr - 1] < y[qr - 1] * x[qr])
      x[qr - 1] += x[qr], y[qr - 1] += y[qr], qr--;
  }
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, ini, n) for (int i = ini; i < n; i++)
#define _rep(i, ini, n) for (int i = ini; i >= n; i--)
#define ToEnd(a) a.begin(), a.end()
uint64_t MOD = 1000000007;

int CheckIncre(vector<int> V, int i) {
  int num;
  if (V.at(i + 1) > V.at(i))
    num = 1;
  else if (V.at(i + 1) == V.at(i))
    num = 0;
  else if (V.at(i + 1) < V.at(i))
    num = -1;
  return num;
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(n, 0, N) cin >> A.at(n);

  int ans = 1;
  int incre = CheckIncre(A, 0);
  rep(n, 0, N - 1) {
    int _incre = CheckIncre(A, n);
    if (incre * _incre == -1) {
      ans++;
      n++;
      if (n < N - 1)
        incre = CheckIncre(A, n);
    } else if (incre == 0)
      incre = _incre;
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, ini, n) for (int i = ini; i < n; i++)
#define _rep(i, ini, n) for (int i = ini; i >= n; i--)
#define ToEnd(a) a.begin(), a.end()
uint64_t MOD = 1000000007;

int CheckIncre(vector<int> V, int i) {
  int num;
  if (V.at(i + 1) > V.at(i))
    num = 1;
  else if (V.at(i + 1) == V.at(i))
    num = 0;
  else if (V.at(i + 1) < V.at(i))
    num = -1;
  return num;
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(n, 0, N) cin >> A.at(n);

  if (N == 1) {
    cout << 1 << endl;
    exit(0);
  }

  int ans = 1;
  int incre = CheckIncre(A, 0);
  rep(n, 0, N - 1) {
    int _incre = CheckIncre(A, n);
    if (incre * _incre == -1) {
      ans++;
      n++;
      if (n < N - 1)
        incre = CheckIncre(A, n);
    } else if (incre == 0)
      incre = _incre;
  }

  cout << ans << endl;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cstring>
#include <deque>
#include <forward_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <fstream>
#include <complex>
#define _USE_MATH_DEFINES
#include <cmath>
#include <math.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n; i > 0; i--)
#define arep(a, i, n) for (int i = (a); i < (n); i++)
#define SORT(v) sort((v).begin(), (v).end())
const ll INF = (1LL << 50);
typedef pair<int, int> P;

int main() {

  int N;
  cin >> N;

  vector<int> A(N);

  REP(i, N) cin >> A[i];

  int cnt = 0;
  int i = 0;
  while (true) {
    if (A[i + 1] == A[i]) {
      i++;
    } else if (A[i + 1] > A[i]) {
      while (A[i + 1] >= A[i]) {
        i++;
        if (i == N - 1) {
          cnt++;
          break;
        }
      }
      if (i == N - 1)
        break;
      cnt++;
      i++;
    } else {
      while (A[i + 1] <= A[i]) {
        i++;
        if (i == N - 1) {
          cnt++;
          break;
        }
      }
      if (i == N - 1)
        break;
      cnt++;
      i++;
    }

    if (i == N - 1) {
      cnt++;
      break;
    }
  }

  cout << cnt << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cstring>
#include <deque>
#include <forward_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <fstream>
#include <complex>
#define _USE_MATH_DEFINES
#include <cmath>
#include <math.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n; i > 0; i--)
#define arep(a, i, n) for (int i = (a); i < (n); i++)
#define SORT(v) sort((v).begin(), (v).end())
const ll INF = (1LL << 50);
typedef pair<int, int> P;

int main() {

  int N;
  cin >> N;

  vector<int> A(N);

  REP(i, N) cin >> A[i];

  if (N == 1) {
    cout << 1 << endl;
    return 0;
  }

  int cnt = 0;
  int i = 0;
  while (true) {
    if (A[i + 1] == A[i]) {
      i++;
    } else if (A[i + 1] > A[i]) {
      while (A[i + 1] >= A[i]) {
        i++;
        if (i == N - 1) {
          cnt++;
          break;
        }
      }
      if (i == N - 1)
        break;
      cnt++;
      i++;
    } else {
      while (A[i + 1] <= A[i]) {
        i++;
        if (i == N - 1) {
          cnt++;
          break;
        }
      }
      if (i == N - 1)
        break;
      cnt++;
      i++;
    }

    if (i == N - 1) {
      cnt++;
      break;
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define rep(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define irep(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define rrep(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, 0, n) { cin >> a[i]; }

  if (n <= 2)
    return 1;

  int cnt = 0;
  int flag = 0;
  rep(i, 1, n) {
    if (a[i] > a[i - 1]) {
      if (flag == 0)
        flag = 1;
      else if (flag == -1) {
        cnt++;
        flag = 0;
      }
    } else if (a[i] < a[i - 1]) {
      if (flag == 0) {
        flag = -1;
      } else if (flag == 1) {
        cnt++;
        flag = 0;
      }
    }
  }
  cnt++;
  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define rep(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define irep(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define rrep(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, 0, n) { cin >> a[i]; }

  if (n <= 2) {
    cout << 1 << endl;
    return 0;
  }

  int cnt = 0;
  int flag = 0;
  rep(i, 1, n) {
    if (a[i] > a[i - 1]) {
      if (flag == 0)
        flag = 1;
      else if (flag == -1) {
        cnt++;
        flag = 0;
      }
    } else if (a[i] < a[i - 1]) {
      if (flag == 0) {
        flag = -1;
      } else if (flag == 1) {
        cnt++;
        flag = 0;
      }
    }
  }
  cnt++;
  cout << cnt << endl;
  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> sub;
  rep(i, n - 1) {
    if (a[i] < a[i + 1]) {
      sub.push_back(1);
    } else if (a[i] > a[i + 1]) {
      sub.push_back(-1);
    }
  }

  int ans = 1;
  rep(i, sub.size() - 1) if (sub[i] != sub[i + 1]) ans++, i++;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<int> sub;
  rep(i, n - 1) {
    if (a[i] < a[i + 1]) {
      sub.push_back(1);
    } else if (a[i] > a[i + 1]) {
      sub.push_back(-1);
    }
  }

  int ans = 1;
  if (sub.size() > 1)
    rep(i, sub.size() - 1) if (sub[i] != sub[i + 1]) ans++, i++;
  cout << ans << endl;
  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h> // ver1.9.5
#define int long long
#define ALL(v) (v).begin(), (v).end()
#define Vi vector<int>
#define Pii pair<int, int>
#define pb push_back
#define mp make_pair
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repeq(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = a; i > b; i--)
#define repreq(i, a, b) for (int i = a; i >= b; i--)
#define leng(n) (int)(log10(n) + 1)
#define Vout(a)                                                                \
  rep(i, 0, a.size() - 1) cout << a[i] << ' ';                                 \
  cout << a[a.size() - 1] << endl
#define INF 9000000000000000000 //  9.0 * 10^18
#define MOD 1000000007          //  10^9 + 7
using namespace std;

signed main() {
  cout << fixed << setprecision(8);
  int n, qw, er;
  cin >> n;
  Vi a(0);
  cin >> qw;

  rep(i, 0, n - 1) {
    er = qw;
    cin >> qw;
    if (er > qw)
      a.pb(-1);
    else if (er < qw)
      a.pb(1);
  }

  int ans = 1, now = a[0];
  rep(i, 0, a.size()) {
    if (now == 0) {
      now = a[i];
      continue;
    }
    if (now == a[i])
      continue;
    else {
      ans++;
      now = 0;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h> // ver1.9.5
#define int long long
#define ALL(v) (v).begin(), (v).end()
#define Vi vector<int>
#define Pii pair<int, int>
#define pb push_back
#define mp make_pair
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repeq(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = a; i > b; i--)
#define repreq(i, a, b) for (int i = a; i >= b; i--)
#define leng(n) (int)(log10(n) + 1)
#define Vout(a)                                                                \
  rep(i, 0, a.size() - 1) cout << a[i] << ' ';                                 \
  cout << a[a.size() - 1] << endl
#define INF 9000000000000000000 //  9.0 * 10^18
#define MOD 1000000007          //  10^9 + 7
using namespace std;

signed main() {
  cout << fixed << setprecision(8);
  int n, qw, er;
  cin >> n;
  Vi a(0);
  cin >> qw;
  if (n == 1 || n == 2) {
    cout << 1 << endl;
    return 0;
  }
  rep(i, 0, n - 1) {
    er = qw;
    cin >> qw;
    if (er > qw)
      a.pb(-1);
    else if (er < qw)
      a.pb(1);
  }

  int ans = 1, now = a[0];
  rep(i, 0, a.size()) {
    if (now == 0) {
      now = a[i];
      continue;
    }
    if (now == a[i])
      continue;
    else {
      ans++;
      now = 0;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define REPS(i, n) for (int i = 1; i <= (n); i++)
#define RREPS(i, n) for (int i = (n); i > 0; i--)
#define mp make_pair
#define pb(a) push_back(a)
typedef long long ll;

int main() {
  int N;
  vector<ll> A(1);
  cin >> N;
  cin >> A[0];
  REP(i, N - 1) {
    ll in;
    cin >> in;
    if (A.back() != in)
      A.pb(in);
  }
  int ans = 1;
  REPS(i, A.size() - 2) {
    if ((A[i] - A[i - 1]) * (A[i + 1] - A[i]) < 0) {
      ans++;
      i++;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define REPS(i, n) for (int i = 1; i <= (n); i++)
#define RREPS(i, n) for (int i = (n); i > 0; i--)
#define mp make_pair
#define pb(a) push_back(a)
typedef long long ll;

int main() {
  int N;
  vector<ll> A(1);
  cin >> N;
  cin >> A[0];
  REP(i, N - 1) {
    ll in;
    cin >> in;
    if (A.back() != in)
      A.pb(in);
  }
  int ans = 1;
  REPS(i, (int)(A.size()) - 2) {
    if ((A[i] - A[i - 1]) * (A[i + 1] - A[i]) < 0) {
      ans++;
      i++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define rep(i, n) reps(i, 0, n)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;

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

  vi a(n);
  rep(i, n) cin >> a[i];
  string s;
  rep(i, n - 1) {
    if (a[i] > a[i + 1])
      s += '>'; // cout << ">";
    else if (a[i] < a[i + 1])
      s += '<'; // cout << "<";
    // else s+="="; //cout << "=";
  }
  int ans = 1;
  if (s.length() == 1) {
    cout << 1 << endl;
    return 0;
  }
  rep(i, s.length() - 1) {
    if (s[i] == '>' && s[i + 1] == '<') {
      ans++;
      i++;
    } else if (s[i] == '<' && s[i + 1] == '>') {
      ans++;
      i++;
    }
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define rep(i, n) reps(i, 0, n)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;

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

  vi a(n);
  rep(i, n) cin >> a[i];
  string s;
  rep(i, n - 1) {
    if (a[i] > a[i + 1])
      s += '>'; // cout << ">";
    else if (a[i] < a[i + 1])
      s += '<'; // cout << "<";
    // else s+="="; //cout << "=";
  }
  int ans = 1;
  if (s.length() == 0) {
    cout << 1 << endl;
    return 0;
  }
  rep(i, s.length() - 1) {
    if (s[i] == '>' && s[i + 1] == '<') {
      ans++;
      i++;
    } else if (s[i] == '<' && s[i + 1] == '>') {
      ans++;
      i++;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {

  int n;
  cin >> n;
  vector<int> a(n);

  for (int i = 0; i < n; i++)
    cin >> a.at(i);

  int diff = a.at(1) - a.at(0);
  int ans = 1;
  for (int i = 2; i < n; i++) {

    if (diff > 0) {
      if (a.at(i) < a.at(i - 1)) {
        ans++;
        diff = 0;
      }
    } else if (diff < 0) {
      if (a.at(i) > a.at(i - 1)) {
        ans++;
        diff = 0;
      }
    } else {
      diff = a.at(i) - a.at(i - 1);
    }
  }

  cout << ans << endl;
}


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

int main() {

  int n;
  cin >> n;
  vector<int> a(n);

  for (int i = 0; i < n; i++)
    cin >> a.at(i);

  int diff;
  if (n != 1)
    diff = a.at(1) - a.at(0);
  int ans = 1;
  for (int i = 2; i < n; i++) {

    if (diff > 0) {
      if (a.at(i) < a.at(i - 1)) {
        ans++;
        diff = 0;
      }
    } else if (diff < 0) {
      if (a.at(i) > a.at(i - 1)) {
        ans++;
        diff = 0;
      }
    } else {
      diff = a.at(i) - a.at(i - 1);
    }
  }

  cout << ans << endl;
}
Problem p03745 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
A = list(map(int, input().split))
# N = 9
# A = [1, 2, 1, 2, 1, 2, 1, 2, 1]
# N = 7
# A = [1, 2, 3, 2, 1, 999999999, 1000000000]
# N = 6
# A = [1, 2, 3, 2, 2, 1]
# N = 2
# A = [1, 1]
A = A + [A[-1]]

d = 0
i = 0

inc = None
while i < N:
    if inc is None:
        if A[i] < A[i + 1]:
            inc = True
            d += 1
        elif A[i] > A[i + 1]:
            inc = False
            d += 1
        else:
            inc = None
    else:
        if inc:
            if A[i] > A[i + 1]:
                inc = None
        else:
            if A[i] < A[i + 1]:
                inc = None
    i += 1

if inc is None:
    d += 1
print(d)


Fixed submission:
N = int(input())
A = list(map(int, input().split()))
# N = 9
# A = [1, 2, 1, 2, 1, 2, 1, 2, 1]
# N = 7
# A = [1, 2, 3, 2, 1, 999999999, 1000000000]
# N = 6
# A = [1, 2, 3, 2, 2, 1]
# N = 2
# A = [1, 1]
A = A + [A[-1]]

d = 0
i = 0

inc = None
while i < N:
    if inc is None:
        if A[i] < A[i + 1]:
            inc = True
            d += 1
        elif A[i] > A[i + 1]:
            inc = False
            d += 1
        else:
            inc = None
    else:
        if inc:
            if A[i] > A[i + 1]:
                inc = None
        else:
            if A[i] < A[i + 1]:
                inc = None
    i += 1

if inc is None:
    d += 1
print(d)
Problem p03745 (Python)

Buggy submission (status: Runtime Error):
def upper_or_lower(i):
    if A[i] < A[i + 1]:
        return "upper"
    elif A[i] > A[i + 1]:
        return "lower"
    else:
        return upper_or_lower(i + 1)


N = int(input())
A = list(map(int, input().split()))

state = "upper"  # state が upper か lower で判定
ans = 1

# i = 0 の処理
if A[0] > A[1]:
    state = "lower"

for i in range(2, N):
    if (state == "upper") and (A[i - 1] > A[i]):
        ans += 1
        if i < N - 1:
            state = upper_or_lower(i)
    elif (state == "lower") and (A[i - 1] < A[i]):
        ans += 1
        if i < N - 1:
            state = upper_or_lower(i)
    else:
        continue

print(ans)


Fixed submission:
def upper_or_lower(i):
    if A[i] < A[i + 1]:
        return "upper"
    elif A[i] > A[i + 1]:
        return "lower"
    else:
        return upper_or_lower(i + 1)


N = int(input())
A = list(map(int, input().split()))

state = "upper"  # state が upper か lower で判定
ans = 1

# i = 0 の処理
if (len(A) > 2) and (A[0] > A[1]):
    state = "lower"

for i in range(2, N):
    if (state == "upper") and (A[i - 1] > A[i]):
        ans += 1
        if i < N - 1:
            state = upper_or_lower(i)
    elif (state == "lower") and (A[i - 1] < A[i]):
        ans += 1
        if i < N - 1:
            state = upper_or_lower(i)
    else:
        continue

print(ans)
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

// macro
#define rep(i, n) for (int i = 0; i < n; i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  int i;
  rep(i, n) cin >> a[i];
  vector<int> dis(n - 1);
  rep(i, n) dis[i] = a[i + 1] - a[i];
  bool pos = false;
  bool neg = false;
  int ans = 1;
  int lend = 0;
  rep(i, n - 1) {
    if (dis[i] > 0 && !neg)
      pos = true;
    else if (dis[i] > 0) {
      neg = false;
      pos = false;
      ans++;
    }
    if (dis[i] < 0 && !pos)
      neg = true;
    else if (dis[i] < 0) {
      neg = false;
      pos = false;
      ans++;
    }
  }
  cout << ans << endl;
}


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

// macro
#define rep(i, n) for (int i = 0; i < n; i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  int i;
  rep(i, n) cin >> a[i];
  vector<int> dis(n - 1);
  rep(i, n - 1) dis[i] = a[i + 1] - a[i];
  bool pos = false;
  bool neg = false;
  int ans = 1;
  int lend = 0;
  rep(i, n - 1) {
    if (dis[i] > 0 && !neg)
      pos = true;
    else if (dis[i] > 0) {
      neg = false;
      pos = false;
      ans++;
    }
    if (dis[i] < 0 && !pos)
      neg = true;
    else if (dis[i] < 0) {
      neg = false;
      pos = false;
      ans++;
    }
  }
  cout << ans << endl;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int N;
vector<int> A;
vector<int> B;
int main() {
  cin >> N;
  A.resize(N);
  B.resize(N);
  rep(i, N) cin >> A[i];
  rep(i, N) if (i) B[i] = A[i] - A[i - 1];
  vector<int> C;
  rep(i, N) if (B[i]) C.push_back(B[i]);

  // rep(i, C.size()) cout << C[i] << " ";
  // cout << endl;
  bool sign = C[0] > 0;
  int ans = 0;
  rep(i, C.size()) {
    if (sign && C[i] < 0)
      ans++, i++;
    if (!sign && C[i] > 0)
      ans++, i++;
    if (i >= C.size())
      break;
    sign = C[i] > 0;
  }
  cout << ans + 1 << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int N;
vector<int> A;
vector<int> B;
int main() {
  cin >> N;
  A.resize(N);
  B.resize(N);
  rep(i, N) cin >> A[i];
  rep(i, N) if (i) B[i] = A[i] - A[i - 1];
  vector<int> C;
  rep(i, N) if (B[i]) C.push_back(B[i]);

  // rep(i, C.size()) cout << C[i] << " ";
  // cout << endl;
  if (C.size() == 0) {
    cout << "1" << endl;
    return 0;
  }
  bool sign = C[0] > 0;
  int ans = 0;
  rep(i, C.size()) {
    if (sign && C[i] < 0)
      ans++, i++;
    if (!sign && C[i] > 0)
      ans++, i++;
    if (i >= C.size())
      break;
    sign = C[i] > 0;
  }
  cout << ans + 1 << endl;

  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int INF = 1 << 29;
const long long int Mod = 1000000007;
using ll = long long int;
#define all(x) (x).begin(), (x).end()
#define rep(i, N) for (ll i = 0; i < (ll)N; ++i)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)

ll a[10010];

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

  rep(i, n) cin >> a[i];

  int change = 0;
  int ans = 0;
  rep(i, n - 1) {
    if (change == 0) {
      if (a[i] < a[i + 1])
        change = 1;
      else if (a[i] > a[i + 1])
        change = -1;
    } else if (change == 1) {
      if (a[i] > a[i + 1]) {
        ++ans;
        change = 0;
      }
    } else {
      if (a[i] < a[i + 1]) {
        ++ans;
        change = 0;
      }
    }
  }
  if (change == 0)
    ++ans;
  else if (change == 1) {
    if (a[n - 2] <= a[n - 1]) {
      ++ans;
    } else {
      ans += 2;
    }
  } else {
    if (a[n - 2] >= a[n - 1])
      ++ans;
    else
      ans += 2;
  }

  cout << ans << endl;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int INF = 1 << 29;
const long long int Mod = 1000000007;
using ll = long long int;
#define all(x) (x).begin(), (x).end()
#define rep(i, N) for (ll i = 0; i < (ll)N; ++i)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)

ll a[100010];

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

  rep(i, n) cin >> a[i];

  int change = 0;
  int ans = 0;
  rep(i, n - 1) {
    if (change == 0) {
      if (a[i] < a[i + 1])
        change = 1;
      else if (a[i] > a[i + 1])
        change = -1;
    } else if (change == 1) {
      if (a[i] > a[i + 1]) {
        ++ans;
        change = 0;
      }
    } else {
      if (a[i] < a[i + 1]) {
        ++ans;
        change = 0;
      }
    }
  }
  if (change == 0)
    ++ans;
  else if (change == 1) {
    if (a[n - 2] <= a[n - 1]) {
      ++ans;
    } else {
      ans += 2;
    }
  } else {
    if (a[n - 2] >= a[n - 1])
      ++ans;
    else
      ans += 2;
  }

  cout << ans << endl;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int an[11000];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &an[i]);
  int cnt = 1;
  bool decr = true;
  bool fir = true;
  for (int i = 2; i <= n; i++) {
    if (fir) {
      fir = false;
      if (an[i - 1] < an[i])
        decr = true;
      else if (an[i - 1] > an[i])
        decr = false;
      else
        fir = true;
    } else {
      if (decr) {
        if (an[i] < an[i - 1]) {
          cnt++;
          fir = true;
        }
      } else {
        if (an[i] > an[i - 1]) {
          cnt++;
          fir = true;
        }
      }
    }
    //        printf("i=%d %d\n",i,cnt);
  }
  printf("%d\n", cnt);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int an[101000];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &an[i]);
  int cnt = 1;
  bool decr = true;
  bool fir = true;
  for (int i = 2; i <= n; i++) {
    if (fir) {
      fir = false;
      if (an[i - 1] < an[i])
        decr = true;
      else if (an[i - 1] > an[i])
        decr = false;
      else
        fir = true;
    } else {
      if (decr) {
        if (an[i] < an[i - 1]) {
          cnt++;
          fir = true;
        }
      } else {
        if (an[i] > an[i - 1]) {
          cnt++;
          fir = true;
        }
      }
    }
    //        printf("i=%d %d\n",i,cnt);
  }
  printf("%d\n", cnt);
  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;

int main(int argc, const char *argv[]) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;

  vector<ll> a(n), b;

  for (ll i = 0; i < n; i++)
    cin >> a[i];

  for (ll i = 0; i < n - 1; i++) {
    if (a[i + 1] == a[i])
      continue;
    b.push_back(a[i + 1] - a[i]);
  }

  ll ans = 0;
  ll t = 0;
  for (ll i = 0; i < n - 2; i++) {

    if (t != 0) {
      t = 0;
      continue;
    }
    if (b[i + 1] * b[i] < 0) {
      ++t;
      ++ans;
    }
  }

  cout << ans + 1 << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;

int main(int argc, const char *argv[]) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;

  vector<ll> a(n), b;

  for (ll i = 0; i < n; i++)
    cin >> a[i];

  for (ll i = 0; i < n - 1; i++) {
    if (a[i + 1] == a[i])
      continue;
    b.push_back(a[i + 1] - a[i]);
  }

  ll ans = 0;
  ll t = 0;
  ll m = b.size();
  for (ll i = 0; i < m - 1; i++) {

    if (t != 0) {
      t = 0;
      continue;
    }
    if (b[i + 1] * b[i] < 0) {
      ++t;
      ++ans;
    }
  }

  cout << ans + 1 << endl;

  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main(void) {
  int N;
  cin >> N;

  int a[10010];
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  int status = 0;
  int cnt = 1;
  for (int i = 0; i < N - 1; i++) {
    if (a[i] < a[i + 1]) {
      if (status == -1) {
        cnt++;
        status = 0;
      } else {
        status = 1;
      }
    }
    if (a[i] > a[i + 1]) {
      if (status == 1) {
        cnt++;
        status = 0;
      } else {
        status = -1;
      }
    }
  }

  cout << cnt << endl;

  return 0;
}

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

int main(void) {
  int N;
  cin >> N;

  int a[100100];
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  int status = 0;
  int cnt = 1;
  for (int i = 0; i < N - 1; i++) {
    if (a[i] < a[i + 1]) {
      if (status == -1) {
        cnt++;
        status = 0;
      } else {
        status = 1;
      }
    }
    if (a[i] > a[i + 1]) {
      if (status == 1) {
        cnt++;
        status = 0;
      } else {
        status = -1;
      }
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  // ios::sync_with_stdio(false);
  int n;
  cin >> n;
  scanf("%d", &n);
  vector<int> v(n);
  for (int i = 0; i < n; ++i)
    scanf("%d", &v[i]);

  if (n <= 2) {
    printf("1\n");
    return 0;
  }
  int ct = 0;
  int dir = 0;
  for (int i = 1; i < n; ++i) {
    if (v[i] > v[i - 1]) {
      if (dir < 0) {
        ct++;
        dir = 0;
      } else
        dir = 1;
    } else if (v[i] < v[i - 1]) {
      if (dir > 0) {
        ct++;
        dir = 0;
      } else
        dir = -1;
    }
  }

  ct++;
  printf("%d\n", ct);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  // ios::sync_with_stdio(false);
  int n;
  scanf("%d", &n);
  vector<int> v(n);
  for (int i = 0; i < n; ++i)
    scanf("%d", &v[i]);

  if (n <= 2) {
    printf("1\n");
    return 0;
  }
  int ct = 0;
  int dir = 0;
  for (int i = 1; i < n; ++i) {
    if (v[i] > v[i - 1]) {
      if (dir < 0) {
        ct++;
        dir = 0;
      } else
        dir = 1;
    } else if (v[i] < v[i - 1]) {
      if (dir > 0) {
        ct++;
        dir = 0;
      } else
        dir = -1;
    }
  }

  ct++;
  printf("%d\n", ct);
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
int dy[] = {1, -1, 0, 0};
int dx[] = {0, 0, 1, -1};

int main() {
  int N;
  cin >> N;

  vector<ll> a(N);
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }
  int ans = 1;
  int fugo = 0;
  for (int i = 1; i < N; ++i) {
    if (fugo == 0) {
      fugo = abs(a[i] - a[i - 1]) / (a[i] - a[i - 1]);
      continue;
    }

    if (fugo * (a[i] - a[i - 1]) >= 0)
      continue;
    fugo = 0;
    ans++;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
int dy[] = {1, -1, 0, 0};
int dx[] = {0, 0, 1, -1};

int main() {
  int N;
  cin >> N;

  vector<ll> a(N);
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }
  int ans = 1;
  int fugo = 0;
  for (int i = 1; i < N; ++i) {
    if (fugo == 0) {

      fugo = a[i] - a[i - 1];
      continue;
    }

    if (fugo * (a[i] - a[i - 1]) >= 0)
      continue;
    fugo = 0;
    ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
/* it was worth becoming a chemist */
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ld;
typedef unsigned int uni;
typedef unsigned long long unll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef vector<int> vi;
typedef vector<long long> vll;

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define NAME "puts"
#define F first
#define S second

const ll INF = 1e18;
const int inf = 1e9;
const int mod = 1e9 + 7;
const db EPS = (db)1e-9;
const db pi = acos(-1.0);
const int MAXN = 1e5 + 5;

ll n, a[MAXN], res;

int main() {
#ifndef ONLINE_JUDGE
  freopen(NAME ".in", "r", stdin);
  freopen(NAME ".out", "w", stdout);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(9) << fixed;

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  ll l = 1;
  while (l <= n) {
    ll r1 = l + 1, r2 = l + 1;
    while (a[r1] >= a[r1 - 1] && r1 <= n)
      r1++;
    while (a[r2] <= a[r2 - 1] && r2 <= n)
      r2++;
    l = max(r1, r2);
    res++;
  }
  cout << res;
  return 0;
}


Fixed submission:
/* it was worth becoming a chemist */
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ld;
typedef unsigned int uni;
typedef unsigned long long unll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef vector<int> vi;
typedef vector<long long> vll;

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define NAME "puts"
#define F first
#define S second

const ll INF = 1e18;
const int inf = 1e9;
const int mod = 1e9 + 7;
const db EPS = (db)1e-9;
const db pi = acos(-1.0);
const int MAXN = 1e5 + 5;

ll n, a[MAXN], res;

int main() {

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  ll l = 1;
  while (l <= n) {
    ll r1 = l + 1, r2 = l + 1;
    while (a[r1] >= a[r1 - 1] && r1 <= n)
      r1++;
    while (a[r2] <= a[r2 - 1] && r2 <= n)
      r2++;
    l = max(r1, r2);
    res++;
  }
  cout << res;
  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

using pint = pair<int, int>;
using tint = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N;
  cin >> N;
  vint A;
  rep(i, N) {
    int a;
    cin >> a;
    if (A.empty() || A.back() != a)
      A.push_back(a);
  }
  if (N == 1) {
    cout << 1 << endl;
    return 0;
  }
  if (A[N - 2] < A[A.size() - 1])
    A.push_back(A[A.size() - 1] + 1);
  else
    A.push_back(A[A.size() - 1] - 1);
  bool up = A[0] < A[1];
  int cnt = 1;
  reps(i, 1, A.size()) {
    // cout<<i<<" "<<up<<" "<<A[i-1]<<" "<<A[i]<<endl;
    if (up) {
      if (A[i - 1] > A[i])
        i++, cnt++, up = A[i - 1] < A[i];
    } else {
      if (A[i - 1] < A[i])
        i++, cnt++, up = A[i - 1] < A[i];
    }
  }
  cout << cnt << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

using pint = pair<int, int>;
using tint = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N;
  cin >> N;
  vint A;
  rep(i, N) {
    int a;
    cin >> a;
    if (A.empty() || A.back() != a)
      A.push_back(a);
  }
  if (N == 1) {
    cout << 1 << endl;
    return 0;
  }
  if (A[A.size() - 2] < A[A.size() - 1])
    A.push_back(A[A.size() - 1] + 1);
  else
    A.push_back(A[A.size() - 1] - 1);
  bool up = A[0] < A[1];
  int cnt = 1;
  reps(i, 1, A.size()) {
    // cout<<i<<" "<<up<<" "<<A[i-1]<<" "<<A[i]<<endl;
    if (up) {
      if (A[i - 1] > A[i])
        i++, cnt++, up = A[i - 1] < A[i];
    } else {
      if (A[i - 1] < A[i])
        i++, cnt++, up = A[i - 1] < A[i];
    }
  }
  cout << cnt << endl;

  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int N;
  cin >> N;
  vector<ll> a(N), b;
  for (int i = 0; i < N; i++)
    cin >> a[i];
  for (int i = 0; i < N - 1; i++) {
    ll dif = a[i + 1] - a[i];
    if (dif == 0)
      continue;
    b.push_back(dif);
  }
  int ans = 1;
  for (int i = 0; i < b.size() - 1; i++) {
    if (b[i] * b[i + 1] < 0) {
      ans++;
      i++;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int N;
  cin >> N;
  if (N <= 2) {
    cout << 1 << endl;
    return 0;
  }
  vector<ll> a(N), b;
  for (int i = 0; i < N; i++)
    cin >> a[i];
  for (int i = 0; i < N - 1; i++) {
    ll dif = a[i + 1] - a[i];
    if (dif == 0)
      continue;
    b.push_back(dif);
  }
  int ans = 1;
  for (int i = 0; i < b.size() - 1; i++) {
    if (b[i] * b[i + 1] < 0) {
      ans++;
      i++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03745 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int assign(int, int);

int main() {
  long n;
  cin >> n;

  long i = 0;
  long input, a[n];
  while (cin >> input) {
    a[i] = input;
    i++;
  }

  long sum = 1;
  int sign;
  for (i = 0; i < n - 2; i++) {
    sign = assign(a[i], a[i + 1]);
    for (int j = i + 1; j < n - 1; j++) {
      if ((sign == 1 && assign(a[j], a[j + 1]) == -1) ||
          (sign == -1 && assign(a[j], a[j + 1]) == 1)) {
        sum++;
        i = j;
        break;
      } else if (sign == 0) {
        sign = assign(a[j], a[j + 1]);
      }
      if (j == n - 2)
        break;
    }
  }
  cout << sum << endl;
  return 0;
}

int assign(int x, int y) {
  if (x > y)
    return -1;
  else if (x < y)
    return 1;
  else
    return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int assign(int, int);

int main() {
  long n;
  cin >> n;

  long i = 0;
  long input, a[n];
  while (cin >> input) {
    a[i] = input;
    i++;
  }

  long sum = 1;
  int sign;
  for (i = 0; i < n - 2; i++) {
    sign = assign(a[i], a[i + 1]);
    for (int j = i + 1; j < n - 1; j++) {
      if ((sign == 1 && assign(a[j], a[j + 1]) == -1) ||
          (sign == -1 && assign(a[j], a[j + 1]) == 1)) {
        sum++;
        i = j;
        break;
      } else if (sign == 0) {
        sign = assign(a[j], a[j + 1]);
      }
      if (j == n - 2) {
        cout << sum << endl;
        return 0;
      };
    }
  }
  cout << sum << endl;
  return 0;
}

int assign(int x, int y) {
  if (x > y)
    return -1;
  else if (x < y)
    return 1;
  else
    return 0;
}