alexjercan/bugnet huggingface dataset:
Problem p01096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int n;
vector<int> w;

enum MEMO { NOT_CHECKED, TRUE, FALSE };

MEMO memo[301][301];

MEMO rec(int l, int r) {
  if (r == l) {
    return TRUE;
  }
  if (r - l - 1 == 1) {
    if (abs(w[r - 1] - w[l]) <= 1) {
      return memo[l][r] = TRUE;
    } else {
      return memo[l][r] = FALSE;
    }
  }

  if (r - 1 == l) {
    return memo[l][r] = FALSE;
  }

  if (memo[l][r] != NOT_CHECKED) {
    return memo[l][r];
  }

  if (abs(w[l] - w[r - 1]) <= 1) {
    for (int i = l + 1; i < r; i++) {
      int temp1 = rec(l + 1, i);
      int temp2 = rec(i, r - 1);
      if (temp1 == TRUE && temp2 == TRUE) {
        return memo[l][r] = TRUE;
      }
    }
  }

  for (int i = l + 1; i < r; i++) {
    int temp1 = rec(l, i);
    int temp2 = rec(i, r);
    if (temp1 == TRUE && temp2 == TRUE) {
      return memo[l][r] = TRUE;
    }
  }

  return memo[l][r] = FALSE;
}

int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    w.resize(n);

    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        memo[i][j] = NOT_CHECKED;
      }
    }

    rec(0, n);

    int dp[301] = {0};
    for (int r = 1; r <= n; r++) {
      dp[r] = dp[r - 1];
      for (int l = 0; l < r; l++) {
        if (memo[l][r] == TRUE) {
          dp[r] = max(dp[r], dp[l] + (r - l));
        }
      }
    }

    int ans = dp[n];
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n;
vector<int> w;

int memo[301][301];

int rec(int l, int r) {
  if (r == l) {
    return (memo[l][r] = 1);
  }
  if (memo[l][r] != 0) {
    return memo[l][r];
  }
  if (r - l == 1) {
    if (abs(w[l] - w[r]) <= 1) {
      return (memo[l][r] = 2);
    } else {
      return (memo[l][r] = 1);
    }
  }

  if (abs(w[l] - w[r]) <= 1) {
    if (rec(l + 1, r - 1) == 2) {
      return (memo[l][r] = 2);
    }
  }

  if (abs(w[l] - w[l + 1]) <= 1) {
    if (rec(l + 2, r) == 2) {
      return (memo[l][r] = 2);
    }
  }

  if (abs(w[r] - w[r - 1]) <= 1) {
    if (rec(l, r - 2) == 2) {
      return (memo[l][r] = 2);
    }
  }

  return (memo[l][r] = 1);
}

int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    w.resize(n);

    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }

    memset(memo, 0, sizeof(memo));

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if ((j - i) % 2 == 0) {
          continue;
        }
        rec(i, j);
      }
    }

    int ans = 0;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (memo[i][j] == 2) {
          ans = max(ans, j - i + 1);
        }
        for (int k = i + 1; k <= j; k++) {
          for (int l = k + 1; l < j; l++) {
            if (memo[i][k] == 2 && memo[l][j] == 2) {
              ans = max(ans, (k - i + 1) + (j - l + 1));
            }
          }
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p01096 (C++) — buggy submission, verdict: Runtime Error

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

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

int w[305], dp[305][305];
signed main(void) {
  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;
    REP(i, n) cin >> w[i];
    REP(i, 305) REP(j, 305) dp[i][j] = 0;

    FOR(W, 2, n + 1) {
      REP(i, n) {
        int j = i + W - 1;
        if (j >= n)
          continue;
        // cout << i << " " << j << endl;

        if (dp[i + 1][j - 1] == W - 2 && abs(w[i] - w[j]) <= 1)
          dp[i][j] = W;
        FOR(k, i, j) {
          dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]);
          // cout << dp[i][j] << endl;
        }
      }
    }
    /*REP(i, n) {
            cout << i << ":";
            REP(j, n) {
                    cout << dp[i][j] << " ";
            }
            cout << endl;
    }*/
    cout << dp[0][n - 1] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

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

int w[305], dp[305][305];
signed main(void) {
  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;
    REP(i, n) cin >> w[i];
    REP(i, 305) REP(j, 305) dp[i][j] = 0;

    FOR(W, 2, n + 1) {
      REP(i, n) {
        int j = i + W - 1;
        // cout << i << " " << j << endl;

        if (dp[i + 1][j - 1] == W - 2 && abs(w[i] - w[j]) <= 1)
          dp[i][j] = W;
        FOR(k, i, j) {
          dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]);
          // cout << dp[i][j] << endl;
        }
      }
    }
    /*REP(i, n) {
            cout << i << ":";
            REP(j, n) {
                    cout << dp[i][j] << " ";
            }
            cout << endl;
    }*/
    cout << dp[0][n - 1] << endl;
  }

  return 0;
}
Problem p01096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int n;
int w[301];
int dp[301][301];

int func(int l, int r) {
  if (dp[l][r] != -1)
    return dp[l][r];
  if (l + 1 >= r)
    return 0;
  if (l + 2 == r) {
    if (abs(w[l] - w[l + 1]) <= 1)
      return (dp[l][r] = 2);
    else
      return (dp[l][r] = 0);
  }
  int res = 0;
  for (int i = l + 1; i < r; i++) {
    res = max(res, func(l, i) + func(i, r));
  }
  res = max(res, func(l + 1, r));
  res = max(res, func(l + 2, r) + ((abs(w[l] - w[l + 1]) <= 1) ? 2 : 0));
  res = max(res, func(l, r - 1));
  res = max(res, func(l, r - 2) + ((abs(w[r - 1] - w[r - 2]) <= 1) ? 2 : 0));
  if (abs(w[l] - w[r - 1]) <= 1) {
    int v = func(l + 1, r - 1);
    if (v == ((r - 1) - (l + 1))) {
      res = r - l;
    }
  }
  return (dp[l][r] = res);
}

int main(void) {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d", &w[i]);
    }
    memset(dp, -1, sizeof(dp));
    printf("%d\\n", func(0, n));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int n;
int w[301];
int dp[301][301];

int func(int l, int r) {
  if (dp[l][r] != -1)
    return dp[l][r];
  if (l + 1 >= r)
    return 0;
  if (l + 2 == r) {
    if (abs(w[l] - w[l + 1]) <= 1)
      return 2;
    else
      return 0;
  }
  int res = 0;
  res = max(res, func(l + 1, r));
  res = max(res, func(l + 2, r) + (abs(w[l] - w[l + 1]) <= 1 ? 2 : 0));
  res = max(res, func(l, r - 1));
  res = max(res, func(l, r - 2) + (abs(w[r - 1] - w[r - 2]) <= 1 ? 2 : 0));
  if (abs(w[l] - w[r - 1]) <= 1) {
    int v = func(l + 1, r - 1);
    if (v == (r - 1) - (l + 1)) {
      res = r - l;
    }
  }
  return res;
}

int main(void) {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d", &w[i]);
    }
    memset(dp, -1, sizeof(dp));
    printf("%d\\n", func(0, n));
  }
  return 0;
}
Problem p01096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
int w[300];
int memo[300][300]; //[l,r]
int solve(int l, int r) {
  if (l >= r)
    return 0;
  if (memo[l][r] != -1)
    return memo[l][r];
  int ans1 = 0, ans2 = 0;
  for (int i = l; i <= r - 1; i++) {
    int sum = 0;
    sum += solve(l, i);
    sum += solve(i + 1, r);
    ans1 = max(ans1, sum);
  }
  for (int i = l + 1; i <= r - 1; i++) { //[l+1,i] [i+1,r-1]
    int sum = 0;
    sum += solve(l + 1, i);
    sum += solve(i + 1, r - 1);
    ans2 = max(ans2, sum);
  }
  if (ans2 == (r - l - 1) && abs(w[l] - w[r]) <= 1) {
    ans2 += 2;
  }
  return memo[l][r] = max(ans1, ans2);
}

int main() {
  int n;
  while (scanf("%d", &n), n) {
    for (int i = 0; i < n; i++) {
      scanf("%d", &w[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        memo[i][j] = -1;
    }
    printf("%d\\n", solve(0, n - 1));
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
int w[300];
int memo[300][300]; //[l,r]
int solve(int l, int r) {
  if (l >= r)
    return 0;
  if (memo[l][r] != -1)
    return memo[l][r];
  int ans = 0;
  for (int i = l; i <= r; i++) {
    for (int j = i + 1; j <= r; j++) {
      int sum = 0;
      sum += solve(l, i - 1);
      sum += solve(j + 1, r);
      int add = solve(i + 1, j - 1);
      sum += add;
      if (add == (j - i - 1) && abs(w[i] - w[j]) <= 1) {
        sum += 2;
      }
      ans = max(ans, sum);
    }
  }
  return memo[l][r] = ans;
}

int main() {
  int n;
  while (scanf("%d", &n), n) {
    for (int i = 0; i < n; i++) {
      scanf("%d", &w[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        memo[i][j] = -1;
    }
    printf("%d\\n", solve(0, n - 1));
  }
}
Problem p01096 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <math.h>
#include <bitset>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
using namespace std;
int dx[5] = {1, 0, -1, 0, 0}, dy[5] = {0, 1, 0, -1, 0};
long long gcd(long long a, long long b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
#define INF 2000000000
#define MOD 1000000007
#define sym cout << "---------" << endl;
#define ll long long
#define mk make_pair
#define en endl
#define RE return 0
#define int ll
#define P pair<int, int>

int dp[305][305];

signed main() {
  int n;
  while (cin >> n && n) {
    int w[305];
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < n; i++)
      cin >> w[i];

    for (int W = 2; W <= n; W++) {
      for (int l = 0; l < n; l++) {
        int r = l + W;
        if (r > n)
          continue;

        if (dp[l + 1][r - 1] == W - 2 && abs(w[l] - w[r - 1]) <= 1)
          dp[l][r] = W;

        for (int mid = l; mid <= r; mid++) {
          dp[l][r] = max(dp[l][r], dp[l][mid] + dp[mid][r]);
        }
      }
    }
    cout << dp[0][n] << en;
  }
}


Error: 0

Fixed submission:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <math.h>
#include <bitset>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
using namespace std;
int dx[5] = {1, 0, -1, 0, 0}, dy[5] = {0, 1, 0, -1, 0};
long long gcd(long long a, long long b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
#define INF 2000000000
#define MOD 1000000007
#define sym cout << "---------" << endl;
#define ll long long
#define mk make_pair
#define en endl
#define RE return 0
#define int ll
#define P pair<int, int>

int dp[305][305];

signed main() {
  int n;
  while (cin >> n && n) {
    int w[305];
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < n; i++)
      cin >> w[i];

    for (int W = 2; W <= n; W++) {
      for (int l = 0; l < n; l++) {
        int r = l + W;

        if (dp[l + 1][r - 1] == W - 2 && abs(w[l] - w[r - 1]) <= 1)
          dp[l][r] = W;

        for (int mid = l; mid <= r; mid++) {
          dp[l][r] = max(dp[l][r], dp[l][mid] + dp[mid][r]);
        }
      }
    }
    cout << dp[0][n] << en;
  }
}
Problem p01096 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG

#include <bits/stdc++.h>

using namespace std;

#define DUMP(x) cout << #x << ": " << x << endl

int n;
int memo[310][310] = {false};
int dp[310] = {0};

vector<int> w(n);

int rec(int l, int r) {
  if (r - l == 0) {
    return memo[l][r] = 2;
  }
  if (r - l == 1) {
    return memo[l][r] = 1;
  }

  if (r - l - 1 == 1) {
    if (abs(w[r - 1] - w[l]) <= 1) {
      return memo[l][r] = 2;
    }
  }
  if (memo[l][r]) {
    return memo[l][r];
  }

  for (int t = l + 1; t < r; t++) {
    int temp = rec(l, t);
    int temp2 = rec(t, r);
    int temp3 = rec(l + 1, t);
    int temp4 = rec(t, r - 1);

    if (temp == 2 && temp2 == 2) {
      return memo[l][r] = 2;
    }
    if (abs(w[l] - w[r - 1]) <= 1 && temp3 == 2 && temp4 == 2) {
      return memo[l][r] = 2;
    }
  }

  return memo[l][r] = 1;
}

int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }

    w.clear();
    w.resize(n);

    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }
    memset(memo, 0, sizeof(memo));
    memset(dp, 0, sizeof(dp));

    //    cerr << " " << rec(0, n) << endl;
    rec(0, n);

    dp[0] = 0;

    for (int r = 0; r <= n; r++) {
      dp[r] = dp[r - 1];
      for (int l = 0; l < r; l++) {
        if (memo[l][r] == 2) {
          dp[r] = max(dp[r], dp[l] + (r - l));
        }
      }
    }

    cout << dp[n] << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#define _GLIBCXX_DEBUG

#include <bits/stdc++.h>

using namespace std;

#define DUMP(x) cout << #x << ": " << x << endl

int n;
int memo[310][310] = {false};
vector<int> w(n);

int rec(int l, int r) {
  if (r - l == 0) {
    return memo[l][r] = 2;
  }
  if (r - l == 1) {
    return memo[l][r] = 1;
  }

  if (r - l - 1 == 1) {
    if (abs(w[r - 1] - w[l]) <= 1) {
      return memo[l][r] = 2;
    }
  }
  if (memo[l][r]) {
    return memo[l][r];
  }

  for (int t = l + 1; t < r; t++) {
    int temp = rec(l, t);
    int temp2 = rec(t, r);
    int temp3 = rec(l + 1, t);
    int temp4 = rec(t, r - 1);

    if (temp == 2 && temp2 == 2) {
      return memo[l][r] = 2;
    }
    if (abs(w[l] - w[r - 1]) <= 1 && temp3 == 2 && temp4 == 2) {
      return memo[l][r] = 2;
    }
  }

  return memo[l][r] = 1;
}

int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }

    w.clear();
    w.resize(n);

    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }
    memset(memo, 0, sizeof(memo));

    cerr << " " << rec(0, n) << endl;

    int dp[310] = {0};
    dp[0] = 0;

    for (int r = 0; r <= n; r++) {
      dp[r] = dp[r - 1];
      for (int l = 0; l < r; l++) {
        if (memo[l][r] == 2) {
          dp[r] = max(dp[r], dp[l] + (r - l));
        }
      }
    }

    cout << dp[n] << endl;
  }

  return 0;
}
Problem p01096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int n, a[1000], dp[1000][1000];
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < 1000; i++) {
      for (int j = 0; j < 1000; j++)
        dp[i][j] = 0;
    }
    for (int i = 0; i < n; i++)
      cin >> a[i];
    for (int i = 0; i < n - 1; i++) {
      if (abs(a[i] - a[i + 1]) <= 1) {
        dp[i][i + 1] = 2;
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        int a1 = j, a3 = i + j;
        if (a3 >= n)
          continue;
        for (int k = a1 + 1; k <= a3; k++) {
          int a2 = k;
          dp[a1][a3] = max(dp[a1][a3], dp[a1][a2 - 1] + dp[a2][a3]);
        }
        if (dp[a1][a3] == i + 1 && a1 > 0 && a3 < n - 1 &&
            abs(a[a1 - 1] - a[a3 + 1]) <= 1) {
          dp[a1 - 1][a3 + 1] = dp[a1][a3] + 2;
        }
      }
    }
    cout << dp[0][n - 1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int n, a[1000], dp[1000][1000];
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < 1000; i++) {
      for (int j = 0; j < 1000; j++)
        dp[i][j] = 0;
    }
    for (int i = 0; i < n; i++)
      cin >> a[i];
    for (int i = 0; i < n - 1; i++) {
      if (abs(a[i] - a[i + 1]) <= 1) {
        dp[i][i + 1] = 2;
      }
    }
    for (int h = 0; h < 30; h++) {
      for (int i = 2; i <= n; i++) {
        for (int j = 0; j < n; j++) {
          int a1 = j, a2 = i + j - 1;
          if (a2 >= n)
            continue;
          if (dp[a1][a2] == 0)
            continue;
          if (a1 > 0 && a2 < n - 1 && abs(a[a1 - 1] - a[a2 + 1]) <= 1)
            dp[a1 - 1][a2 + 1] = i + 2;
        }
      }
      for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= n; j++) {
          int a1 = j, a3 = i + j;
          if (a3 >= n)
            continue;
          for (int k = a1 + 1; k <= a3; k++) {
            int a2 = k;
            dp[a1][a3] = max(dp[a1][a3], dp[a1][a2 - 1] + dp[a2][a3]);
          }
        }
      }
    }
    cout << dp[0][n - 1] << endl;
  }
  return 0;
}
Problem p01096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int dp[300][300];
int w[300];
int rec(int s, int t) {
  if (dp[s][t] != -1)
    return dp[s][t];
  if (s == t)
    return 0;
  // if((t-s+1)%2)return max(rec(s,t-1),rec(s+1,t));
  int ans = rec(s + 1, t - 1);
  if (ans == t - s - 1 && abs(w[s] - w[t]) <= 1)
    return dp[s][t] = ans + 2;
  for (int i = s; i < t; i++) {
    ans = max(ans, rec(s, i) + rec(i + 1, t));
  }
  return dp[s][t] = ans;
}

int main() {
  int n;
  while (cin >> n, n) {
    memset(w, 0, sizeof(w));
    for (int i = 0; i < n; i++)
      cin >> w[i];
    memset(dp, -1, sizeof(dp));
    for (int i = 1; i < n; i++)
      dp[i - 1][i] = (abs(w[i - 1] - w[i]) <= 1 ? 2 : 0);
    cout << rec(0, n - 1) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define lp(i, n) for (int i = 0; i < n; i++)

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    int a[1000];
    lp(i, n) { cin >> a[i]; }
    sort(a, a + n);
    reverse(a, a + n);
    int ans = 0;
    lp(i, n) {
      if ((i + 1) % m == 0)
        continue;
      else
        ans += a[i];
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

#define REP(i, n) for (int i = 0; i < n; i++)
#define debug(x) cerr << #x << " = " << (x) << endl
#define SIZE 302

using namespace std;

int dp[SIZE][SIZE], w[SIZE];

int f(int j, int k) {
  int maxf = -1;
  if (dp[j][k] >= 0)
    return dp[j][k];
  // printf("f(%d, %d)\\n", j, k);
  if (k - j <= 0)
    return dp[j][k] = 0;
  if (k - j == 1) {
    dp[j][k] = ((abs(w[j] - w[k])) <= 1 ? 2 : 0);
    /*if (abs(w[j] - w[k]) <= 1) {
            debug(j);
            debug(k);
    }*/
    /*debug(j);
    debug(k);
    debug(dp[j][k]);*/
    return dp[j][k];
  }
  if (k - j == 2) {
    dp[j][k] = max(f(j + 1, k), f(j, k - 1));
    return dp[j][k];
  }
  for (int i = j + 1; i + 1 < k; i++) {
    maxf = max(maxf, f(j, i) + f(i + 1, k));
    // debug(i);
    // debug(maxf);
  }
  dp[j][k] =
      max(f(j + 1, k - 1) +
              ((abs(w[j] - w[k]) <= 1 && f(j + 1, k - 1) == k - j - 1) ? 2 : 0),
          maxf);
  /*if (abs(w[j] - w[k]) <= 1) {
          debug(j);
          debug(k);
          debug(dp[j][k]);
  }*/
  return dp[j][k];
}

int main() {
  while (1) {
    int n;

    cin >> n;
    if (n == 0)
      break;
    REP(i, n) cin >> w[i];
    REP(j, n) { REP(k, n) dp[j][k] = -1; }

    cout << f(0, n - 1) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

#define REP(i, n) for (int i = 0; i < n; i++)
#define debug(x) cerr << #x << " = " << (x) << endl
#define SIZE 302

using namespace std;

int dp[SIZE][SIZE], w[SIZE];

int f(int j, int k) {
  // printf("f(%d, %d)\\n", j, k);
  int maxf = -1;
  if (dp[j][k] > 0)
    return dp[j][k];
  if (j == k)
    return 0;
  if (k - j == 1) {
    dp[j][k] = ((abs(w[j] - w[k])) <= 1 ? 2 : 0);
    /*debug(j);
    debug(k);
    debug(dp[j][k]);*/
    return dp[j][k];
  }
  for (int i = j + 1; i + 1 < k; i++) {
    maxf = max(maxf, f(j, i) + f(i + 1, k));
  }
  dp[j][k] = max(f(j + 1, k - 1) + (abs(w[j] - w[k]) <= 1 ? 2 : 0), maxf);
  /*debug(j);
  debug(k);
  debug(dp[j][k]);*/
  return dp[j][k];
}

int main() {
  while (1) {
    int n;

    cin >> n;
    if (n == 0)
      break;
    REP(i, n) cin >> w[i];
    REP(j, n) { REP(k, n) dp[j][k] = -1; }

    cout << f(0, n - 1) << endl;
  }
  return 0;
}
Problem p01096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define MOD 1000000007
#define ADD(X, Y) ((X) = ((X) + (Y) % MOD) % MOD)
typedef long long i64;
typedef vector<int> iv;
typedef vector<string> svec;
typedef pair<int, int> pi;
int dd[] = {0, 1, 0, -1, 0};
#define mp make_pair
#define pb push_back
#define MAX 300
// This is DP.

int W[MAX];
int dp[MAX + 10][MAX + 10];
int n, M;
int main() {
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;

    rep(i, n) { scanf("%d", &W[i]); }

    memset(dp, 0, sizeof(dp));

    rep(k, n + 1) {
      if (k == 0)
        continue;
      rep(i, n) {
        if (i + k > n)
          continue;
        M = 0;
        if (dp[i + 1][i + k - 2] == k - 2) {
          if (abs(W[i] - W[i + k - 1]) <= 1) {
            dp[i][i + k - 1] = k;
            continue;
          }
        }
        repi(j, i, i + k - 1) { M = max(M, dp[i][j] + dp[j + 1][i + k - 1]); }

        dp[i][i + k - 1] = M;
      }
    }
    /*rep(i,n)
      {
        rep(j,n)
          {
            printf("%d ",dp[i][j]);
          }
        printf("\\n");
        }*/
    printf("%d\\n", dp[0][n - 1]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define MOD 1000000007
#define ADD(X, Y) ((X) = ((X) + (Y) % MOD) % MOD)
typedef long long i64;
typedef vector<int> iv;
typedef vector<string> svec;
typedef pair<int, int> pi;
int dd[] = {0, 1, 0, -1, 0};
#define mp make_pair
#define pb push_back

int ans;

void dfs(iv E, int depth) {

  bool flag = false;
  repi(i, 1, E.size()) {
    if (depth + E.size() <= ans)
      continue;
    if (abs(E[i] - E[i - 1]) > 1)
      continue;
    flag = true;
    iv F;
    rep(j, E.size()) {
      if (j == i || j == i - 1)
        continue;
      F.pb(E[j]);
      // printf("%d" ,E[j]);
    }
    // printf("\\n");

    dfs(F, depth + 2);
  }
  if (!flag)
    ans = max(ans, depth);
}
int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;

    iv W;
    rep(i, n) {
      int w;
      scanf("%d", &w);
      W.pb(w);
    }
    ans = 0;
    dfs(W, 0);
    printf("%d\\n", ans);
  }
  return 0;
}