alexjercan/bugnet huggingface dataset:
Problem p02316 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 100;
const int MAX_W = 10000;
int DP[MAX_N + 1][MAX_W + 1];
int v[MAX_N];
int w[MAX_N];

int main() {
  int n, W;
  scanf("%d%d", &n, &W);
  rep(i, n) { scanf("%d%d", &v[i], &w[i]); }
  rep(i, n) {
    rep(j, W + 1) {
      if (j < w[i]) {
        DP[i + 1][j] = DP[i][j];
      } else {
        DP[i + 1][j] = max(DP[i][j], DP[i + 1][j - w[i]] + v[i]);
      }
    }
  }
  printf("%d\\n", DP[n][W]);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 100;
const int MAX_W = 10000;
int DP[MAX_N][MAX_W + 1];
int v[MAX_N];
int w[MAX_N];

int main() {
  int n, W;
  cin >> n >> W;
  rep(i, n) { scanf("%d%d", &v[i], &w[i]); }
  rep(i, n) {
    rep(j, W + 1) {
      if (j < w[i]) {
        DP[i + 1][j] = DP[i][j];
      } else {
        DP[i + 1][j] = max(DP[i][j], DP[i + 1][j - w[i]] + v[i]);
      }
    }
  }
  cout << DP[n][W] << "\\n";
}
Problem p02316 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int val[10010];
int wt[10010];
int max(int a, int b) { return (a > b) ? a : b; }

int main() {

  int W;
  int n;
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> val[i] >> wt[i];
  }
  int i, w;
  unsigned int K[10010] = {0};

  for (i = 0; i <= n; i++) {
    for (w = 1; w <= W; w++) {
      if (wt[i] <= w) {
        K[w] = max(val[i] + K[w - wt[i]], K[w]); // changed from 0-1 problem
      }
    }
  }

  cout << K[W] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
const int maxnum = 1000;
int w[maxnum]; // w[i] -- the i-th item's weight
int v[maxnum]; // v[i] -- the i-th item's value
int main() {
  int N; // the total number of items
  int W; // the maximum load capacity
  cin >> N >> W;
  int w[1000]; // w[i] -- the i-th item's weight
  int v[1000]; // v[i] -- the i-th item's value
  int maxvalue[9999][9999] = {0};
  for (int i = 1; i <= N; i++) {
    cin >> v[i] >> w[i];
  }
  for (int j = 0; j <= W; j++) {
    maxvalue[0][j] = 0;
  }
  for (int counter = 1; counter <= N; counter++) {
    for (int j = 0; j <= W; j++) {
      if (j < w[counter - 1]) {
        maxvalue[counter][j] = maxvalue[counter - 1][j];
      } else {
        if (maxvalue[counter - 1][j] >
            maxvalue[counter][j - w[counter - 1]] + v[counter - 1]) {
          maxvalue[counter][j] = maxvalue[counter - 1][j];
        } else {
          maxvalue[counter][j] =
              maxvalue[counter][j - w[counter - 1]] + v[counter - 1];
        }
      }
    }
  }
  cout << maxvalue[N][W] << endl;
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;
int dp[101][10001], v[101], w[101];
int Max(int a, int b) { return a >= b ? a : b; }
int main() {
  int N, W;
  cin >> N >> W;
  for (int i = 1; i <= N; i++) {
    cin >> v[i] >> w[i];
  }
  for (int j = 0; j <= W; j++) {
    dp[0][j] = 0;
  }
  for (int i = 1; i <= N; i++)
    for (int j = 0; j <= W; j++) {
      if (j < w[i])
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = Max(dp[i][j - w[i]] + v[i],
                       Max(dp[i - 1][j - w[i]] + v[i], dp[i - 1][j]));
    }
  cout << dp[N][W] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int dp[101][10001], v[101], w[101];
int Max(int a, int b) { return a >= b ? a : b; }
int main() {
  int N, W;
  int packMax;
  cin >> N >> W;
  for (int i = 1; i <= N; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 0; i <= N; i++)
    for (int j = 0; j <= W; j++) {
      dp[0][j] = 0;
    }
  for (int i = 1; i <= N; i++)
    for (int j = 0; j <= W; j++) {
      packMax = j / w[i];
      for (int k = 0; k <= packMax; k++)
        dp[i][j] = Max(dp[i - 1][j - w[i] * k] + v[i] * k, dp[i][j]);
    }
  cout << dp[N][W] << endl;
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Runtime Error

/*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_C&lang=jp
*/
#include <iostream>
using namespace std;

int v[1001], w[1001];
int n, k;
int dp[101][10001];
/*
ナップサック
iは品物の番号、jはその時の重さ
*/
void knapsack() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      if (w[i] > j) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - w[i]] + v[i]);
      }
    }
  }
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> v[i] >> w[i];
  }
  knapsack();
  cout << dp[n][k] << "\\n";
}


Error: 0

Fixed submission:

/*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_C&lang=jp
*/
#include <iostream>
using namespace std;

int v[1001], w[1001];
int n, k;
int dp[101][10001];
/*
ナップサック
iは品物の番号、jはその時の重さ
*/
void knapsack() {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= k; j++) {
      if (w[i] > j) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - w[i]] + v[i]);
      }
    }
  }
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> v[i] >> w[i];
  }
  knapsack();
  cout << dp[n][k] << "\\n";
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

int main() {

  int N, W;
  cin >> N >> W;
  vector<vector<int>> DP(N + 1, vector<int>(W + 1, 0));
  vector<int> v(N + 1, 0);
  vector<int> w(N + 1, 0);
  for (int i = 1; i <= N; ++i) {
    cin >> v[i] >> w[i];
  }

  // int DP[N+1][W+1] = {};
  // ??°?¢?????????£??????????????§?????????0??§???????????????????????????

  for (int i = 1; i <= N; ++i) {
    for (int c = 1; c <= W; ++c) {
      if (c - w[i] < 0) {
        DP[i][c] = DP[i - 1][c];
      } else {
        DP[i][c] = max(DP[i][c - w[i]] + v[i], DP[i - 1][c]);
      }
    }
  }

  cout << DP[N][W] << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {

  int N, W;
  cin >> N >> W;
  vector<vector<int>> DP(N + 1, vector<int>(W + 1, 0));
  vector<int> v(N + 1, 0);
  vector<int> w(N + 1, 0);
  for (int i = 1; i <= N; ++i) {
    cin >> v[i] >> w[i];
  }

  for (int i = 1; i <= N; ++i) {
    for (int c = 1; c <= W; ++c) {
      int d = c / w[i];
      for (int k = 0; k <= d; ++k) {
        DP[i][c] = max(DP[i][c],
                       max(DP[i - 1][c - k * w[i]] + k * v[i], DP[i - 1][c]));
      }
    }
  }

  cout << DP[N][W] << endl;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

#define FOR(i, n) for (int i = 0; i < n; i++)
#define FORI(i, z, n) for (int i = z; i < n; i++)
#define MAX(a, b, c) max(a, max(b, c))

using namespace std;

struct Obj {
  int weight;
  int value;
};

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

  vector<Obj> objects(N);
  FOR(i, N) {
    int v, w;
    cin >> v >> w;
    objects[i] = Obj{w, v};
  }

  vector<int> dyn(W + 1);
  dyn[0] = 0;

  FORI(i, 1, W + 1)
  for (Obj o : objects)
    if (i >= o.weight)
      dyn[i] = max(dyn[i], dyn[i - o.weight] + o.value);

  cout << dyn[W] << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

#define FOR(i, n) for (int i = 0; i < n; i++)
#define FORI(i, z, n) for (int i = z; i < n; i++)
#define MAX(a, b, c) max(a, max(b, c))

using namespace std;

struct Obj {
  int weight;
  int value;
};

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

  vector<Obj> objects(N);
  FOR(i, N) {
    int v, w;
    cin >> v >> w;
    objects[i] = Obj{w, v};
  }

  vector<vector<int>> dyn(N + 1, vector<int>(W + 1, 0));

  FOR(i, N) {
    int oWeight = objects[i].weight;
    int oValue = objects[i].value;
    FOR(w, W) {
      int x = i + 1;
      int y = w + 1;
      int maxValue = max(dyn[x - 1][y], dyn[x][y - 1]);
      int k = 1;
      while (y - k * oWeight >= 0) {
        maxValue = max(dyn[x - 1][y - k * oWeight] + k * oValue, maxValue);
        k++;
      }
      dyn[x][y] = maxValue;
    }
  }

  cout << dyn[N][W] << endl;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int A[100100], L[100100], P[100100];
int n, ans;

void simple_DP() {
  // 添字が一つずれていることに注意
  A[0] = -1;
  P[0] = -1;

  for (int i = 1; i <= n; ++i) { // DP about A[i]
    int k = 0;
    for (int j = 0; j < i; ++j) { // define recurrence relation
      if (L[j] > L[k] && A[j] < A[i]) {
        k = j;
      }
      L[i] = L[k] + 1;
      P[i] = k;
    }
  }

  ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, L[i]);
  }

  cout << ans << endl;
  /*for debug
  for(int i=1;i<=n;++i){
      cout<<L[i]<<endl;
  }
  */
}

void LIS() {
  int length = 1;
  L[0] = A[0];

  for (int i = 1; i < n; ++i) {
    if (L[length - 1] < A[i]) {
      L[length++] = A[i];
    } else {
      *lower_bound(L, L + length, A[i]) = A[i];
    }
  }
  cout << length << endl;
}

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

  LIS();

  // simple_DP(); TLE
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int A[100100], L[100100], P[100100];
int n, ans;

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
  }
  A[0] = -1;
  P[0] = -1;

  for (int i = 1; i <= n; ++i) { // DP about A[i]
    int k = 0;
    for (int j = 0; j < i; ++j) { // define recurrence relation
      if (L[j] > L[k] && A[j] < A[i]) {
        k = j;
      }
      L[i] = L[k] + 1;
      P[i] = k;
    }
  }

  ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, L[i]);
  }

  cout << ans << endl;

  /*for debug
  for(int i=1;i<=n;++i){
      cout<<L[i]<<endl;
  }
  */
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <queue>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#pragma comment(lib, "winmm.lib")
#define debug(x) cout << #x << ": " << x << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
using namespace std;

vector<int> A(100001);
vector<int> L(100001, (1 << 30));

void Hoge() {
#ifdef _WIN32
  istream &cin = ifstream("input.txt");
#endif

  int n;
  string str1;

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

  L.at(0) = A.at(0);
  int length = 1;
  for (int i = 1; i < n; i++) {
    *lower_bound(L.begin(), L.end(), A[i]) = A[i];
  }

  int ans =
      (int)distance(L.begin(), lower_bound(L.begin(), L.end(), (1 << 30)));
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  Hoge();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <queue>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#define debug(x) cout << #x << ": " << x << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
using namespace std;

int A[100001];
int memo[100001];

int dfs(int n) {
  if (memo[n])
    return memo[n];

  int ans = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (A[i] < A[n]) {
      ans = max(ans, dfs(i) + 1);
    }
  }
  return memo[n] = ans;
}

void Hoge() {
  int n;
  string str1;

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

  int ans = 0;
  rep(i, n) ans = max(ans, dfs(i));

  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

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

#include <bits/stdc++.h>

using namespace std;

int main() {

  int n, len = 0;

  cin >> n;

  int num[n], dp[n];

  fill(dp, dp + n, 1 << 30);

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

  dp[0] = num[0];
  for (int i = 1; i < n; ++i) {
    if (dp[len] < num[i]) {
      dp[++len] = num[i];
    } else {
      *lower_bound(dp, dp + len, num[i]) = num[i];
    }
  }

  cout << len + 1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {

  int n, longest = 0;
  int num[100000];
  int dp[100000];

  cin >> n;

  fill(dp, dp + n, 1);

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

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      if (num[i] > num[j]) {
        dp[i] = max(dp[i], dp[j] + 1);
      }
    }
    longest = max(longest, dp[i]);
  }

  cout << longest << endl;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;
int N;
const int MAX_N = 100000, INF = 1000000000;
int a[MAX_N];
int dp[MAX_N]; // dp????????????

void solve() {
  fill(dp, dp + N, INF);
  for (int i = 0; i < N; i++) {
    *lower_bound(dp, dp + N, a[i]) = a[i];
  }
  cout << lower_bound(dp, dp + N, INF) - dp << endl;
}

int main() {

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;
int N;
const int MAX_N = 100000;
int a[MAX_N];
int dp[MAX_N]; // dp????????????

void solve() {
  int res = 0;
  for (int i = 0; i < N; i++) {
    dp[i] = 1;
    for (int j = 0; j < i; j++) {
      if (a[j] < a[i]) {
        dp[i] = max(dp[i], dp[j] + 1);
      }
    }
    res = max(res, dp[i]);
  }
  cout << res << endl;
}

int main() {

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  solve();
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())

#define pb push_back
#define mp make_pair

// ?§£??¬??????????????????????£?
// ?????¬???p65??¨??????

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

  vector<int> A(N);
  REP(n, N) cin >> A[n];

  int INF = 1e9;
  vector<int> dp(N + 1, INF);
  REP(i, N) {
    auto a = A[i];
    *lower_bound(ALL(dp), a) = a;
  }
  auto it2 = lower_bound(ALL(dp), INF);
  cout << (it2 - dp.begin()) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())

#define pb push_back
#define mp make_pair

//     5 1 3 2 4
// -------------
// 0   0 0 0 0 0
// 1   0 1 1 1 1
// 2   0 0 0 2 2
// 3   0 0 2 2 2
// 4   0 0 0 0 3
// 5   1 1 1 1 1

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

  // vector<int> as(N);
  int MX = 100000;
  vector<vector<int>> dp(N + 1, vector<int>(100000));
  REP(n, N) {
    int a;
    cin >> a;

    if (n == 0) {
      dp[n + 1][a] = 1;
    } else {
      // ??°?????????????????´???
      REP(b, a) {
        dp[n + 1][a] = max(max(dp[n][a], dp[n + 1][a]), dp[n][b] + 1);
      }

      // ??°???????????°????????´???
      REP(b, MX + 1) { dp[n + 1][b] = max(dp[n][b], dp[n + 1][b]); }
    }
  }

  int ret = 0;
  REP(b, MX + 1) { ret = max(ret, dp[N][b]); }

  cout << ret << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MAX_N 1000100
int n, dat[2 * (MAX_N)-1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = 0;
}

void update(int k, int a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

//[a,b)
int query(int a, int b, int k = 0, int l = 0, int r = n) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return dat[k];
  else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
}

int main() {
  cin >> n;
  int tn = n;
  vector<int> a(100010), b;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b.push_back(a[i]);
  }
  sort(b.begin(), b.end());
  for (int i = 0; i < n; i++) {
    a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
  }
  init(n);
  for (int i = 0; i < tn; i++) {
    int t = query(0, a[i]);
    if (dat[a[i] + n - 1] >= t + 1)
      continue;
    update(a[i], t + 1);
  }
  cout << query(0, tn) << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX_N 100010
int n, dat[2 * (MAX_N)-1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = 0;
}

void update(int k, int a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

//[a,b)
int query(int a, int b, int k = 0, int l = 0, int r = n) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return dat[k];
  else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
}

int main() {
  cin >> n;
  int tn = n;
  vector<int> a(100010), b;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b.push_back(a[i]);
  }
  sort(b.begin(), b.end());
  for (int i = 0; i < n; i++) {
    a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
  }
  init(n);
  for (int i = 0; i < tn; i++) {
    int t = query(0, a[i]);
    if (dat[a[i] + n - 1] >= t + 1)
      continue;
    update(a[i], t + 1);
  }
  cout << query(0, tn) << endl;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, j, k) for (int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for (int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7

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

int N, a[100000];
int dp[100000];

int main() {
  scanf("%d", &N);
  rep(i, N) scanf("%d", &a[i]);
  fill(dp, dp + N, INF);
  rep(i, N) { *lower_bound(dp, dp + N, a[i]) = a[i]; }
  printf("%d\\n", lower_bound(dp, dp + N, INF) - dp);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, j, k) for (int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for (int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7

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

int N, a[100000];
int dp[100000];

int main() {
  scanf("%d", &N);
  rep(i, N) scanf("%d", &a[i]);
  int ans = 0;
  rep(i, N) {
    dp[i] = 1;
    FOR(j, 0, i) if (a[j] < a[i]) { dp[i] = max(dp[i], dp[j] + 1); }
    ans = max(ans, dp[i]);
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int my_binary_search(vector<long long> array, int left, int right,
                     long long value) {
  int med;
  while (left < right) {
    med = (left + right) / 2;
    if (array[med] == value)
      return med;
    else if (array[med] > value)
      right = med;
    else
      left = med + 1;
  }
  return right;
}

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  const int inf = 1000000001;
  vector<long long> l(n, inf);
  l[0] = a[0];
  int length = 1;
  for (int i = 1; i < n; i++) {
    // cout << "a[i]= " << a[i] << endl;
    // cout << "length= " << length << endl;
    if (l[length - 1] < a[i]) {
      l[length++] = a[i];
    } else {
      // int j = my_binary_search(l, 0, length-1, a[i]);
      // l[j] = a[i];
      *lower_bound(l.begin(), l.end(), a[i]) = a[i];
    }
    // for(int k = 0; k < length; k++) cout << l[k] << " "; cout << endl;
  }
  cout << length << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int my_binary_search(vector<long long> array, int left, int right,
                     long long value) {
  int med;
  while (left < right) {
    med = (left + right) / 2;
    if (array[med] == value)
      return med;
    else if (array[med] > value)
      right = med;
    else
      left = med + 1;
  }
  return right;
}

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  const int inf = 1000000001;
  vector<long long> l(n, inf);
  l[0] = a[0];
  int length = 1;
  for (int i = 1; i < n; i++) {
    // cout << "a[i]= " << a[i] << endl;
    // cout << "length= " << length << endl;
    if (l[length - 1] < a[i]) {
      l[length++] = a[i];
    } else {
      int j = my_binary_search(l, 0, length - 1, a[i]);
      l[j] = a[i];
    }
    // for(int k = 0; k < length; k++) cout << l[k] << " "; cout << endl;
  }
  cout << length << endl;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int n, a[100000];
  // input
  scanf(" %d", &n);
  for (int i = 0; i < n; ++i)
    scanf(" %d", &a[i]);

  const int INF = 1e9 + 1;
  // dp[i]:=?????????i+1??§????????????????¢??????¨????????????????????????????´????????°????
  int dp[100000];
  // initialize
  fill(dp, dp + n, INF);
  for (int i = 0; i < n; ++i) {
    *lower_bound(dp, dp + n, a[i]) = a[i];
  }

  printf("%d\\n", lower_bound(dp, dp + n, INF) - dp);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int n, a[100000];
  // input
  scanf(" %d", &n);
  for (int i = 0; i < n; ++i)
    scanf(" %d", &a[i]);

  // dp[i]:=?¢??????¨??????????????????a[i]??§?????????????¢????????????????????????§???
  int dp[100000] = {0};
  dp[0] = 1;
  int ans = 0;

  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < i; ++j) {
      if (a[j] < a[i])
        dp[i] = max(dp[i], dp[j] + 1);
      else
        dp[i] = max(dp[i], 1);
    }
    ans = max(ans, dp[i]);
  }

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