Problem p03552 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
  return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
  o << "sz = " << vc.size() << endl << "[";
  for (const T &v : vc)
    o << v << ",";
  o << "]";
  return o;
}
typedef long long ll;
int N;
ll X, Y;
ll a[2000];
ll last;
ll T;
bool memo[2010][2];
bool vis[2010][2];
bool Xwin(int y, bool xturn) {
  if (y >= 0 && vis[y][xturn])
    return memo[y][xturn];

  if (xturn) {
    for (int nx = y + 1; nx < N - 1; nx++) {
      if (abs(a[nx] - last) < T)
        continue;
      if (Xwin(nx, 0))
        return memo[y][xturn] = 1;
    }
    return memo[y][xturn] = 0;
  } else {
    for (int nx = y + 1; nx < N - 1; nx++) {
      if (abs(a[nx] - last) >= T)
        continue;
      if (!Xwin(nx, 1))
        return memo[y][xturn] = 0;
    }
    return memo[y][xturn] = 1;
  }
}
bool can(ll t) {
  rep(i, 2010) rep(j, 2) vis[i][j] = 0;
  T = t;
  if (abs(Y - last) >= t)
    return 1;
  return Xwin(0, -1);
}
int main() {
  cin >> N >> X >> Y;
  rep(i, N) cin >> a[i];
  last = a[N - 1];
  ll ub = 1e9, lb = 0;
  while (ub - lb > 1) {
    ll m = (ub + lb) / 2;
    if (can(m))
      lb = m;
    else
      ub = m;
  }
  cout << lb << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
  return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
  o << "sz = " << vc.size() << endl << "[";
  for (const T &v : vc)
    o << v << ",";
  o << "]";
  return o;
}
typedef long long ll;
int N;
ll X, Y;
ll a[2000];
ll last;
ll T;
bool memo[2010][2];
bool vis[2010][2];
bool Xwin(int y, bool xturn) {
  if (y >= 0 && vis[y][xturn])
    return memo[y][xturn];
  vis[y][xturn] = 1;

  if (xturn) {
    for (int nx = y + 1; nx < N - 1; nx++) {
      if (abs(a[nx] - last) < T)
        continue;
      if (Xwin(nx, 0))
        return memo[y][xturn] = 1;
    }
    return memo[y][xturn] = 0;
  } else {
    for (int nx = y + 1; nx < N - 1; nx++) {
      if (abs(a[nx] - last) >= T)
        continue;
      if (!Xwin(nx, 1))
        return memo[y][xturn] = 0;
    }
    return memo[y][xturn] = 1;
  }
}
bool can(ll t) {
  rep(i, 2010) rep(j, 2) vis[i][j] = 0;
  T = t;
  if (abs(Y - last) >= t)
    return 1;
  return Xwin(0, -1);
}
int main() {
  cin >> N >> X >> Y;
  rep(i, N) cin >> a[i];
  last = a[N - 1];
  ll ub = 1e9, lb = 0;
  while (ub - lb > 1) {
    ll m = (ub + lb) / 2;
    if (can(m))
      lb = m;
    else
      ub = m;
  }
  cout << lb << endl;
}