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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

vec<int> mult(vec<int> &A, vec<int> &B) {
  vec<int> C(A.size());
  for (int i = 0; i < A.size(); i++)
    C[i] = B[A[i]];
  return C;
}

vec<int> pow(vec<int> &A, ll n) {
  if (n == 1)
    return A;
  vec<int> R = pow(A, n >> 1);
  R = mult(R, R);
  if (n & 1)
    R = mult(A, R);
  return R;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vec<int> X(N);
  vec<int> D(N - 1);
  for (int i = 0; i < N; i++) {
    cin >> X[i];
    if (i)
      D[i - 1] = X[i] - X[i - 1];
  }
  vec<int> perm(N - 1);
  iota(perm.begin(), perm.end(), 0);
  int M;
  ll K;
  cin >> M >> K;
  for (int i = 0; i < M; i++) {
    int a;
    cin >> a;
    a -= 2;
    swap(perm[a], perm[a + 1]);
  }
  auto res = pow(perm, K);
  ll s = X[0];
  for (int i = 0; i < N; i++) {
    cout << s << "\n";
    s += D[res[i]];
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

vec<int> mult(vec<int> &A, vec<int> &B) {
  vec<int> C(A.size());
  for (int i = 0; i < A.size(); i++)
    C[i] = B[A[i]];
  return C;
}

vec<int> pow(vec<int> &A, ll n) {
  if (n == 1)
    return A;
  vec<int> R = pow(A, n >> 1);
  R = mult(R, R);
  if (n & 1)
    R = mult(A, R);
  return R;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vec<int> X(N);
  vec<int> D(N - 1);
  for (int i = 0; i < N; i++) {
    cin >> X[i];
    if (i)
      D[i - 1] = X[i] - X[i - 1];
  }
  vec<int> perm(N - 1);
  iota(perm.begin(), perm.end(), 0);
  int M;
  ll K;
  cin >> M >> K;
  for (int i = 0; i < M; i++) {
    int a;
    cin >> a;
    a -= 2;
    swap(perm[a], perm[a + 1]);
  }
  auto res = pow(perm, K);
  ll s = X[0];
  for (int i = 0; i < N; i++) {
    cout << s << "\n";
    if (i != N - 1)
      s += D[res[i]];
  }
}
Problem p03953 (C++)

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

int N;
#define MAX_N 100000
int X[MAX_N], D[MAX_N - 1];
int M;
long long K;
int P[MAX_N - 1];
bool Seen[MAX_N - 1];
vector<vector<int>> L;
int OL[MAX_N - 1], IL[MAX_N - 1];

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i)
    cin >> X[i];
  for (int i = 0; i < N - 1; ++i)
    D[i] = X[i + 1] - X[i];
  cin >> M >> K;
  for (int i = 0; i < N - 1; ++i)
    P[i] = i;
  for (int i = 0; i < M; ++i) {
    int a;
    cin >> a;
    swap(P[a - 2], P[a - 1]);
  }
  for (int i = 0; i < N - 1; ++i) {
    if (!Seen[i]) {
      int j = i;
      int ol = L.size();
      L.emplace_back();
      do {
        Seen[j] = true;
        OL[j] = ol;
        IL[j] = L[ol].size();
        L[ol].push_back(j);
        j = P[j];
      } while (j != i);
    }
  }
  cout << X[0] << endl;
  long long tmp = X[0];
  for (int i = 0; i < N - 1; ++i) {
    vector<int> loop = L[OL[i]];
    tmp += D[loop[(IL[i] + K) % loop.size()]];
    cout << tmp << endl;
  }
  return 0;
}


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

int N;
#define MAX_N 100000
int X[MAX_N], D[MAX_N - 1];
int M;
long long K;
int P[MAX_N - 1];
bool Seen[MAX_N - 1];
vector<vector<int>> L;
int OL[MAX_N - 1], IL[MAX_N - 1];

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i)
    cin >> X[i];
  for (int i = 0; i < N - 1; ++i)
    D[i] = X[i + 1] - X[i];
  cin >> M >> K;
  for (int i = 0; i < N - 1; ++i)
    P[i] = i;
  for (int i = 0; i < M; ++i) {
    int a;
    cin >> a;
    swap(P[a - 2], P[a - 1]);
  }
  for (int i = 0; i < N - 1; ++i) {
    if (!Seen[i]) {
      int j = i;
      int ol = L.size();
      L.emplace_back();
      do {
        Seen[j] = true;
        OL[j] = ol;
        IL[j] = L[ol].size();
        L[ol].push_back(j);
        j = P[j];
      } while (j != i);
    }
  }
  cout << X[0] << endl;
  long long tmp = X[0];
  for (int i = 0; i < N - 1; ++i) {
    vector<int> &loop = L[OL[i]];
    tmp += D[loop[(IL[i] + K) % loop.size()]];
    cout << tmp << endl;
  }
  return 0;
}
Problem p03953 (C++)

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

using Lint = long long;

vector<int> mul(vector<int> &a, vector<int> &b) {
  const int N = a.size();
  vector<int> res(N);
  for (int i = 0; i < N; i++)
    res[i] = b[a[i]];
  return res;
}

vector<int> pow(vector<int> &p, Lint K) {
  const int N = p.size();
  vector<int> res(N);
  for (int i = 0; i < N; i++)
    res[i] = i;
  while (K > 0) {
    if (K & 1)
      res = mul(res, p);
    p = mul(p, p);
    K >>= 1;
  }
  return res;
}

int main() {
  int N;
  cin >> N;
  vector<Lint> x(N);
  for (int i = 0; i < N; i++)
    cin >> x[i];
  Lint M, K;
  cin >> M >> K;
  vector<int> a(M);
  for (int i = 0; i < N; i++)
    cin >> a[i], a[i]--;

  const int sz = N - 1;
  vector<int> d(sz);
  for (int i = 0; i < sz; i++)
    d[i] = x[i + 1] - x[i];

  vector<int> p(sz);
  for (int i = 0; i < sz; i++)
    p[i] = i;
  for (int i = 0; i < M; i++) {
    swap(p[a[i] - 1], p[a[i]]);
  }
  vector<int> res = pow(p, K);
  for (int i = 0; i < sz; i++) {
    x[i + 1] = x[i] + d[res[i]];
  }
  for (int i = 0; i < N; i++)
    cout << x[i] << endl;
  return 0;
}


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

using Lint = long long;

vector<int> mul(vector<int> &a, vector<int> &b) {
  const int N = a.size();
  vector<int> res(N);
  for (int i = 0; i < N; i++)
    res[i] = b[a[i]];
  return res;
}

vector<int> pow(vector<int> &p, Lint K) {
  const int N = p.size();
  vector<int> res(N);
  for (int i = 0; i < N; i++)
    res[i] = i;
  while (K > 0) {
    if (K & 1)
      res = mul(res, p);
    p = mul(p, p);
    K >>= 1;
  }
  return res;
}

int main() {
  int N;
  cin >> N;
  vector<Lint> x(N);
  for (int i = 0; i < N; i++)
    cin >> x[i];
  Lint M, K;
  cin >> M >> K;
  vector<int> a(M);
  for (int i = 0; i < M; i++)
    cin >> a[i], a[i]--;

  const int sz = N - 1;
  vector<int> d(sz);
  for (int i = 0; i < sz; i++)
    d[i] = x[i + 1] - x[i];

  vector<int> p(sz);
  for (int i = 0; i < sz; i++)
    p[i] = i;
  for (int i = 0; i < M; i++) {
    swap(p[a[i] - 1], p[a[i]]);
  }
  vector<int> res = pow(p, K);
  for (int i = 0; i < sz; i++) {
    x[i + 1] = x[i] + d[res[i]];
  }
  for (int i = 0; i < N; i++)
    cout << x[i] << endl;
  return 0;
}
Problem p03953 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
const int N = 1e5 + 5;
int n, a[N], b[N], c[N], tot, sta[N], lcm;
ll k;
bool vis[N];
int rdn() {
  int ret = 0;
  bool fx = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      fx = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    ret = ret * 10 + ch - '0', ch = getchar();
  return fx ? ret : -ret;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void dfs(int cr) {
  sta[++tot] = cr;
  vis[cr] = 1;
  if (!vis[b[cr]])
    dfs(b[cr]);
}
int main() {
  n = rdn();
  for (int i = 1; i <= n; i++)
    a[i] = rdn();
  for (int i = n; i; i--)
    a[i] -= a[i - 1];
  for (int i = 1; i <= n; i++)
    b[i] = i;
  int m;
  m = rdn();
  scanf("%lld", &k);
  for (int i = 1, d; i <= m; i++) {
    d = rdn();
    swap(b[d], b[d + 1]);
  }
  lcm = 1;
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      tot = 0;
      dfs(i);
      for (int j = tot + 1; j <= tot << 1; j++)
        sta[j] = sta[j - tot];
      int d = k % tot;
      for (int j = 1; j <= tot; j++)
        c[sta[j]] = a[sta[j + d]];
    }
  ll lj = c[1];
  for (int i = 1; i <= n; i++, lj += c[i])
    printf("%lld\n", lj);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
const int N = 1e5 + 5;
int n, a[N], b[N], c[N], tot, sta[N << 1], lcm;
ll k;
bool vis[N];
int rdn() {
  int ret = 0;
  bool fx = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      fx = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    ret = ret * 10 + ch - '0', ch = getchar();
  return fx ? ret : -ret;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void dfs(int cr) {
  sta[++tot] = cr;
  vis[cr] = 1;
  if (!vis[b[cr]])
    dfs(b[cr]);
}
int main() {
  n = rdn();
  for (int i = 1; i <= n; i++)
    a[i] = rdn();
  for (int i = n; i; i--)
    a[i] -= a[i - 1];
  for (int i = 1; i <= n; i++)
    b[i] = i;
  int m;
  m = rdn();
  scanf("%lld", &k);
  for (int i = 1, d; i <= m; i++) {
    d = rdn();
    swap(b[d], b[d + 1]);
  }
  lcm = 1;
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      tot = 0;
      dfs(i);
      for (int j = tot + 1; j <= tot << 1; j++)
        sta[j] = sta[j - tot];
      int d = k % tot;
      for (int j = 1; j <= tot; j++)
        c[sta[j]] = a[sta[j + d]];
    }
  ll lj = c[1];
  for (int i = 1; i <= n; i++, lj += c[i])
    printf("%lld\n", lj);
  return 0;
}
Problem p03953 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
const int maxn = 1e5;
int n, m;
ll k, v[maxn + 8];
int a[maxn + 8], ans[maxn + 8];
bool vis[maxn + 8];

ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}

int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    v[i] = read(), a[i] = i;
  m = read(), k = read();
  for (int i = 1; i <= m; i++) {
    int x = read();
    swap(a[x - 1], a[x]);
  }
  for (int i = 1; i < n; i++) {
    if (vis[i])
      continue;
    int x = i, cnt = 0, y;
    while (!vis[x])
      cnt++, vis[x] = 1, x = a[x];
    y = x = i;
    for (int j = 1; j <= k % cnt; j++)
      y = a[y];
    while (!ans[x])
      ans[x] = v[y + 1] - v[y], x = a[x], y = a[y];
  }
  ll now = v[1];
  for (int i = 1; i <= n; i++)
    printf("%lld.0\n", now), now += ans[i];
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
const int maxn = 1e5;
int n, m;
ll k, v[maxn + 8];
int a[maxn + 8], ans[maxn + 8];
bool vis[maxn + 8];

ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}

int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    v[i] = read(), a[i] = i;
  m = read(), k = read();
  for (int i = 1; i <= m; i++) {
    int x = read();
    swap(a[x - 1], a[x]);
  }
  for (int i = 1; i < n; i++) {
    if (vis[i])
      continue;
    int x = i, cnt = 0, y;
    while (!vis[x])
      cnt++, vis[x] = 1, x = a[x];
    y = x = i;
    for (int j = 1; j <= k % cnt; j++)
      y = a[y];
    ans[x] = v[y + 1] - v[y], x = a[x], y = a[y];
    while (x != i)
      ans[x] = v[y + 1] - v[y], x = a[x], y = a[y];
  }
  ll now = v[1];
  for (int i = 1; i <= n; i++)
    printf("%lld.0\n", now), now += ans[i];
  return 0;
}
Problem p03954 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
const int N = 101010;
int n, i, l, r, m, ans;
int a[N], b[N];
int main() {
  scanf("%d", &n);
  for (i = 1; i <= 2 * n - 1; i++)
    scanf("%d", &a[i]);
  l = 1;
  r = 2 * n - 1;
  while (l <= r) {
    m = (l + r) >> 1;
    ans = -1;
    for (i = 1; i <= 2 * n - 1; i++)
      if (a[i] <= m)
        b[i] = 0;
      else
        b[i] = 1;

    for (i = 0; i < n - 1; i++) {

      if (b[n - i] == b[n - i - 1]) {
        ans = b[n - i];
        break;
      }

      if (b[n + i] == b[n + i + 1]) {
        ans = b[n + i];
        break;
      }
    }

    if (ans == -1) {
      ans = b[n] ^ ((n - 1) % 2);
    }
    //	printf("%d %d\n",m,ans);
    if (ans == 0)
      r = m - 1;
    else
      l = m + 1;
  }
  printf("%d\n", l);
}


Fixed submission:
#include <cstdio>
const int N = 501010;
int n, i, l, r, m, ans;
int a[N], b[N];
int main() {
  scanf("%d", &n);
  for (i = 1; i <= 2 * n - 1; i++)
    scanf("%d", &a[i]);
  l = 1;
  r = 2 * n - 1;
  while (l <= r) {
    m = (l + r) >> 1;
    ans = -1;
    for (i = 1; i <= 2 * n - 1; i++)
      if (a[i] <= m)
        b[i] = 0;
      else
        b[i] = 1;

    for (i = 0; i < n - 1; i++) {

      if (b[n - i] == b[n - i - 1]) {
        ans = b[n - i];
        break;
      }

      if (b[n + i] == b[n + i + 1]) {
        ans = b[n + i];
        break;
      }
    }

    if (ans == -1) {
      ans = b[n] ^ ((n - 1) % 2);
    }
    //	printf("%d %d\n",m,ans);
    if (ans == 0)
      r = m - 1;
    else
      l = m + 1;
  }
  printf("%d\n", l);
}
Problem p03954 (C++)

Buggy submission (status: Runtime Error):
/************************************************
 *Author        :  Modesty
 *Created Time  :  2019.11.07.08:36
 *Mail          :  modestyscholarly@gmail.com
 *Problem       :  pyramid
 ************************************************/
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n, a[maxn];
int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == 45) {
      f = -1;
    }
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + ch - 48;
    ch = getchar();
  }
  return s * f;
}
bool check(int k) {
  if ((a[n - 1] <= k && a[n] <= k) || (a[n] <= k && a[n + 1] <= k)) {
    return 1;
  }
  if ((a[n - 1] > k && a[n] > k) || (a[n] > k && a[n + 1] > k)) {
    return 0;
  }
  for (int i = 1; i < n - 1; i++) {
    if ((a[n + i] <= k && a[n + i + 1] <= k) ||
        (a[n - i] <= k && a[n - i - 1] <= k)) {
      return 1;
    }
    if ((a[n + i] > k && a[n + i + 1] > k) ||
        (a[n - i] > k && a[n - i - 1] > k)) {
      return 0;
    }
  }
  return a[1] <= k;
}
int main() {
  //	freopen("pyramid.in","r",stdin);
  //	freopen("pyramid.out","w",stdout);
  cin >> n;
  for (int i = 1; i < n * 2; i++) {
    a[i] = read();
  }
  int l = 1, r = n * 2 - 1;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << l << endl;
  return 0;
}

Fixed submission:
/************************************************
 *Author        :  Modesty
 *Created Time  :  2019.11.07.08:36
 *Mail          :  modestyscholarly@gmail.com
 *Problem       :  pyramid
 ************************************************/
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
int n, a[maxn];
int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == 45) {
      f = -1;
    }
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + ch - 48;
    ch = getchar();
  }
  return s * f;
}
bool check(int k) {
  if ((a[n - 1] <= k && a[n] <= k) || (a[n] <= k && a[n + 1] <= k)) {
    return 1;
  }
  if ((a[n - 1] > k && a[n] > k) || (a[n] > k && a[n + 1] > k)) {
    return 0;
  }
  for (int i = 1; i < n - 1; i++) {
    if ((a[n + i] <= k && a[n + i + 1] <= k) ||
        (a[n - i] <= k && a[n - i - 1] <= k)) {
      return 1;
    }
    if ((a[n + i] > k && a[n + i + 1] > k) ||
        (a[n - i] > k && a[n - i - 1] > k)) {
      return 0;
    }
  }
  return a[1] <= k;
}
int main() {
  //	freopen("pyramid.in","r",stdin);
  //	freopen("pyramid.out","w",stdout);
  cin >> n;
  for (int i = 1; i < n * 2; i++) {
    a[i] = read();
  }
  int l = 1, r = n * 2 - 1;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << l << endl;
  return 0;
}
Problem p03954 (C++)

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

int N, p[200010];

inline bool check(int x) {
  int lt = 0;
  for (int i = 1; i <= 2 * N - 1; i++) {
    int r = i;
    while (r < 2 * N - 1 && (p[r + 1] >= x) == (p[i] >= x))
      r++;
    if (r == i)
      continue;
    if (i <= N && r >= N)
      return p[i] >= x;
    if (r < N) {
      lt = r;
      continue;
    }
    if (i - N < N - lt)
      return p[i] >= x;
    else
      return p[lt] >= x;
  }
  if (lt)
    return p[lt] >= x;
  if (N & 1)
    return p[N] >= x;
  else
    return p[N] < x;
}

inline void solveB() {
  scanf("%d", &N);
  for (int i = 1; i <= 2 * N - 1; i++)
    scanf("%d", &p[i]);
  int l = 1, r = 2 * N - 1;
  while (l < r) {
    int mid = (l + r + 1) >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  printf("%d\n", l);
}

int main() {
  solveB();
  return 0;
}

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

int N, p[200010];

inline bool check(int x) {
  int lt = 0;
  for (int i = 1; i <= 2 * N - 1; i++) {
    int r = i;
    while (r < 2 * N - 1 && (p[r + 1] >= x) == (p[i] >= x))
      r++;
    if (r == i)
      continue;
    if (i <= N && r >= N)
      return p[i] >= x;
    if (r < N) {
      lt = i = r;
      continue;
    }
    if (i - N < N - lt)
      return p[i] >= x;
    else
      return p[lt] >= x;
  }
  if (lt)
    return p[lt] >= x;
  if (N & 1)
    return p[N] >= x;
  else
    return p[N] < x;
}

inline void solveB() {
  scanf("%d", &N);
  for (int i = 1; i <= 2 * N - 1; i++)
    scanf("%d", &p[i]);
  int l = 1, r = 2 * N - 1;
  while (l < r) {
    int mid = (l + r + 1) >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  printf("%d\n", l);
}

int main() {
  solveB();
  return 0;
}
Problem p03954 (C++)

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

using namespace std;

const int N = 1e5 + 5;

int n, a[N];

bool can(int u) {
  for (int i = 0; i < n - 1; i++) {
    if (a[n + i] <= u && a[n + i + 1] <= u) {
      return true;
    }
    if (a[n - i] <= u && a[n - i - 1] <= u) {
      return true;
    }
    if (a[n + i] > u && a[n + i + 1] > u) {
      return false;
    }
    if (a[n - i] > u && a[n - i - 1] > u) {
      return false;
    }
  }
  return a[1] <= u;
}

int bs(int l, int r) {
  if (l == r) {
    return l;
  }
  int mid = (l + r) >> 1;
  if (can(mid)) {
    return bs(l, mid);
  } else {
    return bs(mid + 1, r);
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < (n << 1); i++) {
    scanf("%d", &a[i]);
  }
  printf("%d\n", bs(1, (n << 1) - 1));
  return 0;
}

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

using namespace std;

const int N = 1e5 + 5;

int n, a[N << 1];

bool can(int u) {
  for (int i = 0; i < n - 1; i++) {
    if (a[n + i] <= u && a[n + i + 1] <= u) {
      return true;
    }
    if (a[n - i] <= u && a[n - i - 1] <= u) {
      return true;
    }
    if (a[n + i] > u && a[n + i + 1] > u) {
      return false;
    }
    if (a[n - i] > u && a[n - i - 1] > u) {
      return false;
    }
  }
  return a[1] <= u;
}

int bs(int l, int r) {
  if (l == r) {
    return l;
  }
  int mid = (l + r) >> 1;
  if (can(mid)) {
    return bs(l, mid);
  } else {
    return bs(mid + 1, r);
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < (n << 1); i++) {
    scanf("%d", &a[i]);
  }
  printf("%d\n", bs(1, (n << 1) - 1));
  return 0;
}
Problem p03954 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const long long MAXN = 100005;
long long n, N;
long long a[MAXN];
bool check(long long mid) {
  for (long long i = 0; i < n - 1; i++) {
    long long s0 = 0, s1 = 0;
    if (a[n - i] >= mid && a[n - i - 1] >= mid)
      s1++;
    if (a[n - i] < mid && a[n - i - 1] < mid)
      s0++;
    if (a[n + i] >= mid && a[n + i + 1] >= mid)
      s1++;
    if (a[n + i] < mid && a[n + i + 1] < mid)
      s0++;
    if (s1)
      return 1;
    if (s0)
      return 0;
  }
  return a[1] >= mid;
}
int main() {
  // freopen("mid.in","r",stdin);
  // freopen("mid.out","w",stdout);
  scanf("%lld", &n);
  N = 2 * n - 1;
  for (long long i = 1; i <= N; i++) {
    scanf("%lld", &a[i]);
  }
  long long l = 2, r = N, ans = 1;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    if (check(mid)) {
      ans = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  printf("%lld\n", ans);
  fclose(stdin);
  fclose(stdout);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const long long MAXN = 200005;
long long n, N;
long long a[MAXN];
bool check(long long mid) {
  for (long long i = 0; i < n - 1; i++) {
    long long s0 = 0, s1 = 0;
    if (a[n - i] >= mid && a[n - i - 1] >= mid)
      s1++;
    if (a[n - i] < mid && a[n - i - 1] < mid)
      s0++;
    if (a[n + i] >= mid && a[n + i + 1] >= mid)
      s1++;
    if (a[n + i] < mid && a[n + i + 1] < mid)
      s0++;
    if (s1)
      return 1;
    if (s0)
      return 0;
  }
  return a[1] >= mid;
}
int main() {
  // freopen("mid.in","r",stdin);
  // freopen("mid.out","w",stdout);
  scanf("%lld", &n);
  N = 2 * n - 1;
  for (long long i = 1; i <= N; i++) {
    scanf("%lld", &a[i]);
  }
  long long l = 2, r = N, ans = 1;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    if (check(mid)) {
      ans = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  printf("%lld\n", ans);
  fclose(stdin);
  fclose(stdout);
}
Problem p03954 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n;
int val[maxn], sor[maxn], tag[maxn];
bool check(int v) {
  for (int i = 1; i <= 2 * n - 1; i++)
    tag[i] = val[i] >= v;
  int dis1 = 1 << 30, col1 = 0;
  int dis2 = 1 << 30, col2 = 0;
  for (int i = n; i >= 2; i--)
    if (tag[i] == tag[i - 1]) {
      dis1 = n - i, col1 = tag[i];
      break;
    }
  for (int i = n; i <= 2 * n - 2; i++)
    if (tag[i] == tag[i + 1]) {
      dis2 = i - n, col2 = tag[i];
      break;
    }
  if (dis1 == dis2 && dis1 == (1 << 30))
    return ((n - 1) & 1) ^ tag[n];
  return dis1 < dis2 ? col1 : col2;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= 2 * n - 1; i++)
    scanf("%d", &val[i]), sor[i] = val[i];
  sort(sor + 1, sor + 2 * n);
  int l = 1, r = 2 * n - 1, ans = 1;
  while (l <= r) {
    int mid = l + r >> 1;
    if (check(sor[mid]))
      l = mid + 1, ans = mid;
    else
      r = mid - 1;
  }
  printf("%d\n", sor[ans]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n;
int val[maxn], sor[maxn], tag[maxn];
bool check(int v) {
  for (int i = 1; i <= 2 * n - 1; i++)
    tag[i] = val[i] >= v;
  int dis1 = 1 << 30, col1 = 0;
  int dis2 = 1 << 30, col2 = 0;
  for (int i = n; i >= 2; i--)
    if (tag[i] == tag[i - 1]) {
      dis1 = n - i, col1 = tag[i];
      break;
    }
  for (int i = n; i <= 2 * n - 2; i++)
    if (tag[i] == tag[i + 1]) {
      dis2 = i - n, col2 = tag[i];
      break;
    }
  if (dis1 == dis2 && dis1 == (1 << 30))
    return ((n - 1) & 1) ^ tag[n];
  return dis1 < dis2 ? col1 : col2;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= 2 * n - 1; i++)
    scanf("%d", &val[i]), sor[i] = val[i];
  sort(sor + 1, sor + 2 * n);
  int l = 1, r = 2 * n - 1, ans = 1;
  while (l <= r) {
    int mid = l + r >> 1;
    if (check(sor[mid]))
      l = mid + 1, ans = mid;
    else
      r = mid - 1;
  }
  printf("%d\n", sor[ans]);
}
Problem p03954 (C++)

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

const int N = 1e5 + 10;
int n;
int a[N];
int b[N];

int check(int mid) {
  for (int i = 1; i < 2 * n; i++)
    if (a[i] >= mid)
      b[i] = 1;
    else
      b[i] = 0;
  for (int i = 1; i < n; i++) {
    if (b[n - i] == b[n - i + 1])
      return b[n - i];
    if (b[n + i] == b[n + i - 1])
      return b[n + i];
  }
  return b[1];
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < 2 * n; i++)
    scanf("%d", a + i);
  int l = 1, r = 2 * n - 1;
  while (l < r) {
    int mid = (l + r + 1) / 2;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  printf("%d\n", l);
  return 0;
}

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

const int N = 1e5 + 10;
int n;
int a[2 * N];
int b[2 * N];

int check(int mid) {
  for (int i = 1; i < 2 * n; i++)
    if (a[i] >= mid)
      b[i] = 1;
    else
      b[i] = 0;
  for (int i = 1; i < n; i++) {
    if (b[n - i] == b[n - i + 1])
      return b[n - i];
    if (b[n + i] == b[n + i - 1])
      return b[n + i];
  }
  return b[1];
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < 2 * n; i++)
    scanf("%d", a + i);
  int l = 1, r = 2 * n - 1;
  while (l < r) {
    int mid = (l + r + 1) / 2;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  printf("%d\n", l);
  return 0;
}
Problem p03954 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n;
int a[100100];
bool b[100100];
bool check(int mid) {
  if (a[n + 1] <= mid && a[n] <= mid || a[n] <= mid && a[n - 1] <= mid)
    return 1;
  if (a[n + 1] > mid && a[n] > mid || a[n] > mid && a[n - 1] > mid)
    return 0;
  for (int i = 1; i < n - 1; i++) {
    if (a[n + i + 1] <= mid && a[n + i] <= mid ||
        a[n - i] <= mid && a[n - i - 1] <= mid)
      return 1;
    if (a[n + i + 1] > mid && a[n + i] > mid ||
        a[n - i] > mid && a[n - i - 1] > mid)
      return 0;
  }
  return a[1] <= mid;
}
int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 1; i <= 2 * n - 1; i++)
    cin >> a[i];
  int le = 0;
  int ri = 2 * n - 1;
  while (le < ri) {
    int mi = (le + ri) / 2;
    if (check(mi))
      ri = mi;
    else
      le = mi + 1;
  }
  cout << le << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n;
int a[200100];
bool check(int mid) {
  if (a[n + 1] <= mid && a[n] <= mid || a[n] <= mid && a[n - 1] <= mid)
    return 1;
  if (a[n + 1] > mid && a[n] > mid || a[n] > mid && a[n - 1] > mid)
    return 0;
  for (int i = 1; i < n - 1; i++) {
    if (a[n + i + 1] <= mid && a[n + i] <= mid ||
        a[n - i] <= mid && a[n - i - 1] <= mid)
      return 1;
    if (a[n + i + 1] > mid && a[n + i] > mid ||
        a[n - i] > mid && a[n - i - 1] > mid)
      return 0;
  }
  return a[1] <= mid;
}
int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 1; i <= 2 * n - 1; i++)
    cin >> a[i];
  int le = 0;
  int ri = 2 * n - 1;
  while (le < ri) {
    int mi = (le + ri) / 2;
    if (check(mi))
      ri = mi;
    else
      le = mi + 1;
  }
  cout << le << endl;
  return 0;
}
Problem p03954 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = (x); i <= (y); i++)

using namespace std;

const int N = 1e5 + 10;
int n, a[N], b[N];

bool check(int x) {
  rep(i, 1, 2 * n - 1) b[i] = a[i] > x;
  int m = n, l = m, r = m;
  while (l > 1 && b[l - 1] != b[l])
    l--;
  while (r < 2 * n - 1 && b[r + 1] != b[r])
    r++;
  if (l == m || r == m)
    return b[m];
  if (b[l] == b[r])
    return b[l];
  return l > 2 * n - r ? b[l] : b[r];
}

int main() {
  scanf("%d", &n);
  rep(i, 1, 2 * n - 1) scanf("%d", &a[i]);
  int l = 1, r = 2 * n - 1;
  while (l <= r) {
    int mid = l + r >> 1;
    if (!check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", r + 1);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = (x); i <= (y); i++)

using namespace std;

const int N = 2e5 + 10;
int n, a[N], b[N];

bool check(int x) {
  rep(i, 1, 2 * n - 1) b[i] = a[i] > x;
  int m = n, l = m, r = m;
  while (l > 1 && b[l - 1] != b[l])
    l--;
  while (r < 2 * n - 1 && b[r + 1] != b[r])
    r++;
  if (l == m || r == m)
    return b[m];
  if (b[l] == b[r])
    return b[l];
  return l > 2 * n - r ? b[l] : b[r];
}

int main() {
  scanf("%d", &n);
  rep(i, 1, 2 * n - 1) scanf("%d", &a[i]);
  int l = 1, r = 2 * n - 1;
  while (l <= r) {
    int mid = l + r >> 1;
    if (!check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", r + 1);
  return 0;
}
Problem p03954 (C++)

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

using namespace std;

const int N = 2e5 + 10;

int n, A[N], now[N];

void Init() {
  scanf("%d", &n);
  n = 2 * n - 1;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &A[i]);
}

int len[N], op[N];
bool Check(int x) {
  for (int i = 1; i <= n; ++i)
    if (A[i] >= x)
      now[i] = 1;
    else
      now[i] = 0;
  int top = 0, cnt = 0;
  for (int i = 1; i <= n;) {
    int pos = i;
    while (now[pos] == now[i] && i <= n)
      ++i;
    if (i - pos > 1) {
      if (top && now[pos] == op[top])
        len[top] += i - pos + cnt;
      else {
        ++top;
        len[top] = i - pos;
        if (top - 1)
          len[top - 1] += cnt >> 1, len[top] += cnt >> 1;
        else
          len[top] += cnt;
        op[top] = now[pos];
      }
      cnt = 0;
    } else
      cnt++;
  }
  len[top] += cnt;
  cnt = 0;
  int sum = 0;
  for (int i = 1; i <= top; ++i)
    sum += len[i];
  int l = 1, r = top;
  while (l != r) {
    int del = min(len[l], len[r]);
    len[l] -= del;
    len[r] -= del;
    if (!len[l])
      ++l;
    if (!len[r])
      --r;
  }
  return op[l];
}

void Solve() {
  int l = 1, r = n;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (Check(mid))
      l = mid + 1;
    else
      r = mid - 1;
  }
  printf("%d\n", r);
}

int main() {
  Init();
  Solve();
  return 0;
}

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

using namespace std;

const int N = 2e5 + 10;

int n, A[N], now[N];

void Init() {
  scanf("%d", &n);
  n = 2 * n - 1;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &A[i]);
}

int len[N], op[N];
bool Check(int x) {
  for (int i = 1; i <= n; ++i)
    if (A[i] >= x)
      now[i] = 1;
    else
      now[i] = 0;
  int top = 0, cnt = 0;
  for (int i = 1; i <= n;) {
    int pos = i;
    while (now[pos] == now[i] && i <= n)
      ++i;
    if (i - pos > 1) {
      if (top && now[pos] == op[top])
        len[top] += i - pos + cnt;
      else {
        ++top;
        len[top] = i - pos;
        if (top - 1)
          len[top - 1] += cnt >> 1, len[top] += cnt >> 1;
        else
          len[top] += cnt;
        op[top] = now[pos];
      }
      cnt = 0;
    } else
      cnt++;
  }
  if (!top)
    return now[1];
  len[top] += cnt;
  cnt = 0;
  int sum = 0;
  for (int i = 1; i <= top; ++i)
    sum += len[i];
  int l = 1, r = top;
  while (l != r) {
    int del = min(len[l], len[r]);
    len[l] -= del;
    len[r] -= del;
    if (!len[l])
      ++l;
    if (!len[r])
      --r;
  }
  return op[l];
}

void Solve() {
  int l = 1, r = n;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (Check(mid))
      l = mid + 1;
    else
      r = mid - 1;
  }
  printf("%d\n", r);
}

int main() {
  Init();
  Solve();
  return 0;
}
Problem p03954 (C++)

Buggy submission (status: Runtime Error):
// 2017-11-6
// miaomiao
//
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

#define For(i, a, b) for (int i = (a); i <= (int)(b); ++i)
#define N (100000 + 5)

int n, nn, a[N], num[N];

bool Check(int nv) {
  //	printf("nv = %d\n", nv);
  For(i, 1, nn) num[i] = a[i] >= nv;
  // For(i, 1, nn) printf("%d ", num[i]); puts("");

  int me = num[n], l = n, r = n;
  while (l > 1 && r < nn) {
    --l, ++r;
    //	printf("me = %d (%d %d) %d %d\n", me, l, r, num[l], num[r]);
    if (num[l] == me || num[r] == me)
      return me;

    me ^= 1;
  }
  return me;
}

void Solve() {
  int L = 1, R = nn, mid;
  while (L < R) {
    mid = (L + R + 1) >> 1;

    //	printf("Check(%d) = %d\n", mid, Check(mid));
    if (Check(mid))
      L = mid;
    else
      R = mid - 1;

    //	puts("========================");
  }

  printf("%d\n", L);
}

int main() {
  scanf("%d", &n);
  nn = n * 2 - 1;
  For(i, 1, nn) scanf("%d", &a[i]);

  Solve();

  return 0;
}

Fixed submission:
// 2017-11-6
// miaomiao
//
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

#define For(i, a, b) for (int i = (a); i <= (int)(b); ++i)
#define N (200000 + 5)

int n, nn, a[N], num[N];

bool Check(int nv) {
  //	printf("nv = %d\n", nv);
  For(i, 1, nn) num[i] = a[i] >= nv;
  // For(i, 1, nn) printf("%d ", num[i]); puts("");

  int me = num[n], l = n, r = n;
  while (l > 1 && r < nn) {
    --l, ++r;
    //	printf("me = %d (%d %d) %d %d\n", me, l, r, num[l], num[r]);
    if (num[l] == me || num[r] == me)
      return me;

    me ^= 1;
  }
  return me;
}

void Solve() {
  int L = 1, R = nn, mid;
  while (L < R) {
    mid = (L + R + 1) >> 1;

    //	printf("Check(%d) = %d\n", mid, Check(mid));
    if (Check(mid))
      L = mid;
    else
      R = mid - 1;

    //	puts("========================");
  }

  printf("%d\n", L);
}

int main() {
  scanf("%d", &n);
  nn = n * 2 - 1;
  For(i, 1, nn) scanf("%d", &a[i]);

  Solve();

  return 0;
}
Problem p03954 (C++)

Buggy submission (status: Runtime Error):
// NOT A FSKY CODE
#include <algorithm>
#include <assert.h>
#include <bits/stdc++.h>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdlib.h>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
int n, m;
int a[100004], b[100004];
bool check(int x) {
  for (int i = 1; i <= m; i++) {
    if (a[i] < x)
      b[i] = 0;
    else
      b[i] = 1;
  }
  int l, r;
  for (l = n - 1; l > 0 && b[l] ^ b[l + 1]; l--)
    ;
  for (r = n + 1; r <= m && b[r] ^ b[r - 1]; r++)
    ;
  if (l == 0 && r == m + 1)
    return b[1];
  return r - n > n - l ? b[l] : b[r];
}
int main() {
  cin >> n;
  m = n * 2 - 1;
  for (int i = 1; i <= m; i++)
    scanf("%d", &a[i]);
  int l = -1, r = m + 1;
  while (r > l + 1) {
    int mid = (l + r) / 2;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  cout << l;
}

Fixed submission:
// NOT A FSKY CODE
#include <algorithm>
#include <assert.h>
#include <bits/stdc++.h>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdlib.h>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
int n, m;
int a[200004], b[200004];
bool check(int x) {
  for (int i = 1; i <= m; i++) {
    if (a[i] < x)
      b[i] = 0;
    else
      b[i] = 1;
  }
  int l, r;
  for (l = n - 1; l > 0 && b[l] ^ b[l + 1]; l--)
    ;
  for (r = n + 1; r <= m && b[r] ^ b[r - 1]; r++)
    ;
  if (l == 0 && r == m + 1)
    return b[1];
  return r - n > n - l ? b[l] : b[r];
}
int main() {
  cin >> n;
  m = n * 2 - 1;
  for (int i = 1; i <= m; i++)
    scanf("%d", &a[i]);
  int l = -1, r = m + 1;
  while (r > l + 1) {
    int mid = (l + r) / 2;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  cout << l;
}
Problem p03954 (C++)

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

#define dd ch = getchar()
inline int read() {
  int x = 0;
  bool f = false;
  char dd;
  while (!isdigit(ch))
    f |= ch == '-', dd;
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + ch - 48, dd;
  return f ? -x : x;
}
#undef dd
void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + 48);
}
#define writeln(x) (write(x), putchar('\n'))
#define writesp(x) (write(x), putchar(' '))

const int N = 1e5 + 5, INF = 0x3f3f3f3f;
bool a[N];
int val[N], n, midid;
inline int dis(int x) { return abs(midid - x); }
inline bool check(int k) {
  bool f0 = true, f1 = true;
  for (int i = 1; i <= n; i++) {
    a[i] = (val[i] >= k);
    if (i & 1)
      f0 &= a[i] ^ 1, f1 &= a[i];
    else
      f0 &= a[i], f1 &= a[i] ^ 1;
  }
  if (f0)
    return false;
  if (f1)
    return true;
  int minn[2] = {INF, INF};
  for (int i = 2; i <= n; i++)
    if (a[i] == a[i - 1])
      minn[a[i]] = min(minn[a[i]], min(dis(i), dis(i - 1)));
  return minn[1] < minn[0];
}
int main() {
  n = (midid = read()) * 2 - 1;
  for (int i = 1; i <= n; i++)
    val[i] = read();
  int l = 0, r = INF;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  write(l);
  return 0;
}

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

#define dd ch = getchar()
inline int read() {
  int x = 0;
  bool f = false;
  char dd;
  while (!isdigit(ch))
    f |= ch == '-', dd;
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + ch - 48, dd;
  return f ? -x : x;
}
#undef dd
void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + 48);
}
#define writeln(x) (write(x), putchar('\n'))
#define writesp(x) (write(x), putchar(' '))

const int N = 2e5 + 5, INF = 0x3f3f3f3f;
bool a[N];
int val[N], n, midid;
inline int dis(int x) { return abs(midid - x); }
inline bool check(int k) {
  bool f0 = true, f1 = true;
  for (int i = 1; i <= n; i++) {
    a[i] = (val[i] >= k);
    if (i & 1)
      f0 &= a[i] ^ 1, f1 &= a[i];
    else
      f0 &= a[i], f1 &= a[i] ^ 1;
  }
  if (f0)
    return false;
  if (f1)
    return true;
  int minn[2] = {INF, INF};
  for (int i = 2; i <= n; i++)
    if (a[i] == a[i - 1])
      minn[a[i]] = min(minn[a[i]], min(dis(i), dis(i - 1)));
  return minn[1] < minn[0];
}
int main() {
  n = (midid = read()) * 2 - 1;
  for (int i = 1; i <= n; i++)
    val[i] = read();
  int l = 0, r = INF;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  write(l);
  return 0;
}
Problem p03954 (C++)

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

const int MaxN = 100005;

int n, arr[MaxN];

bool check(int x) {
  auto get = [x](int pos) { return arr[pos] > x; };
  int l = 0;
  while (l + 1 < n && get(n - l - 1) != get(n - l))
    ++l;
  int r = 0;
  while (r + 1 < n && get(n + r + 1) != get(n + r))
    ++r;
  return l < r ? get(n - l) : get(n + r);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < 2 * n; ++i)
    scanf("%d", arr + i);
  int l = 1, r = 2 * n - 1;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      l = mid + 1;
    } else {
      r = mid;
    }
  }
  printf("%d\n", l);
  return 0;
}

Fixed submission:
#include <cstdio>

const int MaxN = 200005;

int n, arr[MaxN];

bool check(int x) {
  auto get = [x](int pos) { return arr[pos] > x; };
  int l = 0;
  while (l + 1 < n && get(n - l - 1) != get(n - l))
    ++l;
  int r = 0;
  while (r + 1 < n && get(n + r + 1) != get(n + r))
    ++r;
  return l < r ? get(n - l) : get(n + r);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < 2 * n; ++i)
    scanf("%d", arr + i);
  int l = 1, r = 2 * n - 1;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      l = mid + 1;
    } else {
      r = mid;
    }
  }
  printf("%d\n", l);
  return 0;
}
Problem p03954 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int a[2000100], p[2000100], n;
int calc(int lim) {
  for (int i = 1; i <= 2 * n - 1; i++)
    p[i] = (a[i] > lim);
  if ((p[n] == p[n - 1]) || (p[n] == p[n + 1]))
    return p[n];
  int l = n - 1, r = n + 1;
  while ((l) && (p[l] != p[l + 1]))
    l--;
  while ((r <= 2 * n - 1) && (p[r] != p[r - 1]))
    r++;
  if ((!l) && (r != 2 * n))
    return p[r];
  if ((r == 2 * n) && (l))
    return p[l];
  if ((!l) && (r == 2 * n))
    return p[n] ^ (n & 1) ^ 1;
  if ((n - l) < (r - n))
    return p[l];
  if ((n - l) > (r - n))
    return p[r];
  assert(0);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= 2 * n - 1; i++)
    scanf("%d", &a[i]);
  int l = 0, r = 2 * n - 1;
  while (l + 1 != r) {
    int mid = (l + r) >> 1;
    if (calc(mid) == 0)
      r = mid;
    else
      l = mid;
  }
  printf("%d\n", r);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[2000100], p[2000100], n;
int calc(int lim) {
  for (int i = 1; i <= 2 * n - 1; i++)
    p[i] = (a[i] > lim);
  if ((p[n] == p[n - 1]) || (p[n] == p[n + 1]))
    return p[n];
  int l = n - 1, r = n + 1;
  while ((l) && (p[l] != p[l + 1]))
    l--;
  while ((r <= 2 * n - 1) && (p[r] != p[r - 1]))
    r++;
  if ((!l) && (r != 2 * n))
    return p[r];
  if ((r == 2 * n) && (l))
    return p[l];
  if ((!l) && (r == 2 * n))
    return p[n] ^ (n & 1) ^ 1;
  if ((n - l) < (r - n))
    return p[l];
  if ((n - l) > (r - n))
    return p[r];
  if (p[l] == p[r])
    return p[l];
  assert(0);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= 2 * n - 1; i++)
    scanf("%d", &a[i]);
  int l = 0, r = 2 * n - 1;
  while (l + 1 != r) {
    int mid = (l + r) >> 1;
    if (calc(mid) == 0)
      r = mid;
    else
      l = mid;
  }
  printf("%d\n", r);
}
Problem p03954 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <class t> inline t read(t &x) {
  char c = getchar();
  bool f = 0;
  x = 0;
  while (!isdigit(c))
    f |= c == '-', c = getchar();
  while (isdigit(c))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  if (f)
    x = -x;
  return x;
}
template <class t> inline void write(t x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x > 9)
      write(x / 10);
    putchar('0' + x % 10);
  }
}

const int N = 1e5 + 5;
int n, m, ans, a[N];
bool b[N];

bool check(int x) {
  for (int i = 1; i <= n; i++)
    b[i] = a[i] <= x;
  int flag = -1, mi = n;
  for (int i = 2; i <= n; i++)
    if (b[i] == b[i - 1])
      if (abs(m - i) < mi) {
        mi = abs(m - i);
        flag = b[i];
      }
  for (int i = n - 1; i >= 1; i--)
    if (b[i] == b[i + 1])
      if (abs(m - i) < mi) {
        mi = abs(m - i);
        flag = b[i];
      }
  if (flag == -1)
    flag = b[1];
  return flag;
}

signed main() {
  m = read(n);
  n = n * 2 - 1;
  for (int i = 1; i <= n; i++)
    read(a[i]);
  int l = 1, r = n;
  while (l <= r) {
    int mid = l + r >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  write(ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class t> inline t read(t &x) {
  char c = getchar();
  bool f = 0;
  x = 0;
  while (!isdigit(c))
    f |= c == '-', c = getchar();
  while (isdigit(c))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  if (f)
    x = -x;
  return x;
}
template <class t> inline void write(t x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x > 9)
      write(x / 10);
    putchar('0' + x % 10);
  }
}

const int N = 2e5 + 5;
int n, m, ans, a[N];
bool b[N];

bool check(int x) {
  for (int i = 1; i <= n; i++)
    b[i] = a[i] <= x;
  int flag = -1, mi = n;
  for (int i = 2; i <= n; i++)
    if (b[i] == b[i - 1])
      if (abs(m - i) < mi) {
        mi = abs(m - i);
        flag = b[i];
      }
  for (int i = n - 1; i >= 1; i--)
    if (b[i] == b[i + 1])
      if (abs(m - i) < mi) {
        mi = abs(m - i);
        flag = b[i];
      }
  if (flag == -1)
    flag = b[1];
  return flag;
}

signed main() {
  m = read(n);
  n = n * 2 - 1;
  for (int i = 1; i <= n; i++)
    read(a[i]);
  int l = 1, r = n;
  while (l <= r) {
    int mid = l + r >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  write(ans);
}
Problem p03954 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 20;
int a[N], n;
bool check(int k) {
  for (int i = 0; i < n - 1; i++) {
    if ((a[n - i] <= k && a[n - i - 1] <= k) ||
        (a[n + i] <= k && a[n + i + 1] <= k))
      return 1;
    if ((a[n - i] > k && a[n - i - 1] > k) ||
        (a[n + i] > k && a[n + i + 1] > k))
      return 0;
  }
  return a[1] <= k;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < (n << 1); i++)
    scanf("%d", &a[i]);
  int l = 1, r = (n << 1) - 1;
  while (l < r) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 20;
int a[N], n;
bool check(int k) {
  for (int i = 0; i < n - 1; i++) {
    if ((a[n - i] <= k && a[n - i - 1] <= k) ||
        (a[n + i] <= k && a[n + i + 1] <= k))
      return 1;
    if ((a[n - i] > k && a[n - i - 1] > k) ||
        (a[n + i] > k && a[n + i + 1] > k))
      return 0;
  }
  return a[1] <= k;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < (n << 1); i++)
    scanf("%d", &a[i]);
  int l = 1, r = (n << 1) - 1;
  while (l < r) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
  return 0;
}
Problem p03954 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 4;
const int INF = 1e8 + 54;
int n;
int a[2 * N];
int b[2 * N];
bool check(int val) {
  int pos1 = INF, pos2 = -INF;
  for (int i = 1; i <= 2 * n - 1; i++) {
    b[i] = (a[i] >= val);
    //		cout<<b[i]<<" ";
  }
  //	cout<<"\n";
  for (int i = n; i <= 2 * n - 1; i++) {
    if (b[i] == b[i + 1]) {
      pos1 = i;
      break;
    }
  }

  for (int i = n; i >= 1; i--) {
    if (b[i] == b[i - 1]) {
      pos2 = i;
      break;
    }
  }

  if (pos1 == -1 && pos2 == -1) {
    if (n % 2 != 0)
      return b[n];

    else
      return (b[n] ^ 1);
  }

  if (pos1 - n + 1 < n - pos2 + 1)
    return b[pos1];

  else
    return b[pos2];
}

int main() {
  cin >> n;
  for (int i = 1; i <= 2 * n - 1; i++) {
    scanf("%d", &a[i]);
  }

  int l = 1, r = 2 * n - 1;
  while (l < r) {
    int mid = (l + r + 1) / 2;
    //		cout<<mid<<"\n";
    if (check(mid) == 1)
      l = mid;
    else
      r = mid - 1;
  }

  cout << l;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 4;
const int INF = 1e8 + 54;
int n;
int a[2 * N];
int b[2 * N];
bool check(int val) {
  int pos1 = INF, pos2 = -INF;
  for (int i = 1; i <= 2 * n - 1; i++) {
    b[i] = (a[i] >= val);
    //		cout<<b[i]<<" ";
  }
  //	cout<<"\n";
  for (int i = n; i <= 2 * n - 1; i++) {
    if (b[i] == b[i + 1]) {
      pos1 = i;
      break;
    }
  }

  for (int i = n; i >= 1; i--) {
    if (b[i] == b[i - 1]) {
      pos2 = i;
      break;
    }
  }

  if (pos1 == INF && pos2 == -INF) {
    if (n % 2 != 0)
      return b[n];

    else
      return (b[n] ^ 1);
  }

  if (pos1 - n + 1 < n - pos2 + 1)
    return b[pos1];

  else
    return b[pos2];
}

int main() {
  cin >> n;
  for (int i = 1; i <= 2 * n - 1; i++) {
    scanf("%d", &a[i]);
  }

  int l = 1, r = 2 * n - 1;
  while (l < r) {
    int mid = (l + r + 1) / 2;
    //		cout<<mid<<"\n";
    if (check(mid) == 1)
      l = mid;
    else
      r = mid - 1;
  }

  cout << l;
}
Problem p03954 (C++)

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

int n, a[100010], b[100010];

bool check(int k) {
  for (int i = 1; i < 2 * n; i++)
    b[i] = (a[i] >= k);
  for (int i = 1; i < n; i++) {
    if ((b[n] ^ b[n + i]) != (i & 1))
      return b[n + i];
    if ((b[n] ^ b[n - i]) != (i & 1))
      return b[n - i];
  }
  return b[n] ^ (!(n & 1));
}

void work() {
  scanf("%d", &n);
  for (int i = 1; i < 2 * n; i++)
    scanf("%d", &a[i]);
  int l = 1, r = 2 * n, mid;
  for (; r - l > 1; (check(mid = (l + r) >> 1) ? l : r) = mid)
    ;
  printf("%d", l);
}

int main() {
  work();
  return 0;
}


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

int n, a[200010], b[200010];

bool check(int k) {
  for (int i = 1; i < 2 * n; i++)
    b[i] = (a[i] >= k);
  for (int i = 1; i < n; i++) {
    if ((b[n] ^ b[n + i]) != (i & 1))
      return b[n + i];
    if ((b[n] ^ b[n - i]) != (i & 1))
      return b[n - i];
  }
  return b[n] ^ (!(n & 1));
}

void work() {
  scanf("%d", &n);
  for (int i = 1; i < 2 * n; i++)
    scanf("%d", &a[i]);
  int l = 1, r = 2 * n, mid;
  for (; r - l > 1; (check(mid = (l + r) >> 1) ? l : r) = mid)
    ;
  printf("%d", l);
}

int main() {
  work();
  return 0;
}