prithivMLmods/Coder-Stat huggingface dataset:
Problem p03346 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i >= n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

int n;
int a[1000000];

int main() {
  cin >> n;

  rep(i, n) {
    int b;
    cin >> b;
    a[b] = i + 1;
  }

  int ans = n - 1;
  int t = 1;
  FOR(i, 2, n + 1) {
    int j = i;
    while (a[j - 1] < a[j])
      j++;
    ans = min(ans, n - (j - i + 1));
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i >= n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

int n;
int a[1000000];

int main() {
  cin >> n;

  rep(i, n) {
    int b;
    cin >> b;
    a[b] = i + 1;
  }

  int ans = n - 1;
  int t = 1;
  FOR(i, 2, n + 1) {
    int j = i;
    while (a[j - 1] < a[j])
      j++;
    ans = min(ans, n - (j - i + 1));
    i = j;
  }

  cout << ans << endl;

  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (int i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define uniq(x) (x).erase(unique((x).begin(), (x).end()), (x).end())

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> p(n), idx(n);
  vector<bool> seen(n, false);
  int cont = INT_MIN;
  rep(i, n) {
    cin >> p[i];
    p[i]--;
    idx[p[i]] = i;
  }
  rep(i, n) {
    if (seen[i])
      continue;
    int cnt = 1;
    reps(ii, i, n - 1) {
      if (idx[ii] > idx[ii + 1])
        break;
      cnt++;
    }
    cont = max(cont, cnt);
  }
  cout << (n - cont) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (int i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define uniq(x) (x).erase(unique((x).begin(), (x).end()), (x).end())

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> p(n), idx(n);
  vector<bool> seen(n, false);
  int cont = INT_MIN;
  rep(i, n) {
    cin >> p[i];
    p[i]--;
    idx[p[i]] = i;
  }
  rep(i, n) {
    if (seen[i])
      continue;
    int cnt = 1;
    reps(ii, i, n - 1) {
      if (idx[ii] > idx[ii + 1])
        break;
      seen[ii] = true;
      cnt++;
    }
    cont = max(cont, cnt);
  }
  cout << (n - cont) << endl;
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
/* Madar Bozorg migoft : "dorost mishavad"
va hamishe badash b aseman negah mikard o mikhandid
hatm daram k chiz haii ""DARBARE KHODA"" midanest
k ma az an bi khabar budim :) */
// RTn_E
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define _file                                                                  \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define ll long long
#define ld long double
#define sp setprecision
#define pb push_back
#define F first
#define S second
#define mk make_pair
#define pii pair<int, int>
#define piii pair<int, pair<int, int>>
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 100;
const ll inf = 1e18;

//Main CODE :/\

vector<int> inv(1e5);
int main() {
  ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int p;
    cin >> p;
    p--;
    inv[p] = i;
  }
  int maxv = 0;
  int tmp = 0;
  for (int i = 1; i < n; i++) {
    if (inv[i - 1] < inv[i])
      tmp++;
    else {
      maxv = max(maxv, tmp);
      tmp = 0;
    }
  }
  maxv = max(maxv, tmp);
  int ans = n - maxv - 1;
  cout << ans << "\n";
  return 0;
}
// The_End
// Game Over :D


Fixed submission:
/* Madar Bozorg migoft : "dorost mishavad"
va hamishe badash b aseman negah mikard o mikhandid
hatm daram k chiz haii ""DARBARE KHODA"" midanest
k ma az an bi khabar budim :) */
// RTn_E
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define _file                                                                  \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define ll long long
#define ld long double
#define sp setprecision
#define pb push_back
#define F first
#define S second
#define mk make_pair
#define pii pair<int, int>
#define piii pair<int, pair<int, int>>
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 100;
const ll inf = 1e18;

//Main CODE :/\

vector<int> inv(2e5);
int main() {
  ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int p;
    cin >> p;
    p--;
    inv[p] = i;
  }
  int maxv = 0;
  int tmp = 0;
  for (int i = 1; i < n; i++) {
    if (inv[i - 1] < inv[i])
      tmp++;
    else {
      maxv = max(maxv, tmp);
      tmp = 0;
    }
  }
  maxv = max(maxv, tmp);
  int ans = n - maxv - 1;
  cout << ans << "\n";
  return 0;
}
// The_End
// Game Over :D
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;
int p[N], dp[N];

int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    dp[p[i]] = dp[p[i] - 1] + 1;
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, dp[i]);
  cout << n - ans << "\n";
  return 0;
}

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

const int N = 2e5 + 10;
int p[N], dp[N];

int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    dp[p[i]] = dp[p[i] - 1] + 1;
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, dp[i]);
  cout << n - ans << "\n";
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
int a[100100], pos[100100], dp[100100];
int main() {
  int n, out = 0;
  cin >> n;
  rep(i, n) {
    cin >> a[i];
    a[i]--;
    pos[a[i]] = i;
  }
  rep(i, n) {
    if (a[i] < 1 || pos[a[i] - 1] > i)
      dp[i] = 1;
    else
      dp[i] = dp[pos[a[i] - 1]] + 1;
    out = max(out, dp[i]);
  }
  cout << n - out << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
int a[200100], pos[200100], dp[200100];
int main() {
  int n, out = 0;
  cin >> n;
  rep(i, n) {
    cin >> a[i];
    a[i]--;
    pos[a[i]] = i;
  }
  rep(i, n) {
    if (a[i] < 1 || pos[a[i] - 1] > i)
      dp[i] = 1;
    else
      dp[i] = dp[pos[a[i] - 1]] + 1;
    out = max(out, dp[i]);
  }
  cout << n - out << endl;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define all(x) (x).begin(), (x).end()
const int mod = 1000000007, MAX = 100005;

int main() {

  std::ifstream in("text.txt");
  std::cin.rdbuf(in.rdbuf());

  int N;
  cin >> N;
  vector<int> S(N); // iがS[i]番目にある
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    a--;
    S[a] = i;
  }
  int ans = 0, now = S[0], cnt = 1;
  for (int i = 1; i < N; i++) {
    if (S[i] > now)
      cnt++;
    else
      cnt = 1;
    ans = max(ans, cnt);
    now = S[i];
  }
  if (N != 1)
    cout << N - ans << endl;
  else
    cout << 0 << endl;
}


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

typedef long long ll;
#define all(x) (x).begin(), (x).end()
const int mod = 1000000007, MAX = 100005;

int main() {

  int N;
  cin >> N;
  vector<int> S(N); // iがS[i]番目にある
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    a--;
    S[a] = i;
  }
  int ans = 0, now = S[0], cnt = 1;
  for (int i = 1; i < N; i++) {
    if (S[i] > now)
      cnt++;
    else
      cnt = 1;
    ans = max(ans, cnt);
    now = S[i];
  }
  if (N != 1)
    cout << N - ans << endl;
  else
    cout << 0 << endl;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define l_ength size
const int inf = (1 << 30);
const int mod = 1000000007;
using ll = long long;
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> pos(n);
  pos[0] = -1;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    pos[t] = i;
  }
  int cnt = 0;
  int ans = 0;
  for (int k = 1; k <= n; ++k) {
    if (pos[k - 1] < pos[k])
      ++cnt;
    else
      cnt = 1;
    ans = max(ans, cnt);
  }
  cout << n - ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define l_ength size
const int inf = (1 << 30);
const int mod = 1000000007;
using ll = long long;
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> pos(n + 1);
  pos[0] = -1;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    pos[t] = i;
  }
  int cnt = 0;
  int ans = 0;
  for (int k = 1; k <= n; ++k) {
    if (pos[k - 1] < pos[k])
      ++cnt;
    else
      cnt = 1;
    ans = max(ans, cnt);
  }
  cout << n - ans << endl;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;

int main() {
  ll n;
  cin >> n;
  vector<ll> p(n);
  rep(i, n) { cin >> p[i]; }
  vector<ll> v(n + 1);
  rep(i, n) { v[p[i] + 1] = v[p[i]] + 1; }
  ans = n - *max_element(v.begin(), v.end());
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;

int main() {
  ll n;
  cin >> n;
  vector<ll> p(n);
  rep(i, n) { cin >> p[i]; }
  vector<ll> v(n + 2);
  rep(i, n) { v[p[i] + 1] = v[p[i]] + 1; }
  ans = n - *max_element(v.begin(), v.end());
  cout << ans << endl;
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
  int n;
  cin >> n;
  int p[200001] = {};
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    p[tmp] = i;
  }
  int r;
  int ans = mod;
  for (int i = 1; i <= n; i++) {
    r = i;
    while (r < n && p[r] < p[r + 1]) {
      r++;
    }
    ans = min(ans, n - (r - i + 1));
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
  int n;
  cin >> n;
  int p[200001] = {};
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    p[tmp] = i;
  }
  int r;
  int ans = mod;
  for (int i = 1; i <= n; i++) {
    r = i;
    while (r < n && p[r] < p[r + 1]) {
      r++;
    }
    ans = min(ans, n - (r - i + 1));
    i = r;
  }
  cout << ans << endl;
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include "stdio.h"
#include <algorithm>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long;
using Pii = pair<int, int>;

const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const long long mod_const = 1e9 + 7;

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

using MIte = multimap<long, long, greater<long>>::iterator;

// 1406

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

  // long long?
  long long N;
  cin >> N;

  vector<int> vP(N, 0);
  rep(i, N) cin >> vP[i];
  rep(i, N) vP[i]--;

  vector<int> vidx(N, 0);
  rep(i, N) { vidx[vP[i]] = i; }

  int idx = 0;
  vector<int> vnum(N, 0);
  int vnumidx = 0;
  int num = 1;
  int maxnum = 0;
  while (1) {
    if (vidx[idx] > vidx[idx + 1]) {
      vnum[vnumidx] = num;
      maxnum = max(maxnum, num);
      num = 1;
      vnumidx++;
    } else {
      num++;
    }
    idx++;
    if (idx == vP.size() - 1) {
      vnum[vnumidx] = num;
      maxnum = max(maxnum, num);
      break;
    }
  }

  cout << N - maxnum << endl;

  return 0;
}

Fixed submission:
#include "stdio.h"
#include <algorithm>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long;
using Pii = pair<int, int>;

const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const long long mod_const = 1e9 + 7;

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

using MIte = multimap<long, long, greater<long>>::iterator;

// 1406

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

  // long long?
  long long N;
  cin >> N;

  vector<int> vP(N, 0);
  rep(i, N) cin >> vP[i];
  rep(i, N) vP[i]--;

  vector<int> vidx(N, 0);
  rep(i, N) { vidx[vP[i]] = i; }

  if (vP.size() == 1) {
    cout << "0" << endl;
    return 0;
  }

  int idx = 0;
  vector<int> vnum(N, 0);
  int vnumidx = 0;
  int num = 1;
  int maxnum = 0;
  while (1) {
    if (vidx[idx] > vidx[idx + 1]) {
      vnum[vnumidx] = num;
      maxnum = max(maxnum, num);
      num = 1;
      vnumidx++;
    } else {
      num++;
    }
    idx++;
    if (idx == vP.size() - 1) {
      vnum[vnumidx] = num;
      maxnum = max(maxnum, num);
      break;
    }
  }

  cout << N - maxnum << endl;

  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

/***********************************************/
/* Dear online judge:
 * I've read the problem, and tried to solve it.
 * Even if you don't accept my solution, you should respect my effort.
 * I hope my code compiles and gets accepted.
 *  ___  __     _______    _______
 * |\  \|\  \  |\  ___ \  |\  ___ \
 * \ \  \/  /|_\ \   __/| \ \   __/|
 *  \ \   ___  \\ \  \_|/__\ \  \_|/__
 *   \ \  \\ \  \\ \  \_|\ \\ \  \_|\ \
 *    \ \__\\ \__\\ \_______\\ \_______\
 *     \|__| \|__| \|_______| \|_______|
 */
const long long mod = 1000000007;

const int mxN = 100010;

int dp[mxN];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int N;
  cin >> N;
  deque<int> a(N);
  for (int i = 0; i < N; i++)
    cin >> a[i];
  int res = 0;
  for (int i = N - 1; i >= 0; i--) {
    dp[a[i]] = dp[a[i] + 1] + 1;
    res = max(res, dp[a[i]]);
  }
  cout << N - res << '\n';
  return 0;
}


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

/***********************************************/
/* Dear online judge:
 * I've read the problem, and tried to solve it.
 * Even if you don't accept my solution, you should respect my effort.
 * I hope my code compiles and gets accepted.
 *  ___  __     _______    _______
 * |\  \|\  \  |\  ___ \  |\  ___ \
 * \ \  \/  /|_\ \   __/| \ \   __/|
 *  \ \   ___  \\ \  \_|/__\ \  \_|/__
 *   \ \  \\ \  \\ \  \_|\ \\ \  \_|\ \
 *    \ \__\\ \__\\ \_______\\ \_______\
 *     \|__| \|__| \|_______| \|_______|
 */
const long long mod = 1000000007;

const int mxN = 200010;

int dp[mxN];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int N;
  cin >> N;
  deque<int> a(N);
  for (int i = 0; i < N; i++)
    cin >> a[i];
  int res = 0;
  for (int i = N - 1; i >= 0; i--) {
    dp[a[i]] = dp[a[i] + 1] + 1;
    res = max(res, dp[a[i]]);
  }
  cout << N - res << '\n';
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}

const int maxn = 1e5 + 1e2;

int f[maxn];
int a[maxn];
int n;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    a[i] = read();
  int ans = 0;
  for (int i = 1; i <= n; i++)
    f[a[i]] = f[a[i] - 1] + 1, ans = max(ans, f[a[i]]);
  cout << n - ans;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}

const int maxn = 3e5 + 1e2;

int f[maxn];
int a[maxn];
int n;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    a[i] = read();
  int ans = 0;
  for (int i = 1; i <= n; i++)
    f[a[i]] = f[a[i] - 1] + 1, ans = max(ans, f[a[i]]);
  cout << n - ans;
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MX = 100010, inf = 2e9;

int n, A[MX], pos[MX], bck[MX];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> A[i], pos[A[i]] = i;
  bck[n] = 1;
  for (int i = n - 1; i >= 1; i--) {
    if (pos[i + 1] > pos[i])
      bck[i] = bck[i + 1] + 1;
    else
      bck[i] = 1;
  }
  int mx = 0;
  for (int i = 1; i <= n; i++)
    mx = max(mx, bck[i]);
  cout << n - mx;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MX = 200010, inf = 2e9;

int n, A[MX], pos[MX], bck[MX];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> A[i], pos[A[i]] = i;
  bck[n] = 1;
  for (int i = n - 1; i >= 1; i--) {
    if (pos[i + 1] > pos[i])
      bck[i] = bck[i + 1] + 1;
    else
      bck[i] = 1;
  }
  int mx = 0;
  for (int i = 1; i <= n; i++)
    mx = max(mx, bck[i]);
  cout << n - mx;
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

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

#define fi first
#define se second
#define mp make_pair
#define fastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);

const int N = (int)1e5 + 9;
int p[N];

int main() {
  fastIO;
  int n;
  cin >> n;
  int q;
  for (int i = 1; i <= n; i++) {
    cin >> q;
    p[q] = i;
  }
  int cnt = 1;
  int mx = 1;
  for (int i = 1; i <= n; i++) {
    mx = max(mx, cnt);
    if (i < n) {
      if (p[i] < p[i + 1])
        cnt++;
      else
        cnt = 1;
    }
  }
  cout << n - mx;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

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

#define fi first
#define se second
#define mp make_pair
#define fastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);

const int N = (int)2e5 + 9;
int p[N];

int main() {
  fastIO;
  int n;
  cin >> n;
  int q;
  for (int i = 1; i <= n; i++) {
    cin >> q;
    p[q] = i;
  }
  int cnt = 1;
  int mx = 1;
  for (int i = 1; i <= n; i++) {
    mx = max(mx, cnt);
    if (i < n) {
      if (p[i] < p[i + 1])
        cnt++;
      else
        cnt = 1;
    }
  }
  cout << n - mx;
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
using Pll = pair<ll, ll>;

const ll INF = 1LL << 60;
const ll MOD = 1000000007;

// cout << fixed << setprecision(10);
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y); // merge technique
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};

int main() {
  ll N;
  cin >> N;
  vector<ll> P(N);
  ll midx = 0, madx = 0;
  for (int i = 0; i < N; i++) {
    cin >> P[i];
  }

  set<ll> se;
  UnionFind uf(N);

  for (int i = 0; i < N; i++) {
    se.insert(P[i]);
    if (se.count(P[i] - 1)) {
      uf.merge(P[i], P[i] - 1);
    }
  }
  ll ct = 1;
  for (int i = 0; i < N; i++) {
    ct = max(ct, (ll)uf.size(i));
  }

  cout << N - ct << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
using Pll = pair<ll, ll>;

const ll INF = 1LL << 60;
const ll MOD = 1000000007;

// cout << fixed << setprecision(10);
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n + 10, -1) {}
  void init(int n) { par.assign(n + 10, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y); // merge technique
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};

int main() {
  ll N;
  cin >> N;
  vector<ll> P(N);
  ll midx = 0, madx = 0;
  for (int i = 0; i < N; i++) {
    cin >> P[i];
  }

  set<ll> se;
  UnionFind uf(N);

  for (int i = 0; i < N; i++) {
    se.insert(P[i]);
    if (se.count(P[i] - 1)) {
      uf.merge(P[i], P[i] - 1);
    }
  }
  ll ct = 1;
  for (int i = 0; i < N; i++) {
    ct = max(ct, (ll)uf.size(i));
  }

  cout << N - ct << endl;
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define SPEED ios::sync_with_stdio(false)
#define ll long long int
#define FF first
#define SS second
#define mp make_pair
#define pb push_back
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pd(x) printf("%d\n", x)
#define plld(x) printf("%lld\n", x)
#define MOD 1000000007
#define INF 1e18
ll n, a1, a[100001];
set<ll> s;
int main() {
  SPEED;
  cin >> n;
  ll ans = 1;
  for (int i = 1; i <= n; i++) {
    cin >> a1;
    a[a1] = i;
  }
  ll i = 1;
  ll j = 2, cur = 1;
  while (j <= n) {
    while (a[j] > a[j - 1] && j <= n)
      cur++, j++;
    ans = max(ans, cur);
    cur = 1;
    i = j;
    j++;
  }
  cout << n - ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define SPEED ios::sync_with_stdio(false)
#define ll long long int
#define FF first
#define SS second
#define mp make_pair
#define pb push_back
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pd(x) printf("%d\n", x)
#define plld(x) printf("%lld\n", x)
#define MOD 1000000007
#define INF 1e18
ll n, a1, a[200001];
set<ll> s;
int main() {
  SPEED;
  cin >> n;
  ll ans = 1;
  for (int i = 1; i <= n; i++) {
    cin >> a1;
    a[a1] = i;
  }
  ll i = 1;
  ll j = 2, cur = 1;
  while (j <= n) {
    while (a[j] > a[j - 1] && j <= n)
      cur++, j++;
    ans = max(ans, cur);
    cur = 1;
    i = j;
    j++;
  }
  cout << n - ans << endl;
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <functional>
#include <iostream>
using namespace std;

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

  int *P = new int[N];
  int *Q = new int[N];
  for (int i = 0; i < N; i++) {
    cin >> P[i];
    Q[P[i]] = i;
  }

  int max_order = 1;
  int order = 1;
  for (int i = 1; i < N; i++) {
    if (Q[i - 1] < Q[i]) {
      order++;
      if (max_order < order)
        max_order = order;
    } else {
      order = 1;
    }
  }

  cout << N - max_order;
}

Fixed submission:
#include <functional>
#include <iostream>
using namespace std;

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

  int *P = new int[N];
  int *Q = new int[N];
  for (int i = 0; i < N; i++) {
    cin >> P[i];
    Q[P[i] - 1] = i;
  }

  int max_order = 1;
  int order = 1;
  for (int i = 1; i < N; i++) {
    if (Q[i - 1] < Q[i]) {
      order++;
      if (max_order < order)
        max_order = order;
    } else {
      order = 1;
    }
  }

  cout << N - max_order;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> P(N);
  for (auto &p : P)
    cin >> p;

  vector<int> C(N);
  for (int p : P) {
    C[p] = C[p - 1] == 0 ? 1 : C[p - 1] + 1;
  }

  cout << N - *max_element(C.begin(), C.end()) << endl;

  return 0;
}


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> P(N);
  for (auto &p : P)
    cin >> p;

  vector<int> C(N + 1);
  for (int p : P) {
    C[p] = C[p - 1] == 0 ? 1 : C[p - 1] + 1;
  }

  cout << N - *max_element(C.begin(), C.end()) << endl;

  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define MAXN 100   // pre-calc max n for phi(m, n)
#define MAXM 10010 // pre-calc max m for phi(m, n)
#define MAXP 40000 // max primes counter
#define MAX 400010 // max prime
#define setbit(ar, i) (((ar[(i) >> 6]) |= (1 << (((i) >> 1) & 31))))
#define chkbit(ar, i) (((ar[(i) >> 6]) & (1 << (((i) >> 1) & 31))))
#define isprime(x) (((x) && ((x)&1) && (!chkbit(ar, (x)))) || ((x) == 2))

using namespace std;

int a[100005];

int main() {
  int n, ans = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    a[x] = a[x - 1] + 1;
    if (a[x] > ans) {
      ans = a[x];
    }
  }
  printf("%d", n - ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define MAXN 100   // pre-calc max n for phi(m, n)
#define MAXM 10010 // pre-calc max m for phi(m, n)
#define MAXP 40000 // max primes counter
#define MAX 400010 // max prime
#define setbit(ar, i) (((ar[(i) >> 6]) |= (1 << (((i) >> 1) & 31))))
#define chkbit(ar, i) (((ar[(i) >> 6]) & (1 << (((i) >> 1) & 31))))
#define isprime(x) (((x) && ((x)&1) && (!chkbit(ar, (x)))) || ((x) == 2))

using namespace std;

int a[200005];

int main() {
  int n, ans = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    a[x] = a[x - 1] + 1;
    if (a[x] > ans) {
      ans = a[x];
    }
  }
  printf("%d", n - ans);
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fo(int, i, a, b) for (int i = a; i < (b); i++)
#define forev(int, i, b, a) for (int i = (b)-1; i >= a; i--)
#define uns unsigned
#define lli long long int
#define ld long double
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define enl "\n"
// #define PI 3.1415926535897932384626
// const int MOD = 1000000007; // const ll INF = 1e18; // const int MX = 100001;
//==================================================================================

void solve() {
  lli n;
  cin >> n;
  lli p[n];
  fo(lli, i, 0, n) { cin >> p[i]; }
  lli ans = 0;
  lli f[1000];
  forev(lli, i, n, 0) {
    f[p[i]] = f[p[i] + 1] + 1;
    ans = max(ans, f[p[i]]);
  }
  cout << n - ans << enl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // int testcase; cin >> testcase; for (int i = 1; i <= testcase; ++i)
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fo(int, i, a, b) for (int i = a; i < (b); i++)
#define forev(int, i, b, a) for (int i = (b)-1; i >= a; i--)
#define uns unsigned
#define lli long long int
#define ld long double
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define enl "\n"
// #define PI 3.1415926535897932384626
// const int MOD = 1000000007; // const ll INF = 1e18; // const int MX = 100001;
//==================================================================================

void solve() {
  lli n;
  cin >> n;
  lli p[n];
  fo(lli, i, 0, n) { cin >> p[i]; }
  lli ans = 0;
  lli f[1000000];
  forev(lli, i, n, 0) {
    f[p[i]] = f[p[i] + 1] + 1;
    ans = max(ans, f[p[i]]);
  }
  cout << n - ans << enl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // int testcase; cin >> testcase; for (int i = 1; i <= testcase; ++i)
  solve();
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define _ << " _ " <<
#define TRACE(x) cout << #x << " = " << x << endl

int n, p[200010], pos[200010];

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

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

  int sol = n + 1;
  for (int i = 0; i < n; i++) {
    int j;
    for (j = i + 1; j <= n && pos[j] > pos[j - 1]; j++) {
    }
    sol = min(sol, n - (j - i));
  }

  cout << sol;

  return 0;
}


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

#define _ << " _ " <<
#define TRACE(x) cout << #x << " = " << x << endl

int n, p[200010], pos[200010];

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

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

  int sol = n + 1;
  for (int i = 0; i < n; i++) {
    int j;
    for (j = i + 1; j <= n && pos[j] > pos[j - 1]; j++) {
    }
    sol = min(sol, n - (j - i));
    i = j - 1;
  }

  cout << sol;

  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;
typedef long long ll;

signed main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen("A.INP", "r")) {
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  }
  int n;
  cin >> n;
  vector<int> p(n), q(n);
  for (int i = 0; i < n; ++i) {
    cin >> p[i];
    q[p[i]] = i;
  }
  int now = 1, res = 0;
  for (int i = 1; i < n; ++i) {
    res = max(res, now);
    if (q[i] > q[i - 1])
      ++now;
    else
      now = 1;
  }
  res = max(res, now);
  cout << n - res;
}


Fixed submission:
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;
typedef long long ll;

signed main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen("A.INP", "r")) {
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  }
  int n;
  cin >> n;
  vector<int> p(n), q(n);
  for (int i = 0; i < n; ++i) {
    cin >> p[i];
    --p[i];
    q[p[i]] = i;
  }
  int now = 1, res = 0;
  for (int i = 1; i < n; ++i) {
    res = max(res, now);
    if (q[i] > q[i - 1])
      ++now;
    else
      now = 1;
  }
  res = max(res, now);
  cout << n - res;
}
Problem p03346 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), ind(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    ind[a[i]] = i;
  }

  int cnt = 1;
  for (int i = 1; i < n; i++) {
    int j = i;
    while (j < n && ind[j] > ind[j - 1]) {
      j++;
    }
    cnt = max(j - i + 1, cnt);
  }
  cout << n - cnt << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), ind(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    ind[a[i]] = i;
  }

  int cnt = 1;
  for (int i = 1; i < n; i++) {
    int j = i;
    while (j < n && ind[j] > ind[j - 1]) {
      j++;
    }
    cnt = max(j - i + 1, cnt);
    i = j;
  }
  cout << n - cnt << endl;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // sort
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <vector>

#define REP(i, n) for (long long i = 0; i < (n); i++)
typedef long long ll;
static const ll INF = 1000000000000000000LL;
using namespace std;

const int MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> p(n);
  vector<int> kai(n);
  // 階段になっている数列(1,2,3,4)の最長な気がする
  // 操作が「階段以外一気に取り出して、順番に入れ替える」と同義っぽい
  //  kai[i] = iがなんばんめに登場するか
  REP(i, n) {
    cin >> p[i];
    kai[p[i]] = i;
  }

  int ren_m = 0;
  int ren = 1;
  for (int i = 0; i < n - 1; ++i) {
    if (kai[i] < kai[i + 1]) {
      ++ren;
    } else {
      ren_m = max(ren_m, ren);
      ren = 1;
    }
  }

  ren_m = max(ren_m, ren);

  int res;
  res = n - ren_m;
  cout << res << endl;

  return 0;
}


Fixed submission:
#include <algorithm> // sort
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <vector>

#define REP(i, n) for (long long i = 0; i < (n); i++)
typedef long long ll;
static const ll INF = 1000000000000000000LL;
using namespace std;

const int MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> p(n);
  vector<int> kai(n);
  // 階段になっている数列(1,2,3,4)の最長な気がする
  // 操作が「階段以外一気に取り出して、順番に入れ替える」と同義っぽい
  //  kai[i] = iがなんばんめに登場するか
  REP(i, n) {
    cin >> p[i];
    --p[i];
    kai[p[i]] = i;
  }

  int ren_m = 0;
  int ren = 1;
  for (int i = 0; i < n - 1; ++i) {
    if (kai[i] < kai[i + 1]) {
      ++ren;
    } else {
      ren_m = max(ren_m, ren);
      ren = 1;
    }
  }

  ren_m = max(ren_m, ren);

  int res;
  res = n - ren_m;
  cout << res << endl;

  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 100008
using namespace std;
int n, a[N], l1, l2, ans[N], as;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i) {
    ans[a[i]] = ans[a[i] - 1] + 1;
    as = max(as, ans[a[i]]);
  }
  cout << n - as;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 200008
using namespace std;
int n, a[N], l1, l2, ans[N], as;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i) {
    ans[a[i]] = ans[a[i] - 1] + 1;
    as = max(as, ans[a[i]]);
  }
  cout << n - as;
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \
  cout << "{ ";                                                                \
  for (auto nth : list) {                                                      \
    cout << nth << " ";                                                        \
  }                                                                            \
  cout << "}" << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using namespace std;

int main() {
  int N;
  cin >> N;
  int P[N];
  rep(i, N) { cin >> P[i]; }

  vector<int> dp(N, 0);
  int ans = 0;
  rep(i, N) {
    int p = P[i];
    dp[p] = dp[p - 1] + 1;
    chmax(ans, dp[p]);
  }

  cout << N - ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \
  cout << "{ ";                                                                \
  for (auto nth : list) {                                                      \
    cout << nth << " ";                                                        \
  }                                                                            \
  cout << "}" << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using namespace std;

int main() {
  int N;
  cin >> N;
  int P[N];
  rep(i, N) { cin >> P[i]; }

  vector<int> dp(N + 1, 0);
  int ans = 0;
  rep(i, N) {
    int p = P[i];
    dp[p] = dp[p - 1] + 1;
    chmax(ans, dp[p]);
  }

  cout << N - ans << endl;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;

using ll = long long;

int N, A[100005], S[100005];
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &A[i]);
  }
  int res = 0;
  for (int i = 1; i <= N; i++) {
    if (S[A[i] - 1]) {
      S[A[i]] = S[A[i] - 1] + 1;
    } else {
      S[A[i]] = 1;
    }
    res = max(res, S[A[i]]);
  }
  printf("%d\n", N - res);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;

using ll = long long;

int N, A[200005], S[200005];
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &A[i]);
  }
  int res = 0;
  for (int i = 1; i <= N; i++) {
    if (S[A[i] - 1]) {
      S[A[i]] = S[A[i] - 1] + 1;
    } else {
      S[A[i]] = 1;
    }
    res = max(res, S[A[i]]);
  }
  printf("%d\n", N - res);
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5 + 6.66;
int arr[MAXN];
int rev[MAXN];
int lsl[MAXN];
int lsr[MAXN];

int main() {
  int N;
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d", arr + i);
  for (int i = 1; i <= N; i++)
    rev[arr[i]] = i;
  if (N == 1) {
    printf("0\n");
    return 0;
  }
  lsl[1] = 1;
  for (int i = 2; i <= N; i++)
    if (rev[i - 1] < rev[i])
      lsl[i] = lsl[i - 1] + 1;
    else
      lsl[i] = 1;
  lsr[N] = 1;
  for (int i = N - 1; i > 0; i--)
    if (rev[i + 1] > rev[i])
      lsr[i] = lsr[i + 1] + 1;
    else
      lsr[i] = 1;
  int ans = min(N - lsl[N], N - lsr[1]);
  for (int i = 1; i < N; i++)
    ans = min(ans, N - (rev[i] < rev[i + 1] ? lsl[i] + lsr[i + 1]
                                            : max(lsl[i], lsr[i + 1])));
  printf("%d\n", ans);
  return 0;
}


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

const int MAXN = 2e5 + 6.66;
int arr[MAXN];
int rev[MAXN];
int lsl[MAXN];
int lsr[MAXN];

int main() {
  int N;
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d", arr + i);
  for (int i = 1; i <= N; i++)
    rev[arr[i]] = i;
  if (N == 1) {
    printf("0\n");
    return 0;
  }
  lsl[1] = 1;
  for (int i = 2; i <= N; i++)
    if (rev[i - 1] < rev[i])
      lsl[i] = lsl[i - 1] + 1;
    else
      lsl[i] = 1;
  lsr[N] = 1;
  for (int i = N - 1; i > 0; i--)
    if (rev[i + 1] > rev[i])
      lsr[i] = lsr[i + 1] + 1;
    else
      lsr[i] = 1;
  int ans = min(N - lsl[N], N - lsr[1]);
  for (int i = 1; i < N; i++)
    ans = min(ans, N - (rev[i] < rev[i + 1] ? lsl[i] + lsr[i + 1]
                                            : max(lsl[i], lsr[i + 1])));
  printf("%d\n", ans);
  return 0;
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int N;
long long A[100010], ans;

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%lld", A + i);

  if (A[0] != 0) {
    printf("-1\n");
    return 0;
  }
  for (int i = 1; i < N; i++) {
    if (A[i] - A[i - 1] > 1) {
      printf("-1\n");
      return 0;
    }
    if (A[i] - A[i - 1] == 1)
      ans++;
    else
      ans += A[i];
  }

  printf("%lld\n", ans);
  return 0;
}


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

int N;
long long A[200010], ans;

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%lld", A + i);

  if (A[0] != 0) {
    printf("-1\n");
    return 0;
  }
  for (int i = 1; i < N; i++) {
    if (A[i] - A[i - 1] > 1) {
      printf("-1\n");
      return 0;
    }
    if (A[i] - A[i - 1] == 1)
      ans++;
    else
      ans += A[i];
  }

  printf("%lld\n", ans);
  return 0;
}
Problem p03347 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
int a[100100];
int main() {
  lint out = 0;
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  if (a[0] > 0) {
    cout << -1 << endl;
    return 0;
  }
  rep(i, n - 1) {
    if (a[i] < a[i + 1] - 1) {
      cout << -1 << endl;
      return 0;
    }
  }
  rep(i, n - 1) {
    if (a[i] + 1 == a[i + 1])
      out++;
    else
      out += a[i + 1];
  }
  cout << out << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
int a[200100];
int main() {
  lint out = 0;
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  if (a[0] > 0) {
    cout << -1 << endl;
    return 0;
  }
  rep(i, n - 1) {
    if (a[i] < a[i + 1] - 1) {
      cout << -1 << endl;
      return 0;
    }
  }
  rep(i, n - 1) {
    if (a[i] + 1 == a[i + 1])
      out++;
    else
      out += a[i + 1];
  }
  cout << out << endl;
}