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

#include <algorithm>
#include <iostream>
using namespace std;
static const int N = 100001;
int n;
int A[N], L[N], P[N];

// slow implementation
void lis() {
  L[0] = 0;
  A[0] = 0;
  P[0] = -1;
  int i, j, k;
  for (i = 1; i <= n; i++) {
    k = 0;
    for (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 MAX() {
  int m = -1, i;
  for (i = 1; i <= n; i++) {
    if (m <= L[i])
      m = L[i];
  }
  return m;
}

// quick implementation using DP and binary search
int lis2() {
  L[0] = A[0];
  int len = 1;

  int i;
  for (i = 1; i < n; i++) {
    if (L[len - 1] < A[i])
      L[len++] = A[i];

    else
      *lower_bound(L, L + len, A[i]) = A[i];
  }
  return len;
}

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

  cout <<MAX() << endl;
  */
  cout << lis2() << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
static const int N = 100001;
int n;
int A[N], L[N], P[N];

void lis() {
  L[0] = 0;
  A[0] = 0;
  P[0] = -1;
  int i, j, k;
  for (i = 1; i <= n; i++) {
    k = 0;
    for (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 MAX() {
  int m = -1, i;
  for (i = 1; i <= n; i++) {
    if (m <= L[i])
      m = L[i];
  }
  return m;
}
int main() {
  int i;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> A[i];
  }
  lis();

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

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

#define INF 1e9

int main(void) {
  int n, input;
  cin >> n;

  vector<int> dp(n + 2, INF);
  dp[0] = -INF;

  for (int i = 0; i < n; i++) {
    cin >> input;
    vector<int>::iterator it = upper_bound(dp.begin(), dp.end(), input);
    if (*(it - 1) < input)
      *it = input;
  }

  int ans = 0;
  while (dp[ans + 1] < INF) {
    ++ans;
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

#define INF 1e9

int main(void) {
  int n, input;
  cin >> n;

  vector<int> dp(n + 1, INF);
  dp[0] = -INF;

  for (int i = 0; i < n; i++) {
    cin >> input;
    vector<int>::iterator it = upper_bound(dp.begin(), dp.end(), input);
    if (*(it - 1) < input)
      *it = input;
  }

  int ans = 0;
  while (dp[ans + 1] < INF) {
    ++ans;
  }
  cout << ans << endl;

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

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

using namespace std;

const int MAX_N = 100000;
const int INF = 1000000001;

int DP[MAX_N];
int a[MAX_N];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  fill(DP, DP + n, INF);
  for (int i = 0; i < n; i++) {
    *lower_bound(DP, DP + n, a[i]) = a[i];
  }
  int i = 0;
  while (DP[i] != INF) {
    i++;
    if (i == n) {
      break;
    }
  }
  cout << i << "\\n";
}

Error: 0

Fixed submission:

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

using namespace std;

const int MAX_N = 100000;
const int INF = 999999999;

int DP[MAX_N];
int a[MAX_N];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  fill(DP, DP + n, INF);
  for (int i = 0; i < n; i++) {
    *lower_bound(DP, DP + n, a[i]) = a[i];
  }
  int i = 0;
  while (DP[i] != INF) {
    i++;
  }
  cout << i << "\\n";
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef vector<int> vi;

vi lis(vi arr) {
  vi seq, back(arr.size()), ans;
  for (int i = 0; i < arr.size(); i++) {
    int res = 0, lo = 1, hi = seq.size();
    while (lo <= hi) {
      int mid = (lo + hi) / 2;
      if (arr[seq[mid - 1]] < arr[i])
        res = mid, lo = mid + 1;
      else
        hi = mid - 1;
    }
    if (res < seq.size())
      seq[res] = i;
    else
      seq.push_back(i);
    back[i] = res == 0 ? -1 : seq[res - 1];
  }
  int at = seq.back();
  while (at != -1)
    ans.push_back(at), at = back[at];
  reverse(ans.begin(), ans.end());
  return ans;
}

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

  int N;

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

  cout << lis(num).size() << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

/*================================================================
=====        LONGEST INCREASING SUBSEQUENCE          =============
================================================================*/
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef unsigned long lli;

int main() {

  ios_base::sync_with_stdio(0);
  std::ios::sync_with_stdio(0);

  cin.tie(NULL);
  cout.tie(NULL);

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

  int *Data = new int[n];

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

  lli *DP = new lli[n];

  for (int i = 0; i < n; ++i) {
    DP[i] = 1;
  }

  for (lli i = 1; i < n; i++)
    for (lli j = 0; j < i; j++)
      if (Data[i] > Data[j])
        if (DP[i] < DP[j] + 1) // If is better to use you
          DP[i] = DP[j] + 1;

  cout << *max_element(DP, DP + n) << "\\n";
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MAX = 100000;

int search(long long dp[], long long num, int begin, int end) {
  if (begin == end - 1)
    return end;

  int mid = (begin + end) / 2;
  if (dp[mid] == num)
    return mid;

  if (dp[mid] > num) {
    return search(dp, num, begin, mid);
  } else {
    return search(dp, num, mid, end);
  }
}

int main() {
  int n;
  long long ary[MAX];
  long long dp[MAX] = {0};
  int len = 0;

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

  for (int i = 0; i < n; i++) {
    int j = search(dp, ary[i], 0, len + 1);
    if (j <= len) {
      dp[j] = ary[i];
    } else {
      dp[++len] = ary[i];
    }
  }

  cout << len << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

const int MAX = 100000;

int solve(long long ary[], int n, int dp[], int cnt, int depth) {
  if (dp[depth] != -1) {
    return cnt + dp[depth];
  }

  if (depth >= n - 1)
    return cnt;

  int res = cnt;
  for (int i = depth + 1; i < n; i++) {
    if (ary[depth] < ary[i]) {
      res = max(res, solve(ary, n, dp, cnt + 1, i));
    }
  }

  dp[depth] = res - cnt;
  return res;
}

int main() {
  int n;
  long long ary[MAX];
  int dp[MAX];

  cin >> n;

  for (int i = 0; i < n + 1; i++) {
    dp[i] = -1;
  }
  ary[0] = -1;
  for (int i = 1; i < n + 1; i++) {
    cin >> ary[i];
  }

  cout << solve(ary, n + 1, dp, 0, 0) << endl;

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

#include <stdio.h>
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_D
// LIS
// got TLE with O(n*n)
/*int findNumberOfLIS(int nums[],int n) {
        int l = n;
        int lis[l];
        for(int i= 0; i<l; i++){//as the last one
            lis[i] = 1;//for 1
            for(int j = 0; j<i; j++){
                if(nums[i]>nums[j]&&lis[j]>=lis[i]){
                    lis[i] = lis[j]+1;
                }
            }
        }
        int res = 0;
        for(int i =0; i<l; i++){
            if(lis[i]>res)
                res = lis[i];

        }
        return res;
    }*/
// binary search to find the first ele bigger than key
// return index of the ele
int ceilindex(int sec_array[], int l, int r, int key) {
  while (r - l > 1) {
    int m = l + (r - l) / 2;
    if (sec_array[m] >= key)
      r = m;
    else
      l = m;
  }
  return r;
}
int findNumberOfLIS(int nums[], int n) {
  int sec_array[n];
  int len;
  sec_array[0] = nums[0];
  len = 1;
  for (int i = 1; i < n; i++) {
    if (nums[i] < sec_array[0])
      sec_array[0] = nums[i];
    else if (nums[i] > sec_array[len - 1])
      sec_array[len++] = nums[i];
    else {
      sec_array[ceilindex(sec_array, -1, len - 1, nums[i])] = nums[i];
    }
  }
  // the sec_array is not LIS: smallest elements
  /*for(int i = 0; i<len; i++){
          printf("%d ",sec_array[i]);
  }
  printf("\\n");*/
  return len;
}

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


Error: TLE

Fixed submission:

#include <stdio.h>

int findNumberOfLIS(int nums[], int n) {
  int l = n;
  int lis[l];
  for (int i = 0; i < l; i++) { // as the last one
    lis[i] = 1;                 // for 1
    for (int j = 0; j < i; j++) {
      if (nums[i] > nums[j] && lis[j] >= lis[i]) {
        lis[i] = lis[j] + 1;
      }
    }
  }
  int res = 0;
  for (int i = 0; i < l; i++) {
    if (lis[i] > res)
      res = lis[i];
  }
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  int nums[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &nums[i]);
  }
  printf("%d\\n", findNumberOfLIS(nums, n));
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int n;
long long int a[100001];
long long int dp[100001];
int main(void) {
  // Your code here!
  int i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    dp[i] = 1000010000;
  }
  for (i = 0; i < n; i++) {
    auto it = lower_bound(dp, dp + n, a[i]);
    *it = a[i];
  }
  /*for(i=0;i<n;i++){
      cout << dp[i];
  }*/
  int ans = lower_bound(dp, dp + n, 1000010000) - dp;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
long long int a[100001];
long long int dp[100001];
int main(void) {
  // Your code here!
  int n;
  long long int ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    dp[i] = 1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
      if (a[i] > a[j])
        dp[i] = max(dp[i], dp[j] + 1);
      // if(a[i] <= a[j]) dp[i] = max(dp[i],)
    }
  }
  for (int i = 0; i < n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int MOD = 1000000007;
const int INF = int(1e9);
const int MAX = 100007;
int dp[MAX], a[MAX], n;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vi lis;
  lis.push_back(a[0]);
  for (int i = 1; i < n; i++) {
    if (a[i] > *(lis.rbegin())) {
      lis.push_back(a[i]);
    } else {
      auto it = lower_bound(lis.begin(), lis.end(), a[i]);
      *it = a[i];
    }
  }
  cout << lis.size() << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int MOD = 1000000007;
const int INF = int(1e9);
const int MAX = 100007;
int dp[MAX], a[MAX], n;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    dp[i] = 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      if (a[j] < a[i]) {
        dp[i] = max(dp[i], dp[j] + 1);
      }
    }
  }
  int res = 0;
  for (int i = 1; i <= n; i++) {
    res = max(res, dp[i]);
  }
  cout << res << "\\n";
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define INF 1234567890
int a[100000];
int dp[100000];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    dp[i] = INF;
  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:

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

typedef long long ll;
ll a[100001];
int dp[100001];

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

  for (int i = 1; i <= n; i++) {
    int maximum = 1;
    for (int j = 1; j < i; j++)
      if (a[j] < a[i] && maximum < dp[j] + 1)
        maximum = dp[j] + 1;
    dp[i] = maximum;
  }

  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, dp[i]);
  cout << ans << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_D
// O(n*log(n)) PCC p. 65 solution
#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char **argv) {
  int n;
  cin >> n;
  vector<int> dp(n, INT_MAX);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    auto p = lower_bound(dp.begin(), dp.end(), a);
    (*p) = a;
  }
  auto p = lower_bound(dp.begin(), dp.end(), INT_MAX);
  cout << distance(dp.begin(), p) << endl;
  return 0;
}

Error: TLE

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_D
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char **argv) {
  int n;
  cin >> n;
  vector<int> a(n), dp(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int l = 0;
  for (int i = 0; i < n; i++) {
    int m = 0;
    for (int j = i - 1; j >= 0; j--)
      if (a[j] < a[i] && dp[j] > m)
        m = dp[j];
    dp[i] = m + 1;
    if (dp[i] > l)
      l = dp[i];
  }
  cout << l << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#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++)

using namespace std;

int findSlot(vector<int> &tail, int d) {
  int l = -1;
  int h = tail.size() - 1;

  while (h - l != 1) {
    int m = (h + l) / 2;
    if (tail[m] >= d)
      h = m;
    else
      l = m;
  }
  return h;
}

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

  vector<int> digits(n);
  vector<int> tail;

  FOR(i, n) {
    int d;
    cin >> d;
    if (tail.empty()) {
      tail.push_back(d);
    } else if (d > tail.back()) {
      tail.push_back(d);
    } else {
      int toChange = findSlot(tail, d);
      tail[toChange] = d;
    }
  }

  cout << tail.size() << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#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++)

using namespace std;

int findSlot(vector<vector<int>> &tail, int d) {
  int l = -1;
  int h = tail.size() - 1;

  while (h - l != 1) {
    int m = (h + l) / 2;
    if (tail[m].back() >= d)
      h = m;
    else
      l = m;
  }
  return h;
}

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

  vector<int> digits(n);
  vector<vector<int>> tail;

  FOR(i, n) {
    int d;
    cin >> d;
    if (tail.empty()) {
      tail.push_back(vector<int>(1, d));
    } else if (d > tail.back().back()) {
      tail.push_back(tail.back());
      tail.back().push_back(d);
    } else {
      int toChange = findSlot(tail, d);
      tail[toChange][tail[toChange].size() - 1] = d;
    }
  }

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

#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long i64;
typedef pair<i64, i64> P;

int Lower(vector<i64> &a, vector<int> &index, int l, int r, i64 key) {
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (a[index[m]] >= key) {
      r = m;
    } else {
      l = m;
    }
  }
  return r;
}

vector<int> LIS(vector<i64> &a) {
  if (a.size() == 0)
    return vector<int>();

  vector<int> tail;
  vector<int> rev(a.size(), -1);
  tail.push_back(0);

  for (int i = 1; i < a.size(); i++) {
    if (a[i] < a[tail[0]]) {
      tail[0] = i;
    } else if (a[i] > a[tail.back()]) {
      tail.push_back(i);
    } else {
      tail[Lower(a, tail, -1, tail.size() - 1, a[i])] = i;
    }
  }

  return tail;
}

#include <iostream>

int main() {
  int n;
  cin >> n;
  vector<i64> a;

  for (int i = 1; i <= n; i++) {
    int t;
    cin >> t;
    a.push_back(t);
  }

  cout << LIS(a).size() << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long i64;

#include <iostream>
template <class T>
int CeilIndex(const vector<T> &vec, vector<int> &t, int l, int r, T key) {
  while (r - l >= 1) {
    int m = (l + r) / 2;
    if (vec[t[m]] > key)
      r = m;
    else
      l = m;
  }
  return r;
}

template <class T> vector<int> LIS(const vector<T> &vec) {
  if (vec.size() == 0)
    return vector<int>();
  vector<int> tailIndex(vec.size(), 0);
  vector<int> revIndex(vec.size(), -1);

  int len = 1;
  for (int i = 1; i < vec.size(); i++) {
    if (vec[i] < vec[tailIndex[0]]) {
      tailIndex[0] = i;
    } else if (vec[i] > vec[tailIndex[len - 1]]) {
      revIndex[i] = tailIndex[len - 1];
      tailIndex[len++] = i;
    } else {
      int pos = CeilIndex(vec, tailIndex, -1, len - 1, vec[i]);
      if (pos == len - 1)
        continue;
      revIndex[i] = tailIndex[pos - 1];
      tailIndex[pos] = i;
    }
  }

  vector<int> ans;
  for (int i = tailIndex[len - 1]; i >= 0; i = revIndex[i]) {
    ans.push_back(i);
  }
  return ans;
}

int main() {
  int n;
  cin >> n;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    a.push_back(t);
  }

  vector<int> temp = LIS(a);

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

#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long i64;

int CeilIndex(const vector<i64> &tail, int l, int r, i64 key) {
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (tail[m] >= key)
      r = m;
    else
      l = m;
  }
  return r;
}

vector<i64> LIS(const vector<i64> &vec) {
  vector<i64> tail;
  tail.push_back(vec[0]);

  for (int i = 1; i < vec.size(); ++i) {
    if (vec[i] < tail[0])
      tail[0] = vec[i];
    else if (vec[i] > tail.back())
      tail.push_back(vec[i]);
    else {
      tail[CeilIndex(tail, -1, tail.size() - 1, vec[i])] = vec[i];
    }
  }
  return tail;
}

#include <iostream>

int main() {
  int n;
  cin >> n;
  vector<i64> a;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    a.push_back(t);
  }

  cout << LIS(a).size() << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long i64;

int CeilIndex(const vector<i64> &tail, int l, int r, i64 key) {
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (tail[m] >= key)
      r = m;
    else
      l = m;
  }
  return r;
}

vector<i64> LIS(const vector<i64> &vec) {
  vector<i64> tail;
  tail.push_back(vec[0]);

  for (int i = 1; i < vec.size(); ++i) {
    if (vec[i] < tail[0])
      tail[0] = vec[i];
    else if (vec[i] > tail.back())
      tail.push_back(vec[i]);
    else {
      tail[CeilIndex(tail, 0, vec.size(), vec[i])] = vec[i];
    }
  }
  return tail;
}

#include <iostream>

int main() {
  int n;
  cin >> n;
  vector<i64> a;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    a.push_back(t);
  }

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

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

int N, A[100005], L[100005];

int main() {
  L[0] = 0;
  cin >> N;
  int i, j;
  for (i = 1; i <= N; i++) {
    cin >> A[i];
  }
  int len = 1;
  L[0] = A[1];
  for (i = 1; i <= N; i++) {
    if (L[len - 1] < A[i])
      L[len++] = A[i];
    else
      *lower_bound(L, L + len, A[i]) = A[i];
  }
  cout << len << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int N, A[100005], L[100005];

int main() {
  L[0] = 0;
  cin >> N;
  int i, j;
  for (i = 1; i <= N; i++) {
    cin >> A[i];
  }
  for (i = 1; i <= N; i++) {
    int k = 0;
    for (j = 0; j < i; j++) {
      if (A[i] > A[j] && L[j] > L[k])
        k = j;
    }
    L[i] = L[k] + 1;
  }
  sort(L + 1, L + N + 1);
  cout << L[N] << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 30, Array = int(1e5);
int main() {
  int n;
  int seq[Array], dp[Array];
  scanf("%d", &n);
  fill(dp, dp + n, INF);
  for (int i = 0; i < n; i++)
    scanf("%d", &seq[i]);
  for (int i = 0; i < n; i++)
    *lower_bound(dp, dp + n, seq[i]) = seq[i];
  cout << lower_bound(dp, dp + n, INF) - dp << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 30, Array = 0x1e6;
int main() {
  int n;
  int seq[Array], dp[Array];
  scanf("%d", &n);
  fill(dp, dp + n, INF);
  for (int i = 0; i < n; i++)
    scanf("%d", &seq[i]);
  for (int i = 0; i < n; i++)
    *lower_bound(dp, dp + n, seq[i]) = seq[i];
  cout << lower_bound(dp, dp + n, INF) - dp << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
#define int64 long long
#define INF 1000000001

int main() {

  int n;
  cin >> n;

  int64 a[n + 1];
  int dp[n + 1];
  fill(dp, dp + n, INF);
  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 <algorithm>
#include <iostream>
using namespace std;
#define int64 long long

int main() {

  int n;
  cin >> n;

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

  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;
  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) {
    scanf("%d", &A[i]);
  }
  int L[n];
  L[0] = A[0];
  int length = 1;
  for (int i = 1; i < n; ++i) {
    if (A[i] > L[length - 1]) {
      L[length++] = A[i];
    } else {
      int left = 0;
      int right = length;
      while (left < right) {
        int mid = (left + right) / 2;
        if (L[mid] >= A[i]) {
          right = mid;
        } else {
          left = mid + 1;
        }
      }
      L[right] = A[i];
    }
  }
  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) {
    scanf("%d", &A[i]);
  }

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

  return 0;
}