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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int LIS(vi in) { // Longest Increasing Subsequence
  int n = in.size();
  int r = 1;
  vi out(n + 1, inf);
  out[0] = -1;
  rep(i, n) {
    for (int j = r; i >= 0; j--) {
      if (out[j] < in[i]) {
        out[j + 1] = in[i];
        r = max(r, j + 1);
        break;
      }
    }
  }
  return r;
}
int main() {
  int n;
  cin >> n;
  vi in(n);
  rep(i, n) cin >> in[i];
  cout << LIS(in) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
const double INF = 1e9;
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> out(1, INF);
  rep(i, n) {
    int in;
    cin >> in;
    bool h = false;
    rep(j, out.size()) if (out[j] >= in) {
      h = true;
      out[j] = in;
      break;
    }
    if (!h)
      out.pb(in);
  }
  cout << out.size() << endl;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <time.h>
#include <vector>
using namespace std;

int n = 0;
int A[100001] = {0};
int L[100001] = {0};

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int a[100000] = {0};
int dp[10001] = {0};
int n, w;
int main() {
  int mx;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  for (int i = n; i >= 1; --i) {
    int bd[100001] = {0};
    for (int j = i; j <= n; ++j) {
      if (i == j)
        bd[j] = 1;
      else if (a[i - 1] < a[j - 1]) {
        bd[j] = max(dp[j] + 1, bd[j - 1]);
      } else {
        bd[j] = bd[j - 1];
      }
    }
    dp[i] = bd[n];
  }
  mx = 0;
  for (int i = 0; i <= n; ++i) {
    if (mx < dp[i]) {
      mx = dp[i];
    }
  }
  printf("%d\\n", mx);
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 100000;
int arr[MAX + 1];
vector<int> L;
int n;
int lis() {
  L.push_back(arr[0]);
  for (int i = 1; i < n; i++)
    if (arr[i] > L.back())
      L.push_back(arr[i]);
    else
      *lower_bound(L.begin(), L.end(), arr[i]) = arr[i];
  return L.size();
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  cout << lis() << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
const int MAX = 100000;
int main() {
  int lis[MAX], a[MAX];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    lis[i] = 1;
  for (int i = 1; i < n; i++)
    for (int j = 0; j < i; j++)
      if (a[j] < a[i] && lis[j] + 1 > lis[i])
        lis[i] = lis[j] + 1;
  int *maxL = max_element(lis, lis + n);
  cout << *maxL << endl;
}
Problem p02317 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

int bs(vector<int> arr, int x, int a, int b) {
  if (a == b)
    return a;
  else {
    int c = (a + b) / 2;
    if (arr[c] < x && arr[c + 1] >= x)
      return c + 1;
    else if (arr[c] >= x)
      return bs(arr, x, a, c);
    else
      return bs(arr, x, c, b);
  }
}

int main() {
  int N, len, a_i;
  cin >> N;
  vector<int> arr(N), lis;
  for (int i = 0; i < N; i++)
    cin >> arr[i];
  lis.push_back(arr[0]);
  len = 1;
  for (int i = 1; i < N; i++) {
    if (lis[len - 1] < arr[i]) {
      lis.push_back(arr[i]);
      len++;
    } else {
      lis[bs(lis, arr[i], 0, len - 1)] = arr[i];
    }
  }
  cout << len << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int bs(vector<int> arr, int x, int a, int b) {
  if (a == b)
    return a;
  else {
    int c = (a + b) / 2;
    if (arr[c] < x && arr[c + 1] > x)
      return c + 1;
    else if (arr[c] > x)
      return bs(arr, x, a, c);
    else
      return bs(arr, x, c, b);
  }
}

int main() {
  int N, len, a_i;
  cin >> N;
  vector<int> arr(N), lis;
  for (int i = 0; i < N; i++)
    cin >> arr[i];
  lis.push_back(arr[0]);
  len = 1;
  for (int i = 1; i < N; i++) {
    if (lis[len - 1] < arr[i]) {
      lis.push_back(arr[i]);
      len++;
    } else {
      lis[bs(lis, arr[i], 0, lis.size() - 1)] = arr[i];
    }
  }
  cout << len << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef stack<int> si;
typedef deque<int> dqi;

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define PF push_front
#define PB push_back
#define FST first
#define SEC second
#define sz(obj) ((int)(obj).size())
#define ALL(v) (v).begin(), (v).end()

const int INF = 999999999;
const int MOD = 1e9 + 7;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

inline int in() {
  int x;
  std::cin >> x;
  return x;
}

template <typename T> void print(std::vector<T> &v, char c = ' ') {
  REP(i, v.size()) {
    if (i != 0)
      std::cout << c;
    std::cout << v[i];
  }
  std::cout << endl;
}
template <typename T> void print(T x) { std::cout << x << '\\n'; }

int gcd(int a, int b) {
  int c;
  while (a != 0) {
    c = a;
    a = b % a;
    b = c;
  }
  return b;
}

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

  int n = in();
  int dp[100005];
  fill(dp, dp + n, MOD);
  int ans = 0;
  vector<int> a(n);
  REP(i, n) a[i] = in();

  REP(i, n) { *lower_bound(dp, dp + n, a[i]) = a[i]; }

  print(lower_bound(dp, dp + n, MOD) - dp);

  /*  REP(i, n){
      dp[i] = 1;
      REP(j, i){
        if (a[i] > a[j]){
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
      ans = max(ans, dp[i]);
    }
  */

  return (0);
}

Error: TLE

Fixed submission:

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

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef stack<int> si;
typedef deque<int> dqi;

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define PF push_front
#define PB push_back
#define FST first
#define SEC second
#define sz(obj) ((int)(obj).size())
#define ALL(v) (v).begin(), (v).end()

const int INF = 999999999;
const int MOD = 1e9 + 7;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

inline int in() {
  int x;
  std::cin >> x;
  return x;
}

template <typename T> void print(std::vector<T> &v, char c = ' ') {
  REP(i, v.size()) {
    if (i != 0)
      std::cout << c;
    std::cout << v[i];
  }
  std::cout << endl;
}
template <typename T> void print(T x) { std::cout << x << '\\n'; }

int gcd(int a, int b) {
  int c;
  while (a != 0) {
    c = a;
    a = b % a;
    b = c;
  }
  return b;
}

int dp[100005];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n = in();
  int ans = 0;
  vector<int> a(n);
  REP(i, n) a[i] = in();

  REP(i, n) {
    dp[i] = 1;
    REP(j, i) {
      if (a[i] > a[j]) {
        dp[i] = max(dp[i], dp[j] + 1);
      }
    }
    ans = max(ans, dp[i]);
  }

  print(ans);

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

#include <bits/stdc++.h>
using namespace std;
int const MN = 100000;
int const INF = 1e9 + 7;

int dp[MN + 1];
vector<int> a;

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

    a.resize(n);
    for(auto &t: a){
        cin >> t;
    }

    fill(dp,dp+MN+1,1);

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

    cout << res << endl;
}*/

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

  a.resize(n);
  for (auto &t : a) {
    cin >> t;
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int const MN = 100000;

int dp[MN + 1];
vector<int> a;

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

  a.resize(n);
  for (auto &t : a) {
    cin >> t;
  }

  fill(dp, dp + MN + 1, 1);

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

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

/*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_D&lang=jp
*/
// 最長増加部分列
#include <climits>
#include <iostream>
using namespace std;

int n;
int L[100000];
int dp[100000];

// O(n^2)アルゴリズム
/*
int LIS(){
  int length=0;
  for(int i=0;i<n;i++){
    dp[i]=1;
    for(int j=0;j<i;j++){
      if(L[i]>L[j]) dp[i]=max(dp[i],dp[j]+1);
    }
    length=max(length,dp[i]);
  }
  return length;
}
*/
int LIS() {
  fill(dp, dp + n, INT_MAX);
  for (int i = 0; i < n; i++) {
    *lower_bound(dp, dp + n, L[i]) = L[i];
  }
  return lower_bound(dp, dp + n, INT_MAX) - dp;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> L[i];
  }
  cout << LIS() << "\\n";
}


Error: TLE

Fixed submission:

/*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_D&lang=jp
*/
// 最長増加部分列
#include <iostream>
using namespace std;

#define INF 99999999
int n;
int L[100000];
int dp[100000];

// O(n^2)アルゴリズム

int LIS() {
  int length = 0;
  for (int i = 0; i < n; i++) {
    dp[i] = 1;
    for (int j = 0; j < i; j++) {
      if (L[i] > L[j])
        dp[i] = max(dp[i], dp[j] + 1);
    }
    length = max(length, dp[i]);
  }
  return length;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> L[i];
  }
  cout << LIS() << "\\n";
}
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 V[n];
  V[0] = A[0];
  int length = 1;
  for (int i = 1; i < n; ++i) {
    if (A[i] > V[length - 1]) {
      V[length++] = A[i];
    } else {
      int l = 0;
      int r = length;
      while (l < r) {
        int mid = (l + r) / 2;
        if (A[i] <= V[mid]) {
          r = mid;
        } else {
          l = mid + 1;
        }
      }
      V[r] = 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 V[n + 1];
  V[0] = 0;
  for (int i = 1; i <= n; ++i) {
    int k = 0;
    for (int j = 0; j < i; ++j) {
      if (A[i] > A[j] && V[j] > V[k])
        k = j;
    }
    V[i] = V[k] + 1;
  }
  int maxv = 0;
  for (int i = 0; i <= n; ++i) {
    maxv = max(maxv, V[i]);
  }
  cout << maxv << 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 l = 0;
      int r = length;
      while (l < r) {
        int mid = (l + r) / 2;
        if (L[mid] < A[i]) {
          l = mid + 1;
        } else {
          r = mid;
        }
      }
      L[l] = 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]);
  }
  A[0] = 0;
  int L[n + 1];
  L[0] = 0;
  for (int i = 1; i <= n; ++i) {
    int k = 0;
    for (int j = 1; 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;
}
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 V[n];
  V[0] = A[0];
  int length = 1;
  for (int i = 1; i < n; ++i) {
    if (A[i] > V[length - 1]) {
      V[length++] = A[i];
    } else {
      int left = 0;
      int right = length;
      while (left < right) {
        int mid = (left + right) / 2;
        if (V[mid] >= A[i]) {
          right = mid;
        } else {
          left = mid + 1;
        }
      }
      V[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 V[n + 1];
  for (int i = 0; i <= n; ++i) {
    V[i] = 0;
  }
  for (int i = 1; i <= n; ++i) {
    int k = 0;
    for (int j = 0; j < i; ++j) {
      if (A[i] > A[j] && V[j] > V[k]) {
        k = j;
      }
    }
    V[i] = V[k] + 1;
  }
  int maxv = 0;
  for (int i = 1; i <= n; ++i) {
    maxv = max(maxv, V[i]);
  }
  cout << maxv << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N = 100000;
const int INF = 0x3f3f3f3f;

int a[100000];
int l[100000];
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> 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;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
// 这个题有毒吧,输入n为0的时候结果居然是1才能过
const int Max = 100000;
using namespace std;
typedef long long ll;
int a[10800];
int dp[10800];
int main() {
  ios::sync_with_stdio(false);
  int n;
  while (cin >> n) {
    for (int i = 0; i < n; i++)
      cin >> a[i];
    for (int i = 0; i < n; i++)
      dp[i] = 1;
    int ans = 1;
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < i; j++)
        if (a[j] < a[i])
          dp[i] = max(dp[j] + 1, dp[i]);
      ans = max(ans, dp[i]);
    }
    /*
    for(int i=0;i<n;i++)
            cout<<dp[i]<<" ";
    cout<<endl;
    */
    cout << ans << endl;
  }

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

#include <iostream>
#include <set>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, n) for (int i = 0; i < n; i++)
#define FOR2(i, z, n) for (int i = z; i < n; i++)

int find_index(vector<int> &subsequence, int length, int start, int value) {
  while (length - start > 1) {
    int medium = start + (length - start) / 2;
    if (subsequence[medium] >= value)
      length = medium;
    else
      start = medium;
  }
  return length;
}

int main() {
  int nbr;
  cin >> nbr;
  vector<int> sequence(nbr);
  vector<int> subsequence(nbr, 0);
  int length = 1;

  FOR(i, nbr) {
    int val;
    cin >> val;
    sequence[i] = val;
  }

  subsequence[0] = sequence[0];

  for (int i = 1; i < sequence.size(); i++) {
    if (sequence[i] < subsequence[0])
      subsequence[0] = sequence[i];
    else if (sequence[i] > subsequence[length - 1])
      subsequence[length++] = sequence[i];
    else {
      subsequence[find_index(subsequence, length - 1, -1, sequence[i])] =
          sequence[i];
    }
  }

  cout << length << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, n) for (int i = 0; i < n; i++)
#define FOR2(i, z, n) for (int i = z; i < n; i++)

int main() {
  int nbr;
  cin >> nbr;
  vector<int> sequence(nbr);
  vector<int> subsequence(nbr);

  FOR(i, nbr) {
    int val;
    cin >> val;
    sequence[i] = val;
    subsequence[i] = 1;
  }

  FOR2(i, 1, nbr) {
    FOR(j, i) {
      if (sequence[i] > sequence[j] && subsequence[i] < subsequence[j] + 1) {
        subsequence[i] = subsequence[j] + 1;
      }
    }
  }

  int max = 0;

  FOR(i, nbr) {
    if (max < subsequence[i]) {
      max = subsequence[i];
    }
  }

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int INF = 1000000000;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), dp(n, INF);

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

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

  auto ite = lower_bound(dp.begin(), dp.end(), INF);
  cout << distance(dp.begin(), ite) << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

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

  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 <stdio.h>

int a[100001];
int b[100001], *pb = b + 1;

int main(void) {
  int n;
  scanf("%d", &n);
  for (int i = 0; i != n; ++i)
    scanf("%d", a + i);
  *pb++ = a[0];
  for (int i = 1; i != n; ++i) {
    if (a[i] > pb[-1])
      *pb++ = a[i];
    else {
      int *p1 = b + 1, *p2 = pb;
      while (p1 <= p2) {
        int *p = p1 + (p2 - p1) / 2;
        if (a[i] > *p)
          p1 = p + 1;
        else
          p2 = p - 1;
      }
      *p1 = a[i];
    }
  }
  printf("%d\\n", pb - b - 1);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int a[100001];
int l[100001];

int main(void) {
  cin.sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 0; i != n; ++i)
    cin >> a[i];
  l[0] = 1;
  for (int i = 1; i != n; ++i) {
    l[i] = 1;
    for (int j = 0; j != i; ++j)
      if (a[i] > a[j])
        l[i] = max(l[j] + 1, l[i]);
  }
  int m = 0;
  for (int i = 0; i != n; ++i)
    m = max(m, l[i]);
  cout << m << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
int a[100010], L[100010];
int n;
int lis() {
  L[0] = a[0];
  int l = 1;
  for (int i = 1; i < n; i++) {
    if (a[i] > L[l - 1])
      L[l++] = a[i];
    else {
      *lower_bound(L, L + l, a[i]) = a[i];
    }
  }
  return l;
}
int main() {

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  printf("%d\\n", lis());
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int a[100005];
int b[100005];
int main() {
  int n;
  scanf("%d", &n);
  int maxn = -1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    b[i] = 1;
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      if (a[i] > a[j]) {
        b[i] = max(b[j] + 1, b[i]);
      }
    }
    maxn = max(b[i], maxn);
  }
  printf("%d\\n", maxn);
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define LLINF 1LL << 60
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> dp(n, LLINF);
  REP(i, n) cin >> a[i];
  REP(i, n) * lower_bound(dp.begin(), dp.end(), a[i]) = a[i];
  cout << lower_bound(dp.begin(), dp.end(), LLINF) - dp.begin() << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define DOWN(i, b, a) for (int i = b; i >= a; i--)

using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  vector<int> dp(n, 1);
  REP(i, n) cin >> a[i];
  REP(i, n) REP(j, i) if (a[i] > a[j]) dp[i] = max(dp[i], dp[j] + 1);
  cout << *max_element(dp.begin(), dp.end()) << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define MAX 100000
using namespace std;
int n;
int A[MAX + 1], L[MAX];
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]; // 函数lower_bound()在first和last中的前?后?区??行二分?找，返回大于或等于val的第一个元素位置。如果所有元素都小于val，?返回last的位置
    }
  }
  return length;
}

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

    cin >> A[i];
  }
  cout << lis() << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define MAX 100000
using namespace std;
int n;
int A[MAX + 1], L[MAX];

int erfen(int l, int r, int key) {
  int mid;
  int low;
  while (l < r) {
    mid = (l + r) / 2;
    if (L[mid] <= key)
      l = mid + 1;
    else {
      r = mid - 1;
      low = mid;
    }
  }
  return low;
}

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 {
      int p = erfen(0, length, A[i]);
      L[p] = A[i];
    }
  }
  // for(int k=0;k<length;k++){
  //    printf("%d ",L[k]);
  //}
  return length;
}

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

    cin >> A[i];
  }
  cout << lis() << endl;
  return 0;
}