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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ph push
#define ptc putchar
#define enter putchar('\n')
#define mod 998244353
using namespace std;
typedef pair<int, int> pii;
typedef double db;
typedef long double ldb;
typedef long long ll;
typedef long long lnt;
inline int read() {
  int x = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    x = (x << 1) + (x << 3) + c - '0', c = getchar();
  return x;
}
inline void write(int x) {
  if (!x) {
    ptc('0');
    return;
  }
  int dg[20], len = 0;
  while (x)
    dg[len++] = x % 10, x /= 10;
  while (len--)
    ptc(dg[len] + '0');
}
inline void writeln(int x) {
  write(x);
  ptc('\n');
}
inline int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
inline int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}
inline int qpow(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = 1ll * res * x % mod;
    x = 1ll * x * x % mod;
    y >>= 1;
  }
  return res;
}
const int N = 2e5 + 10;
int n, a[N], b[N], res;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    b[a[i]] = i;
  }
  res = n;
  for (int i = 1; i <= n; i++) {
    int j = i;
    while (j + 1 <= n && b[j + 1] > b[j])
      j++;
    res = min(res, n - (j - i + 1));
  }

  cout << res;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ph push
#define ptc putchar
#define enter putchar('\n')
#define mod 998244353
using namespace std;
typedef pair<int, int> pii;
typedef double db;
typedef long double ldb;
typedef long long ll;
typedef long long lnt;
inline int read() {
  int x = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    x = (x << 1) + (x << 3) + c - '0', c = getchar();
  return x;
}
inline void write(int x) {
  if (!x) {
    ptc('0');
    return;
  }
  int dg[20], len = 0;
  while (x)
    dg[len++] = x % 10, x /= 10;
  while (len--)
    ptc(dg[len] + '0');
}
inline void writeln(int x) {
  write(x);
  ptc('\n');
}
inline int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
inline int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}
inline int qpow(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = 1ll * res * x % mod;
    x = 1ll * x * x % mod;
    y >>= 1;
  }
  return res;
}
const int N = 2e5 + 10;
int n, a[N], b[N], res;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    b[a[i]] = i;
  }
  res = n;
  for (int i = 1; i <= n; i++) {
    int j = i;
    while (j + 1 <= n && b[j + 1] > b[j])
      j++;
    res = min(res, n - (j - i + 1));
    i = j;
  }

  cout << res;
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> K(N);
  for (int i = 0; i < N; i++) {
    cin >> K[i];
  }

  unordered_set<int> L;
  vector<bool> C(N);
  for (int i = 0; i < N; i++) {
    auto it = L.find(K[i] + 1);
    if (it != L.end()) {
      C[K[i] - 1] = true;
    } else {
      C[K[i] - 1] = false;
    }
    L.insert(K[i]);
  }

  int max = 1;
  for (int i = 1; i <= N; i++) {
    int count = 1;
    int last = i;
    for (int j = i; j < N; j++) {
      if (!C[j - 1] && j != N) {
        last++;
        count++;
        if (count > max) {
          max = count;
        }
      } else {
        i = j;
        break;
      }
    }
  }

  cout << (N - max) << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> K(N);
  for (int i = 0; i < N; i++) {
    cin >> K[i];
  }

  unordered_set<int> L;
  vector<bool> C(N);
  for (int i = 0; i < N; i++) {
    auto it = L.find(K[i] + 1);
    if (it != L.end()) {
      C[K[i] - 1] = true;
    } else {
      C[K[i] - 1] = false;
    }
    L.insert(K[i]);
  }

  int max = 1;
  for (int i = 1; i <= N; i++) {
    int count = 1;
    int last = i;
    for (int j = i; j <= N; j++) {
      if (!C[j - 1] && j != N) {
        last++;
        count++;
        if (count > max) {
          max = count;
        }
      } else {
        i = j;
        break;
      }
    }
  }

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

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

using namespace std;

const int maxn = 1e5 + 10;
int n, a[maxn], d[maxn], ans;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    d[a[i]] = i;
  }
  int t = 1;
  for (int i = 2; i <= n; ++i)
    if (d[i] > d[i - 1])
      ++t;
    else {
      ans = max(ans, t);
      t = 1;
    }
  ans = max(ans, t);
  ans = n - ans;
  cout << ans << '\n';
  return 0;
}


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

using namespace std;

const int maxn = 2e5 + 10;
int n, a[maxn], d[maxn], ans;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    d[a[i]] = i;
  }
  int t = 1;
  for (int i = 2; i <= n; ++i)
    if (d[i] > d[i - 1])
      ++t;
    else {
      ans = max(ans, t);
      t = 1;
    }
  ans = max(ans, t);
  ans = n - ans;
  cout << ans << '\n';
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef set<int> si;
typedef set<ll> sl;
typedef pair<int, int> pii;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define S second

int n;
vi v;
map<int, int> record;

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  freopen("in.txt", "r", stdin);
  cin >> n;
  v.resize(n);
  rep(i, 0, n) {
    cin >> v[i];
    record[v[i]] = i;
  }

  int r = 0;
  vector<bool> visited(n + 1, false);

  for (int x = 1; x <= n; ++x) {
    if (visited[x])
      continue;

    int y = x;
    int idx = record[y];
    while (y < n + 1 && record[y] >= idx) {
      visited[y] = true;
      idx = record[y];
      y++;
    }
    r = max(r, y - x);
  }

  cout << n - r << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef set<int> si;
typedef set<ll> sl;
typedef pair<int, int> pii;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define S second

int n;
vi v;
map<int, int> record;

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("in.txt", "r", stdin);
  cin >> n;
  v.resize(n);
  rep(i, 0, n) {
    cin >> v[i];
    record[v[i]] = i;
  }

  int r = 0;
  vector<bool> visited(n + 1, false);

  for (int x = 1; x <= n; ++x) {
    if (visited[x])
      continue;

    int y = x;
    int idx = record[y];
    while (y < n + 1 && record[y] >= idx) {
      visited[y] = true;
      idx = record[y];
      y++;
    }
    r = max(r, y - x);
  }

  cout << n - r << endl;
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main(void) {
  int n, q[20008] = {0}, m = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int p;
    cin >> p;
    if (q[p - 1] == 0) {
      q[p] = 1;
    } else {
      q[p] = q[p - 1] + 1;
    }
  }
  for (int i = 0; i < n; i++) {
    m = max(m, q[i + 1]);
  }
  cout << n - m << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
int main(void) {
  int n, q[200008] = {0}, m = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int p;
    cin >> p;
    if (q[p - 1] == 0) {
      q[p] = 1;
    } else {
      q[p] = q[p - 1] + 1;
    }
  }
  for (int i = 0; i < n; i++) {
    m = max(m, q[i + 1]);
  }
  cout << n - m << endl;
}
Problem p03346 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200005;
int n, p[MAX], u[MAX], fin;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  for (int i = 1; i <= n; i++)
    u[p[i]] = i;
  int i, j;
  for (i = 1; i <= n; i++) {
    for (j = i + 1; u[j - 1] <= u[j]; j++)
      ;
    fin = max(fin, j - i);
  }
  printf("%d\n", n - fin);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200005;
int n, p[MAX], u[MAX], fin;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  for (int i = 1; i <= n; i++)
    u[p[i]] = i;
  int i, j;
  for (i = 1; i <= n; i++) {
    for (j = i + 1; u[j - 1] <= u[j]; j++)
      ;
    fin = max(fin, j - i);
    i = j - 1;
  }
  printf("%d\n", n - fin);
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define cmin(a, b) (a > (b) ? a = (b), 1 : 0)
#define cmax(a, b) (a < (b) ? a = (b), 1 : 0)
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define dmax(a, b) ((a) > (b) ? (a) : (b))
namespace io {
int F() {
  int n = 0, F = 1;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = n * 10 + ch - '0';
  return F ? n : -n;
}
} // namespace io
int a[111111];
int ia[111111];
int main() {
  int n = io::F();
  for (register int i = 1; i <= n; ++i)
    a[i] = io::F();
  for (register int i = 1; i <= n; ++i)
    ia[a[i]] = i;
  int ans = 1;
  int now = 1;
  for (register int i = 2; i <= n; ++i)
    if (ia[i - 1] < ia[i])
      ++now, cmax(ans, now);
    else
      now = 1;
  printf("%d\n", n - ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define cmin(a, b) (a > (b) ? a = (b), 1 : 0)
#define cmax(a, b) (a < (b) ? a = (b), 1 : 0)
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define dmax(a, b) ((a) > (b) ? (a) : (b))
namespace io {
int F() {
  int n = 0, F = 1;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = n * 10 + ch - '0';
  return F ? n : -n;
}
} // namespace io
int a[222222];
int ia[222222];
int main() {
  int n = io::F();
  for (register int i = 1; i <= n; ++i)
    a[i] = io::F();
  for (register int i = 1; i <= n; ++i)
    ia[a[i]] = i;
  int ans = 1;
  int now = 1;
  for (register int i = 2; i <= n; ++i)
    if (ia[i - 1] < ia[i])
      ++now, cmax(ans, now);
    else
      now = 1;
  printf("%d\n", n - ans);
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
/*
g++ -std=c++11 -o2 -Wall ABC024B.cpp -o ./out/ABC024B.out
./out/ABC024B.out
*/

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

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define present(c, e) ((c).find((e)) != (c).end())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define tr(c, i) for (iter(c) != (c).begin(); i != (c).end(); ++i)
#define pb push_back
#define mp make_pair

typedef long long ll;

int main() {
  freopen("./input/input.txt", "r", stdin);
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int qs[200001];
  int ps[200001];
  rep(i, n) {
    int c;
    cin >> c;
    ps[i] = c;
    //    ps[i + 1] = c;
  }
  int ans = 0;
  fill(qs, qs + (n + 1), 0);
  rep(i, n) {
    qs[ps[i]] = qs[ps[i] - 1] + 1;
    if (qs[ps[i]] > ans)
      ans = qs[ps[i]];
  }
  ans = n - ans;
  cout << ans << "\n";
  return 0;
}


Fixed submission:
/*
g++ -std=c++11 -o2 -Wall ABC024B.cpp -o ./out/ABC024B.out
./out/ABC024B.out
*/

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

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define present(c, e) ((c).find((e)) != (c).end())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define tr(c, i) for (iter(c) != (c).begin(); i != (c).end(); ++i)
#define pb push_back
#define mp make_pair

typedef long long ll;

int main() {
  //  freopen("./input/input.txt", "r", stdin);
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int qs[200001];
  int ps[200001];
  rep(i, n) {
    int c;
    cin >> c;
    ps[i] = c;
    //    ps[i + 1] = c;
  }
  int ans = 0;
  fill(qs, qs + (n + 1), 0);
  rep(i, n) {
    qs[ps[i]] = qs[ps[i] - 1] + 1;
    if (qs[ps[i]] > ans)
      ans = qs[ps[i]];
  }
  ans = n - ans;
  cout << ans << "\n";
  return 0;
}
Problem p03346 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
ppp = list(map(int, (input() for _ in range(n))))

tmp = [0] * n
for p in ppp:
    tmp[p] = tmp[p - 1] + 1

print(n - max(tmp))


Fixed submission:
n = int(input())
ppp = list(map(int, (input() for _ in range(n))))

tmp = [0] * (n + 1)
for p in ppp:
    tmp[p] = tmp[p - 1] + 1

print(n - max(tmp))
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
/*
 * =template=.cpp
 * Copyright (C) 2017 hzw <hzw@huzhenweideMacBook.local>
 *
 * Distributed under terms of the MIT license.
 */

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#define FR first
#define SC second
#define MP make_pair
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
inline int pow(int a, int b, int c) {
  if (a == 0)
    return 0;
  b = (b % (c - 1) + c - 1) % (c - 1);
  int d = 1;
  while (b) {
    if (b & 1)
      d = (1LL * d * a) % c;
    a = 1LL * a * a % c;
    b >>= 1;
  }
  return d;
}
inline void read(int &x) {
  char ch;
  while ((ch = getchar()) <= 32)
    ;
  x = ch - 48;
  while ((ch = getchar()) > 32)
    x = x * 10 + ch - 48;
}
inline int random(int n) { return 1LL * rand() * rand() % n + 1; }
//-------------template------------
const int N = 100010, M = 600000, p = 1000000007, inf = 100000000;
const int inv2 = (p + 1) / 2;
inline void up(int &x, int y) {
  x += y;
  if (x >= p)
    x -= p;
  if (x < 0)
    x += p;
}
struct Edge {
  int y, ne;
};
int n;
int f[N];
// void add(int x,int y) { e[++num]=(Edge){y,last[x]};last[x]=num;}
int main() {
  scanf("%d", &n);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x > 1 && f[x - 1])
      f[x] = f[x - 1] + 1;
    else
      f[x] = 1;
    ans = max(ans, f[x]);
  }
  printf("%d\n", n - ans);
  return 0;
}


Fixed submission:
/*
 * =template=.cpp
 * Copyright (C) 2017 hzw <hzw@huzhenweideMacBook.local>
 *
 * Distributed under terms of the MIT license.
 */

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#define FR first
#define SC second
#define MP make_pair
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
inline int pow(int a, int b, int c) {
  if (a == 0)
    return 0;
  b = (b % (c - 1) + c - 1) % (c - 1);
  int d = 1;
  while (b) {
    if (b & 1)
      d = (1LL * d * a) % c;
    a = 1LL * a * a % c;
    b >>= 1;
  }
  return d;
}
inline void read(int &x) {
  char ch;
  while ((ch = getchar()) <= 32)
    ;
  x = ch - 48;
  while ((ch = getchar()) > 32)
    x = x * 10 + ch - 48;
}
inline int random(int n) { return 1LL * rand() * rand() % n + 1; }
//-------------template------------
const int N = 200010, M = 600000, p = 1000000007, inf = 100000000;
const int inv2 = (p + 1) / 2;
inline void up(int &x, int y) {
  x += y;
  if (x >= p)
    x -= p;
  if (x < 0)
    x += p;
}
struct Edge {
  int y, ne;
};
int n;
int f[N];
// void add(int x,int y) { e[++num]=(Edge){y,last[x]};last[x]=num;}
int main() {
  scanf("%d", &n);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x > 1 && f[x - 1])
      f[x] = f[x - 1] + 1;
    else
      f[x] = 1;
    ans = max(ans, f[x]);
  }
  printf("%d\n", n - ans);
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v[x - 1] = i;
  }
  int res = n - 1;
  int l = 1;
  for (int i = n - 2; i >= 0; i++) {
    if (v[i] < v[i + 1]) {
      l++;
      res = min(res, n - l);
    } else
      l = 1;
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v[x - 1] = i;
  }
  int res = n - 1;
  int l = 1;
  for (int i = n - 2; i >= 0; i--) {
    if (v[i] < v[i + 1]) {
      l++;
      res = min(res, n - l);
    } else
      l = 1;
  }
  cout << res << endl;
}
Problem p03346 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = int64_t;
using ull = uint64_t;

constexpr ll INF = 1000000000; /* 1e+9a */

void solve(istream &cin) {
  ll N;
  cin >> N;
  if (!cin)
    return;
  vector<ll> A, X(N + 1, 0);
  for (int i = 0; i < N; ++i) {
    ll ta;
    cin >> ta;
    A.push_back(ta);
    X[ta] = ta;
    if (X[ta - 1] != 0) {
      X[ta - 1] = ta;
    }
  } // end i
  ll ans = INF;
  for (int i = 1; i <= N; ++i) {
    ans = min(
        ans, N - [&]() {
          int k = i;
          while (X[k] != k) {
            k++;
          }
          return k - i + 1;
        }());
  } // end i
  cout << ans << endl;
}

int main(int argc, char *argv[]) {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << setprecision(16) << scientific;
#ifdef DEBUG
  cerr << "DEBUG MODE" << endl;
  string test_cases = "test.txt";
  auto fs = fstream(test_cases, fstream::in);
  while (fs) {
    solve(fs);
  }
#else
  solve(cin);
#endif
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = int64_t;
using ull = uint64_t;

constexpr ll INF = 1000000000; /* 1e+9a */

void solve(istream &cin) {
  ll N;
  cin >> N;
  if (!cin)
    return;
  vector<ll> A, X(N + 1, 0);
  for (int i = 0; i < N; ++i) {
    ll ta;
    cin >> ta;
    A.push_back(ta);
    X[ta] = ta;
    if (X[ta - 1] != 0) {
      X[ta - 1] = ta;
    }
  } // end i
  ll ans = INF;
  for (int i = 1; i <= N; ++i) {
    ans = min(
        ans, N - [&]() {
          int k = i;
          while (X[k] != k) {
            X[k] = k;
            k++;
          }
          return k - i + 1;
        }());
  } // end i
  cout << ans << endl;
}

int main(int argc, char *argv[]) {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << setprecision(16) << scientific;
#ifdef DEBUG
  cerr << "DEBUG MODE" << endl;
  string test_cases = "test.txt";
  auto fs = fstream(test_cases, fstream::in);
  while (fs) {
    solve(fs);
  }
#else
  solve(cin);
#endif
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll MOD = 1000000007;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];
  vector<int> B(N);
  REP(i, N) { B[A[i]] = B[A[i] - 1] + 1; }
  cout << N - *max_element(B.begin(), B.end()) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll MOD = 1000000007;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];
  vector<int> B(N + 10);
  REP(i, N) { B[A[i]] = B[A[i] - 1] + 1; }
  cout << N - *max_element(B.begin(), B.end()) << endl;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

const int maxn = 100000;

int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-') {
      f = -f;
    }
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

int n, p[maxn + 10], f[maxn + 10], ans;

int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    p[i] = read();
    f[p[i]] = f[p[i] - 1] + 1;
  }
  for (int i = 1; i <= n; ++i) {
    if (f[i] >= ans) {
      ans = f[i];
    }
  }
  printf("%d\n", n - ans);
  return 0;
}


Fixed submission:
#include <cstdio>

const int maxn = 200000;

int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-') {
      f = -f;
    }
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

int n, p[maxn + 10], f[maxn + 10], ans;

int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    p[i] = read();
    f[p[i]] = f[p[i] - 1] + 1;
  }
  for (int i = 1; i <= n; ++i) {
    if (f[i] >= ans) {
      ans = f[i];
    }
  }
  printf("%d\n", n - ans);
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int n, a[100000 + 5];
int main() {
  while (~scanf("%d", &n)) {
    memset(a, 0, sizeof(a));
    int x;
    for (int i = 0; i < n; i++) {
      scanf("%d", &x);
      a[x] = a[x - 1] + 1;
    }
    int _max = 0;
    for (int i = 1; i <= n; i++) {
      _max = max(_max, a[i]);
    }
    printf("%d\n", n - _max);
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int n, a[200000 + 5];
int main() {
  while (~scanf("%d", &n)) {
    memset(a, 0, sizeof(a));
    int x;
    for (int i = 0; i < n; i++) {
      scanf("%d", &x);
      a[x] = a[x - 1] + 1;
    }
    int _max = 0;
    for (int i = 1; i <= n; i++) {
      _max = max(_max, a[i]);
    }
    printf("%d\n", n - _max);
  }

  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <vector>
#define LL long long
#define ft first
#define sd second
#define mp(x, y) make_pair(x, y)
#define db double
// #define int LL
using namespace std;
const int N = 2e5 + 233;
// const int mod = ;
const int INF = numeric_limits<int>::max();

#define rep(i, x, y) for (int i = x; i <= y; ++i)
void read(int &x) {
  x = 0;
  char ch = getchar();
  int f = 1;
  while (!isdigit(ch))
    (ch == '-' ? f = -1 : 0), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}

void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
int n, p[N], nxt[N], vis[N], head[N];
int ans;
signed main() {
  read(n);
  rep(i, 1, n) read(p[i]);
  rep(i, 1, n) {
    vis[p[i]] = i;
    head[p[i]] = 1;
    if (vis[p[i] - 1])
      nxt[i] = vis[p[i] - 1], head[vis[p[i] - 1]] = 0;
  }
  rep(i, 1, n) if (head[i]) {
    int tmp = i, cnt = 1;
    while (nxt[tmp])
      tmp = nxt[tmp], ++cnt;
    ans = max(ans, cnt);
  }
  return printf("%d", n - ans), 0;
}

Fixed submission:
// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <vector>
#define LL long long
#define ft first
#define sd second
#define mp(x, y) make_pair(x, y)
#define db double
// #define int LL
using namespace std;
const int N = 2e5 + 233;
// const int mod = ;
const int INF = numeric_limits<int>::max();

#define rep(i, x, y) for (int i = x; i <= y; ++i)
void read(int &x) {
  x = 0;
  char ch = getchar();
  int f = 1;
  while (!isdigit(ch))
    (ch == '-' ? f = -1 : 0), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}

void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
int n, p[N], nxt[N], vis[N], head[N];
int ans;
signed main() {
  read(n);
  rep(i, 1, n) read(p[i]);
  rep(i, 1, n) {
    vis[p[i]] = i;
    head[i] = 1;
    if (vis[p[i] - 1])
      nxt[i] = vis[p[i] - 1], head[vis[p[i] - 1]] = 0;
  }
  rep(i, 1, n) if (head[i]) {
    int tmp = i, cnt = 1;
    while (nxt[tmp])
      tmp = nxt[tmp], ++cnt;
    ans = max(ans, cnt);
  }
  return printf("%d", n - ans), 0;
}
Problem p03346 (C++)

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

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

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

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

Buggy submission (status: Time Limit Exceeded):
import sys

N = int(sys.stdin.readline())
P = [int(sys.stdin.readline()) for _ in range(N)]
Q = [P.index(i) for i in range(1, N + 1)]
ans = 0
prv_q = 0
now = 0
for i in range(N):
    if prv_q > Q[i]:
        now = 0
    now += 1
    prv_q = Q[i]
    ans = max(ans, now)
print(N - ans)


Fixed submission:
import sys

N = int(sys.stdin.readline())
P = {int(sys.stdin.readline()): i for i in range(N)}
Q = [P[i] for i in range(1, N + 1)]

ans = 0
prv_q = 0
now = 0
for i in range(N):
    if prv_q > Q[i]:
        now = 0
    now += 1
    prv_q = Q[i]
    ans = max(ans, now)
print(N - ans)
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> pos(n);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    pos[x] = i;
  }
  int ans = 1, cnt = 1;
  for (int i = 0; i + 1 < n; i++) {
    if (pos[i] < pos[i + 1])
      cnt++;
    else
      cnt = 1;
    ans = max(ans, cnt);
  }
  cout << n - ans << endl;
  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> pos(n);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    x--;
    pos[x] = i;
  }
  int ans = 1, cnt = 1;
  for (int i = 0; i + 1 < n; i++) {
    if (pos[i] < pos[i + 1])
      cnt++;
    else
      cnt = 1;
    ans = max(ans, cnt);
  }
  cout << n - ans << endl;
  return 0;
}
Problem p03346 (C++)

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

long long n, a[20005], dp[20005], maks;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    dp[a[i]] = dp[a[i] - 1] + 1;
    maks = max(maks, dp[a[i]]);
  }
  cout << n - maks << '\n';
}

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

long long n, a[200005], dp[200005], maks;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    dp[a[i]] = dp[a[i] - 1] + 1;
    maks = max(maks, dp[a[i]]);
  }
  cout << n - maks << '\n';
}
Problem p03346 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> p(n), q(n);
  rep(i, n) {
    cin >> p[i];
    q[p[i]] = i;
  }

  int maxv = 1, t = 1;
  for (int i = 1; i < n; i++) {
    if (q[i - 1] < q[i])
      t++;
    else {
      maxv = max(maxv, t);
      t = 1;
    }
  }
  maxv = max(maxv, t);

  cout << n - maxv << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

int main() {
  int n;
  cin >> n;
  vector<int> p(n), q(n);
  rep(i, n) {
    cin >> p[i];
    q[p[i] - 1] = i;
  }

  int maxv = 1, t = 1;
  for (int i = 1; i < n; i++) {
    if (q[i - 1] < q[i])
      t++;
    else {
      maxv = max(maxv, t);
      t = 1;
    }
  }
  maxv = max(maxv, t);

  cout << n - maxv << endl;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

int main() {
  int n, a[SIZE], pos[SIZE];

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    a[i]--;
    pos[a[i]] = i;
  }

  int ans = n - 1;

  int count = 1;
  for (int i = 1; i < n; i++) {
    if (pos[i - 1] < pos[i])
      count++;
    else
      count = 1;

    ans = min(ans, n - count);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 200010

int main() {
  int n, a[SIZE], pos[SIZE];

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    a[i]--;
    pos[a[i]] = i;
  }

  int ans = n - 1;

  int count = 1;
  for (int i = 1; i < n; i++) {
    if (pos[i - 1] < pos[i])
      count++;
    else
      count = 1;

    ans = min(ans, n - count);
  }

  cout << ans << endl;

  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;
ll mod = 1e9 + 7;

int main(void) {
  ll N, P[200010], d[200010] = {}, inv[200010], ans = 0;
  scanf("%lld", &N);
  reg(i, 1, N) {
    scanf("%lld", &P[i]);
    inv[P[i]] = i;
  }
  reg(i, 1, N) {
    if (d[i] == 0) { // まだきたことがなければ
      ll cnt = 1, p = i;
      d[p] = 1;
      while (1) {
        // printf("%d: %d\n",i,p);
        if (inv[P[p] + 1] > p) { // １つ後の要素が右側にあれば
          p = inv[P[p] + 1];
          d[p] = 1;
          cnt++;
        } else {
          break;
        }
      }
      if (ans < cnt)
        ans = cnt;
    }
  }
  printf("%lld\n", N - ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;
ll mod = 1e9 + 7;

int main(void) {
  ll N, P[200010], d[200010] = {}, inv[200010], ans = 0;
  scanf("%lld", &N);
  reg(i, 1, N) {
    scanf("%lld", &P[i]);
    inv[P[i]] = i;
  }
  reg(i, 1, N) {
    if (d[i] == 0) { // まだきたことがなければ
      ll cnt = 1, p = i;
      d[p] = 1;
      while (1) {
        // printf("%d: %d\n",i,p);
        if (P[p] < N && inv[P[p] + 1] > p) { // １つ後の要素が右側にあれば
          p = inv[P[p] + 1];
          d[p] = 1;
          cnt++;
        } else {
          break;
        }
      }
      if (ans < cnt)
        ans = cnt;
    }
  }
  printf("%lld\n", N - ans);
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, n) for (long long int(i) = 0; (i) < (n); ++(i))
#define ref(i, a, b) for (long long int(i) = (a); (i) <= (b); ++(i))
#define INF 0xFFFFFF
#define endl '\n'
#define pb push_back
#define mp make_pair
const unsigned int mod = 1e9 + 7;
const int mx = 100005;
int n;
int a[mx], pos[mx];
int main() {
  /*freopen("input.in", "r", stdin);
  freopen("output.out", "w", stdout);*/
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  // rep(i,mx)rep(j,mx)dp[i][j]=-1;
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, n) {
    cin >> a[i];
    pos[a[i]] = i;
  }
  int res = 1, ans = n;
  for (int i = 2; i <= n; ++i) {
    if (pos[i] > pos[i - 1])
      ++res;
    else
      res = 1;
    ans = min(ans, n - res);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, n) for (long long int(i) = 0; (i) < (n); ++(i))
#define ref(i, a, b) for (long long int(i) = (a); (i) <= (b); ++(i))
#define INF 0xFFFFFF
#define endl '\n'
#define pb push_back
#define mp make_pair
const unsigned int mod = 1e9 + 7;
const int mx = 2 * 100005;
int n;
int a[mx], pos[mx];
int main() {
  /*freopen("input.in", "r", stdin);
  freopen("output.out", "w", stdout);*/
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  // rep(i,mx)rep(j,mx)dp[i][j]=-1;
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, n) {
    cin >> a[i];
    pos[a[i]] = i;
  }
  int res = 1, ans = n;
  for (int i = 2; i <= n; ++i) {
    if (pos[i] > pos[i - 1])
      ++res;
    else
      res = 1;
    ans = min(ans, n - res);
  }
  cout << ans << endl;
}
Problem p03346 (C++)

Buggy submission (status: Time Limit Exceeded):


#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

// #define FAST_ALLOCATOR_MEMORY 2e8
#undef LOCAL
// #include "optimization.h"

int main() {
  iostream::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<int> p(n);
  for (int i = 0; i < n; i++)
    cin >> p[i];
  vector<int> pos(n);
  for (int i = 0; i < n; i++) {
    p[i]--;
    pos[p[i]] = i;
  }
  int mx = 0;
  for (int i = 0; i < n; i++) {
    int r = i;
    while (r + 1 < n && pos[r + 1] > pos[r])
      r++;
    mx = max(mx, r - i + 1);
  }
  cout << n - mx;
  return 0;
}

Fixed submission:


#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

// #define FAST_ALLOCATOR_MEMORY 2e8
#undef LOCAL
// #include "optimization.h"

int main() {
  iostream::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<int> p(n);
  for (int i = 0; i < n; i++)
    cin >> p[i];
  vector<int> pos(n);
  for (int i = 0; i < n; i++) {
    p[i]--;
    pos[p[i]] = i;
  }
  int mx = 0;
  for (int i = 0; i < n; i++) {
    int r = i;
    while (r + 1 < n && pos[r + 1] > pos[r])
      r++;
    mx = max(mx, r - i + 1);
    i = r;
  }
  cout << n - mx;
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100000
#define rint register int
inline int read() {
  int r;
  int s = 0, c;
  for (; !isdigit(c = getchar()); s = c)
    ;
  for (r = c ^ 48; isdigit(c = getchar()); (r *= 10) += c ^ 48)
    ;
  return s ^ 45 ? r : -r;
}
int P[MAXN + 5], n, Ans;
int main() {
  n = read();
  for (rint i = 1; i <= n; P[read()] = i, i++)
    ;
  for (rint i = 1, j; i <= n; Ans = max(Ans, j - i + 1), i = j + 1)
    for (j = i; j < n && P[j + 1] > P[j]; j++)
      ;
  printf("%d\n", n - Ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAXN 200000
#define rint register int
inline int read() {
  int r;
  int s = 0, c;
  for (; !isdigit(c = getchar()); s = c)
    ;
  for (r = c ^ 48; isdigit(c = getchar()); (r *= 10) += c ^ 48)
    ;
  return s ^ 45 ? r : -r;
}
int P[MAXN + 5], n, Ans;
int main() {
  n = read();
  for (rint i = 1; i <= n; P[read()] = i, i++)
    ;
  for (rint i = 1, j; i <= n; Ans = max(Ans, j - i + 1), i = j + 1)
    for (j = i; j < n && P[j + 1] > P[j]; j++)
      ;
  printf("%d\n", n - Ans);
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF (1 << 30) - 1
#define LINF 1LL << 60
const int MOD = 1000000007;
using ll = long long;
using P = pair<int, int>;

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

  vector<int> dp(n, 0);
  int seq = 0;
  for (int i = 0; i < n; ++i) {
    dp[p[i]] = dp[p[i] - 1] + 1;
    seq = max(seq, dp[p[i]]);
  }

  cout << n - seq << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF (1 << 30) - 1
#define LINF 1LL << 60
const int MOD = 1000000007;
using ll = long long;
using P = pair<int, int>;

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

  vector<int> dp(n + 1, 0);
  int seq = 0;
  for (int i = 0; i < n; ++i) {
    dp[p[i]] = dp[p[i] - 1] + 1;
    seq = max(seq, dp[p[i]]);
  }

  cout << n - seq << endl;

  return 0;
}
Problem p03346 (C++)

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

using namespace std;
#define BUF (1000)
#define ll long long

int n;
int p[BUF];
int b[BUF];

int main(void) {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
    b[p[i]] = i;
  }

  int cnt = 0;
  int max_cnt = 0;
  for (int i = 1; i < n; i++) {
    if (b[i] < b[i + 1]) {
      cnt++;
    } else
      cnt = 0;
    max_cnt = max(cnt, max_cnt);
  }

  cout << n - max_cnt - 1 << endl;

  return 0;
}


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

using namespace std;
#define BUF (200005)
#define ll long long

int n;
int p[BUF];
int b[BUF];

int main(void) {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
    b[p[i]] = i;
  }

  int cnt = 0;
  int max_cnt = 0;
  for (int i = 1; i < n; i++) {
    if (b[i] < b[i + 1]) {
      cnt++;
    } else
      cnt = 0;
    max_cnt = max(cnt, max_cnt);
  }

  cout << n - max_cnt - 1 << endl;

  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;
#define INF 1000000
typedef long long int ll;
typedef pair<ll, ll> mypair;
int main() {
  ll n;
  cin >> n;
  vector<ll> p(n + 1, INF);
  vector<mypair> inc(n + 1);
  for (ll i = 1; i <= n; i++) {
    cin >> p[i];
    inc[i] = mypair(0, i);
    //		dec[i]=mypair(0,n+1);
  }
  //	cout<<"ok"<<endl;
  ll t = 0;
  ll ans = 0;
  ll now = n;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      if (p[i] == inc[j].second) {
        inc[j].second++;
        inc[j].first++;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    ans = max(ans, inc[i].first);
    //	cout<<inc[i].first<<endl;
  }
  /*for(ll i=1;i<=n;i++){
          if(p[i]==now){
          now++;
          t++;
          }
  }*/
  //	ans=max(t,ans);
  ans = n - ans;
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;
#define INF 1000000
typedef long long int ll;
typedef pair<ll, ll> mypair;
int main() {
  ll n;
  cin >> n;
  vector<ll> p(n + 1, INF);
  vector<mypair> inc(n + 1);
  for (ll i = 1; i <= n; i++) {
    cin >> p[i];
    inc[i] = mypair(0, i);
    //		dec[i]=mypair(0,n+1);
  }
  //	cout<<"ok"<<endl;
  ll t = 0;
  ll ans = 0;
  ll now = n;
  for (ll i = 1; i <= n; i++) {
    /*	for(ll j=1;j<=n;j++){
                    if(p[i]==inc[j].second){
                            inc[j].second++;
                            inc[j].first++;
                    }
            }*/
    inc[p[i]].first = inc[p[i] - 1].first + 1;
  }
  for (int i = 1; i <= n; i++) {
    ans = max(ans, inc[i].first);
    //	cout<<inc[i].first<<endl;
  }
  /*for(ll i=1;i<=n;i++){
          if(p[i]==now){
          now++;
          t++;
          }
  }*/
  //	ans=max(t,ans);
  ans = n - ans;
  cout << ans << endl;

  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
const LL maxn = 50005;
LL d[maxn];
int main() {
  LL n, x, cur = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    d[x] = d[x - 1] + 1;
    cur = max(cur, d[x]);
  }
  cout << n - cur << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
const LL maxn = 500050;
LL d[maxn];
int main() {
  LL n, x, cur = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    d[x] = d[x - 1] + 1;
    cur = max(cur, d[x]);
  }
  cout << n - cur << endl;
  return 0;
}
Problem p03346 (C++)

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

int n, ans, a[50005], f[50005];
bool vis[50005];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    f[i] = 1;
  }
  for (int i = 1; i <= n; i++) {
    vis[a[i]] = true;
    if (vis[a[i] - 1]) {
      f[a[i]] = f[a[i] - 1] + 1;
    }
    if (f[a[i]] > ans)
      ans = f[a[i]];
  }
  cout << n - ans << '\n';
  return 0;
}

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

int n, ans, a[200005], f[200005];
bool vis[200005];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    f[i] = 1;
  }
  for (int i = 1; i <= n; i++) {
    vis[a[i]] = true;
    if (vis[a[i] - 1]) {
      f[a[i]] = f[a[i] - 1] + 1;
    }
    if (f[a[i]] > ans)
      ans = f[a[i]];
  }
  cout << n - ans << '\n';
  return 0;
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int x, ans, f[100010], n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    f[x] = f[x - 1] + 1;
    ans = max(ans, f[x]);
  }
  printf("%d\n", n - ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int x, ans, f[200010], n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    f[x] = f[x - 1] + 1;
    ans = max(ans, f[x]);
  }
  printf("%d\n", n - ans);
}
Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int v[100005];
bool f[100005];
int main() {
  int n, x;
  scanf("%d", &n);
  int ss = 0;
  memset(v, 0, sizeof(v));
  memset(f, false, sizeof(f));
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    if (f[x - 1] == true) {
      v[x] = v[x - 1] + 1;
      f[x] = true;
      ss = max(ss, v[x]);
    } else {
      v[x] = 1;
      f[x] = true;
      ss = max(ss, v[x]);
    }
  }
  printf("%d\n", n - ss);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int v[200005];
bool f[200005];
int main() {
  int n, x;
  scanf("%d", &n);
  int ss = 0;
  memset(v, 0, sizeof(v));
  memset(f, false, sizeof(f));
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    if (f[x - 1] == true) {
      v[x] = v[x - 1] + 1;
      f[x] = true;
      ss = max(ss, v[x]);
    } else {
      v[x] = 1;
      f[x] = true;
      ss = max(ss, v[x]);
    }
  }
  printf("%d\n", n - ss);
  return 0;
}