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

#include <algorithm>
#include <array>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class LISubsequence {
public:
  int n;
  vector<int> a;
  vector<int> dp;
  LISubsequence() {}
  LISubsequence(int n) : n(n), a(n) {}
  int solve();
};

int LISubsequence::solve() {

  dp.push_back(a[0]);

  for (auto i = 1; i < n; ++i) {
    if (dp[dp.size() - 1] < a[i]) {
      dp.push_back(a[i]);
    } else {
      auto it = lower_bound(dp.begin(), dp.end(), a[i]);
      *it = a[i];
    }
  }

  cout << dp.size() << endl;

  return 0;
}

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

  LISubsequence lis(n);

  for (auto i = 0; i < n; ++i)
    cin >> lis.a[i];

  lis.solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class LISubsequence {
public:
  int n;
  vector<int> a;
  vector<int> dp;
  LISubsequence() {}
  LISubsequence(int n) : n(n), a(n), dp(n, 1) {}
  int solve();
};

int LISubsequence::solve() {

  int cm = 1;

  for (auto i = 1; i < n; ++i) {
    for (auto j = 0; j < i; ++j) {
      if (a[i] > a[j]) {
        dp[i] = max(dp[i], dp[j] + 1);
      }
    }
    cm = max(cm, dp[i]);
  }

  cout << cm << endl;

  return 0;
}

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

  LISubsequence lis(n);

  for (auto i = 0; i < n; ++i)
    cin >> lis.a[i];

  lis.solve();

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

///
// File:  dpl_1_d.cpp
// Author: ymiyamoto
//
// Created on Thu Nov  2 08:05:56 2017
//

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

using namespace std;

static vector<uint32_t> arr;
static vector<uint32_t> l;

int32_t main() {
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    uint32_t val;
    cin >> val;
    arr.push_back(val);
  }

  l.push_back(arr[0]);
  for (uint32_t i = 1; i < arr.size(); i++) {
    if (l[l.size() - 1] < arr[i]) {
      l.push_back(arr[i]);
    } else {
      uint32_t at = lower_bound(l.begin(), l.end(), arr[i]) - l.begin();
      l[at] = arr[i];
    }
    // for (uint32_t i = 0; i < l.size(); i++) {
    //   cout << l[i] << " ";
    // }
    // cout << endl;
  }

  cout << l.size() << endl;

  return 0;
}

Error: WA

Fixed submission:

///
// File:  dpl_1_d.cpp
// Author: ymiyamoto
//
// Created on Thu Nov  2 08:05:56 2017
//

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

using namespace std;

static vector<uint32_t> arr;
static vector<uint32_t> l;
int32_t main() {
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    uint32_t val;
    cin >> val;
    arr.push_back(val);
  }

  l.push_back(arr[0]);
  for (uint32_t i = 1; i < arr.size(); i++) {
    if (l[l.size() - 1] < arr[i]) {
      l.push_back(arr[i]);
    } else {
      uint32_t at = upper_bound(l.begin(), l.end(), arr[i]) - l.begin();
      cerr << "insert at " << at << endl;
      l[at] = arr[i];
      for (uint32_t i = 0; i < l.size(); i++) {
        cerr << l[i] << " ";
      }
      cerr << endl;
    }
  }

  for (uint32_t i = 0; i < l.size(); i++) {
    cout << l[i] << " ";
  }
  cout << endl;

  cout << l.size() << endl;

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

#include <algorithm>
#include <fstream>
#include <iostream>
// #include <chrono>
#include <climits>
using namespace std;

int main() {
  // ifstream fin("in.txt");

  /*
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
  */

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

  /*
  int dp[n]; //dpテーブル dp[i]がa_iまで見た時のLIS
  dp[0]=1; //1番目まで見た時のLISは1

  int ans=1;
  for(int i=1; i<n; i++){
    int index; int m=0;
    for(int j=0; j<i; j++){
      if(a[i]>a[j]){
        m = max(m, dp[j]);
      }
    }
    dp[i] = m+1;
    ans = max(ans, dp[i]);
  }
  */

  int dp[n]; // dpテーブル dp[i]は長さが(i+1)のLISで最小の文字が入る
  fill(dp, dp + n, INT_MAX);

  for (int i = 0; i < n; i++) {
    *(lower_bound(dp, dp + n, a[i])) = a[i];
  }

  cout << lower_bound(dp, dp + n, INT_MAX) - dp << endl;

  /*
  end = chrono::system_clock::now();
  double elapsed =
  chrono::duration_cast<chrono::milliseconds>(end-start).count();
  */

  // cout << elapsed << " s" << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

  int dp[n]; // dpテーブル dp[i]がa_iまで見た時のLIS
  dp[0] = 1; // 1番目まで見た時のLISは1

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

  cout << ans << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define rep(i, a, b) for (int(i) = (a); i < (b); i++)
#define INF 1000000000
#define MAX_N 150005
using namespace std;

int L[100005];

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

  L[0] = a[0];
  int length = 1;
  for (int i = 1; i < n; i++) {
    if (L[length - 1] < a[i]) {
      L[length++] = a[i];
    } else {
      int right = length - 1, left = 0;
      int mid = 0;
      while (left <= right) {
        mid = (right + left) / 2;
        if (L[mid] > a[i]) {
          if (mid == 0 || L[mid - 1] < a[i])
            L[mid] = a[i];
        }
        if (L[mid] < a[i])
          left = mid + 1;
        else
          right = mid - 1;
      }
    }
  }
  cout << length << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define rep(i, a, b) for (int(i) = (a); i < (b); i++)
#define INF 1000000000
#define MAX_N 150005
using namespace std;

int dp[100005];

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

  int res = 0;
  rep(i, 0, n) {
    dp[i] = 1;
    rep(j, 0, i) {
      if (a[j] < a[i]) {
        dp[i] = max(dp[i], dp[j] + 1);
      }
    }
    res = max(res, dp[i]);
  }
  cout << res << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

using namespace std;

int main() {
  int n;
  scanf("%d", &n);

  int A[n];
  for (int i = 0; i < n; ++i) {
    int v;
    scanf("%d", &v);
    A[i] = v;
  }
  //	4
  //	1
  //	6
  //	2
  //	8
  //	5
  //	7
  //	3

  //	5 length=1
  //	1 length=1
  //	3 length=2
  //	2 length=2
  //	4 length=3
  int L[n];
  L[0] = A[0];
  int length = 1;
  for (int i = 1; i < n; ++i) {
    if (L[length - 1] < A[i]) {
      L[length++] = A[i];
    } else {
      // L[j] (j ??? 0 ?????? lengh-1) ????????§ A[i] ??\\?????¨??????????????? j
      // ????????? = A[i]
      for (int j = 0; j < length; ++j) {
        if (L[j] >= A[i]) {
          L[j] = A[i];
          break;
        }
      }
    }
  }

  cout << length << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

int main() {

  int n;
  scanf("%d", &n);

  int A[n + 1];
  for (int i = 1; i <= n; ++i) {
    int v;
    scanf("%d", &v);
    A[i] = v;
  }

  int L[n + 1];
  int P[n - 1];

  //	5
  //	1
  //	3
  //	2
  //	4

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

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

  cout << maxv << endl;

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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#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++)
const int INF = 1 << 28;

int main() {
  vector<int> vi;
  int n, f;
  cin >> n >> f;
  vi.push_back(f);
  FOR(i, 1, n) {
    int in;
    cin >> in;
    if (vi[vi.size() - 1] < in)
      vi.push_back(in);
    else if (in < vi[vi.size() - 1]) {
      REP(j, vi.size()) {
        if (j == vi.size() - 1)
          vi[j] = in;
        else if (in == vi[j])
          break;
        else if (vi[j] > in) {
          vi[j] = in;
          break;
        }
      }
    }
    //		REP(j, vi.size()) cout << vi[j] << ' ';
    //		cout << endl;
  }
  cout << vi.size() << endl;
  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#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++)
const int INF = 1 << 28;

int main() {
  vector<int> vi;
  int n, f;
  cin >> n >> f;
  vi.push_back(f);
  FOR(i, 1, n) {
    int in;
    cin >> in;
    if (vi[vi.size() - 1] < in)
      vi.push_back(in);
    else if (in < vi[vi.size() - 1]) {
      REP(j, vi.size()) {
        if (j == vi.size() - 1)
          vi[j] = in;
        else if (vi[j] < in) {
          vi[j - 1] = in;
          break;
        }
      }
    }
  }
  cout << vi.size() << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

// Longest Increasing Subsequence
#include <iostream>

using namespace std;

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

  L[0] = A[0];
  int length = 1;

  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;
}


Error: TLE

Fixed submission:

// Longest Increasing Subsequence
#include <iostream>

using namespace std;

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

  int maxl = 0;
  for (int i = 1; i < n + 1; i++) {
    int k = 0;
    for (int j = 0; j < i; j++) {
      if (A[j] < A[i] && L[j] > L[k])
        k = j;
    }
    L[i] = L[k] + 1;
    P[i] = k;
    maxl = max(maxl, L[i]);
  }

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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

int main() {
  long n, len, priv, cur;
  vector<long> v;
  vector<long>::iterator low;
  len = 0;
  priv = -1;
  cin >> n;
  rep(i, n) {
    cin >> cur;
    if (cur > priv) {
      v.push_back(cur);
      len++;
    } else {
      low = lower_bound(v.begin(), v.end(), cur);
      *low = cur;
      // cout << *low << endl;
    }
    priv = *(v.end() - 1);
    // cout << priv << endl;
  }
  /*for(auto itr=v.begin(); itr<v.end();itr++){
        cout<< *itr << " ";
  }
  cout << endl;*/
  cout << len << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

int main() {
  long n, len, priv, cur;
  vector<long> v;
  vector<long>::iterator low;
  len = 0;
  priv = 0;
  cin >> n;
  rep(i, n) {
    cin >> cur;
    if (cur > priv) {
      v.push_back(cur);
      len++;
    } else {
      low = lower_bound(v.begin(), v.end(), cur);
      v.insert(low + 1, cur);
      // cout << *low << endl;
    }
    priv = *(v.end() - 1);
    // cout << priv << endl;
  }
  cout << len << endl;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

using namespace std;

const long long INF = 1e12;
const int N_MAX = 100000;
int N;
int a[N_MAX + 10];
long long dp[N_MAX + 10] = {};

int main() {
  int i, j;
  scanf("%d", &N);
  for (i = 0; i < N; i++) {
    scanf("%d", &a[i]);
  }
  for (i = 0; i < N; i++) {
    dp[i] = INF;
  }
  for (i = 0; i < N; i++) {
    *lower_bound(dp, dp + N, a[i]) = a[i];
  }
  int ans = lower_bound(dp, dp + N, INF) - dp;
  // for (i = 0; i < N+1; i++) {
  //   for (j = 0; j < W+1; j++) {
  //     printf("%d ", dp[i][j]);
  //  }
  //   printf("\\n");
  //  }
  printf("%d\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

const int N_MAX = 100000;
int N;
int a[N_MAX + 10];
int dp[N_MAX + 10] = {};

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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
using namespace std;

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n), dp;
  rep(i, n) { cin >> a[i]; }
  dp.push_back(a[0]);
  FOR(i, 1, n) {
    vector<int>::iterator itr;
    itr = lower_bound(dp.begin(), dp.end(), a[i]);
    if (itr == dp.end()) {
      dp.insert(itr, a[i]);
    } else {
      dp.erase(itr);
      dp.insert(itr, a[i]);
    }
  }
  cout << dp.size() << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
using namespace std;

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n), dp(n);
  rep(i, n) { cin >> a[i]; }
  dp[0] = 1;
  FOR(i, 1, n) {
    int m = 0;
    rep(j, i) {
      if (a[j] < a[i]) {
        m = max(m, dp[j]);
      }
    }
    dp[i] = m + 1;
  }
  int m = 0;
  rep(i, n) { m = max(m, dp[i]); }
  cout << m << endl;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
#define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define vrep(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define mp make_pair
#define pb push_back
template <class T1, class T2> inline void minup(T1 &m, T2 x) {
  if (m > x)
    m = static_cast<T2>(x);
}
template <class T1, class T2> inline void maxup(T1 &m, T2 x) {
  if (m < x)
    m = static_cast<T2>(x);
}

#define INF 1000000000
#define MOD 1000000009
#define EPS 1E-9

const int MAX_N = 100000;
int n;
int dp[MAX_N];
int a[MAX_N];
int res;

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

  rep(i, n) dp[i] = INF;
  rep(i, n) * lower_bound(dp, dp + n, a[i]) = a[i];

  res = lower_bound(dp, dp + n, INF) - dp;
  cout << res << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
#define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define vrep(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define mp make_pair
#define pb push_back
template <class T1, class T2> inline void minup(T1 &m, T2 x) {
  if (m > x)
    m = static_cast<T2>(x);
}
template <class T1, class T2> inline void maxup(T1 &m, T2 x) {
  if (m < x)
    m = static_cast<T2>(x);
}

#define INF 1000000000
#define MOD 1000000009
#define EPS 1E-9

const int MAX_N = 100000;
int n;
int dp[MAX_N];
int a[MAX_N];
int res;

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

  rep(i, n) {
    dp[i] = 1;
    rep(j, i) if (a[j] < a[i]) maxup(dp[i], dp[j] + 1);
    maxup(res, dp[i]);
  }

  cout << res << endl;

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

#include <algorithm> // lower_bound()
#include <cstdio>    // printf(), scanf()

using namespace std;
static const int MAX_N = 100000;

int A[MAX_N + 1];
int L[MAX_N];
int n;

int lis() {
  L[0] = A[0];
  int length = 1;
  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];
  }

  return length;
}

int main(int argc, char **argv) {
  int i;

  scanf("%d", &n);
  for (i = 0; i < n; ++i)
    scanf("%d", &A[i]);

  printf("%d\\n", lis());
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm> // max()
#include <cstdio>    // printf(), scanf()

using namespace std;
static const int MAX_N = 100000;

int A[MAX_N];
int dp[MAX_N];
int n;

int lis() {
  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]);
  }

  return res;
}

int main(int argc, char **argv) {
  int i;

  scanf("%d", &n);
  for (i = 0; i < n; ++i)
    scanf("%d", &A[i]);

  printf("%d\\n", lis());
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
const int INF = 1000000001;

int main() {
  int n;
  cin >> n;
  int dp[100001];
  fill(dp, dp + n, INF);
  int a[100001];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  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;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

  for (i = 1; i <= n; i++) {
    k = 0;
    for (j = 0; j <= i - 1; j++) {
      if (A[j] < A[i] && L[j] > L[k]) {
        k = j;
      }
      L[i] = L[k] + 1;
      P[i] = k;
    }
  }

  int max = 0;
  int index;
  for (i = 1; i <= n; i++) {
    if (max < L[i]) {
      max = L[i];
      index = i;
    }
  }
  cout << L[index] << endl;

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
static const int MAX = 100000;

int solve(int data[], int n) {
  vector<int> l;
  l.push_back(data[0]);

  for (int i = 1; i < n; i++) {
    if (l.back() < data[i]) {
      l.push_back(data[i]);
    } else {
      *lower_bound(l.begin(), l.end(), data[i]) = data[i];
    }
  }

  return l.size();
}

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

  cout << solve(data, n) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
static const int MAX = 100000;

int solve(int data[], int n) {
  int lengths[MAX];
  lengths[0] = 1;
  int ans = 0;

  for (int i = 1; i < n; i++) {
    int premax = 0;
    for (int j = 0; j < i; j++) {
      if (data[j] < data[i]) {
        premax = max(lengths[j], premax);
      }
    }
    lengths[i] = premax + 1;
    ans = max(ans, lengths[i]);
  }
  return ans;
}

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

  cout << solve(data, n) << endl;
}
Problem p02317 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int INF = (1e9) + 100;
int main() {
  int n;
  cin >> n;
  vector<int> DP(n + 1, INF);
  DP[0] = -INF;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    int id = lower_bound(DP.begin(), DP.end(), a) - DP.begin();
    DP[id] = a;
  }
  cout << lower_bound(DP.begin(), DP.end(), INF) - DP.begin() - 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, m;
  m = 0;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
    m = max(m, A[i]);
  }
  int dp[n + 1][m + 1];
  for (int i = 0; i < n + 1; ++i) {
    for (int j = 0; j < m + 1; ++j) {
      dp[i][j] = 0;
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m + 1; ++j) {
      if (j)
        dp[i + 1][j] = max(dp[i + 1][j - 1], dp[i][j]);
      if (j >= A[i])
        dp[i + 1][j] = max(dp[i][A[i] - 1] + 1, dp[i + 1][j]);
    }
  }
  cout << dp[n][m] << endl;
  return 0;
}