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

#include <algorithm>
#include <iostream>
#include <vector>
#pragma warning(disable : 4996)
#define mod 1000000007
using namespace std;
int N, Q, s, t, z[100001], fact[100001];
vector<int> seg[262144];
int query(int a, int b, int x1, int x2, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return lower_bound(seg[k].begin(), seg[k].end(), x2) -
           lower_bound(seg[k].begin(), seg[k].end(), x1);
  int lc = query(a, b, x1, x2, 2 * k, l, (l + r) / 2);
  int rc = query(a, b, x1, x2, 2 * k + 1, (l + r) / 2, r);
  return lc + rc;
}
int main() {
  fact[0] = 1;
  for (int i = 1; i < 100000; i++)
    fact[i] = 1LL * fact[i - 1] * i % mod;
  while (cin >> N >> Q) {
    for (int i = 0; i < N; i++)
      z[i] = i;
    for (int i = 0; i < Q; i++) {
      scanf("%d%d", &s, &t);
      swap(z[s - 1], z[t - 1]);
    }
    for (int i = 0; i < 262144; i++)
      seg[i].clear();
    for (int i = 0; i < N; i++) {
      int u = i + 131072;
      while (u > 0) {
        seg[u].push_back(z[i]);
        u >>= 1;
      }
    }
    for (int i = 0; i < 131072; i++)
      sort(seg[i].begin(), seg[i].end());
    int ret = 0;
    for (int i = N - 1; i >= 0; i--)
      ret = (ret +
             1LL * query(i + 1, N, 0, z[i], 1, 0, 131072) * fact[N - i - 1]) %
            mod;
    printf("%d\\n", ret);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define mod 1000000007
using namespace std;
int N, Q, s, t, a[100001], fact[100001];
int main() {
  fact[0] = 1;
  for (int i = 1; i < 100000; i++)
    fact[i] = 1LL * fact[i - 1] * i % mod;
  while (cin >> N >> Q) {
    for (int i = 0; i < N; i++)
      a[i] = i;
    for (int i = 0; i < Q; i++) {
      scanf("%d%d", &s, &t);
      swap(a[s - 1], a[t - 1]);
    }
    int ret = 0;
    for (int i = N - 1; i >= 0; i--) {
      int res = 0;
      for (int j = i + 1; j < N; j++) {
        if (a[i] > a[j])
          res++;
      }
      ret = (ret + 1LL * res * fact[N - i - 1]) % mod;
    }
    printf("%d\\n", ret);
  }
}
Problem p00266 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define MP make_pair
#define all(s) (s).begin(), (s).end()
#define rev(s) (s).rbegin(), (s).rend()
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

const int DIV = 1000000007;
ll f[100001];

int main() {
  // freopen("test.txt", "r", stdin);
  f[0] = 1;
  for (ll i = 1; i < 100001; i++) {
    f[i] = i * (f[i - 1]) % DIV;
  }

  int N;
  while (cin >> N, N) {
    int R;
    cin >> R;
    vector<int> v(N);
    rep(i, N) v[i] = i;

    rep(i, R) {
      int s, t;
      cin >> s >> t;
      swap(v[s - 1], v[t - 1]);
    }

    vector<int> checked;

    ll ans = 0;
    rep(i, N) {
      vector<int>::iterator ins = lower_bound(all(checked), v[i]);
      ll cnt = ins - checked.begin();

      ans = (ans + (v[i] - cnt) * f[N - 1 - i]) % DIV;
      checked.insert(ins, v[i]);
    }
    cout << ans % DIV << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define MP make_pair
#define all(s) (s).begin(), (s).end()
#define rev(s) (s).rbegin(), (s).rend()
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

const int DIV = 1000000007;
int f[100001];

int main() {
  // freopen("test.txt", "r", stdin);
  f[0] = 1;
  for (ll i = 1; i < 100001; i++) {
    f[i] = i * (f[i - 1]) % DIV;
  }

  int N;
  while (cin >> N, N) {
    int R;
    cin >> R;
    vector<int> v(N);
    rep(i, N) v[i] = i;

    rep(i, R) {
      int s, t;
      cin >> s >> t;
      swap(v[s - 1], v[t - 1]);
    }

    set<int> checked;

    int ans = 0;
    rep(i, N) {
      int cnt = 0;

      cnt = distance(checked.begin(), lower_bound(all(checked), v[i]));

      ans += (v[i] - cnt) * f[N - 1 - i] % DIV;
      checked.insert(v[i]);
    }
    cout << ans % DIV << endl;
  }

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

#include <bits/stdc++.h>
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int a[100000];
ll fact[100000];
ll bit[100001];
int n;

void add(int i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}
int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}
int main() {
  while (scanf("%d", &n), n) {
    int r;
    scanf("%d", &r);
    rep(i, n) {
      a[i] = i;
      add(i + 1, 1);
    }
    rep(i, r) {
      int s, t;
      scanf("%d%d", &s, &t);
      s--;
      t--;
      swap(a[s], a[t]);
    }
    fact[0] = 1;
    for (int i = 1; i < n; i++)
      fact[i] = (fact[i - 1] * i) % MOD;
    ll cnt = 0;
    rep(i, n) {
      int d = sum(a[i]);
      cnt = (cnt + fact[n - i - 1] * d) % MOD;
      add(a[i] + 1, -1);
    }
    printf("%lld\\n", cnt);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int a[100000];
ll fact[100000];
int main() {
  int n;
  while (scanf("%d", &n), n) {
    int r;
    scanf("%d", &r);
    rep(i, n) a[i] = i;
    vector<int> v(a, a + n);
    rep(i, r) {
      int s, t;
      scanf("%d%d", &s, &t);
      s--;
      t--;
      swap(a[s], a[t]);
    }
    fact[0] = 1;
    for (int i = 1; i < n; i++)
      fact[i] = (fact[i - 1] * i) % MOD;
    ll cnt = 0;
    rep(i, n) {
      auto it = lower_bound(v.begin(), v.end(), a[i]);
      int d = it - v.begin();
      cnt = (cnt + fact[n - i - 1] * d) % MOD;
      v.erase(it);
    }
    printf("%lld\\n", cnt);
  }
}
Problem p00266 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const long long int M = 1000000007;
const long long int NIL = -1;

vector<long long int> memo;

long long int functional(long long int n) {
  if (n <= 1)
    return 1;
  if (memo[n] != NIL)
    return memo[n];
  return memo[n] = (n * functional(n - 1)) % M;
}

class SlidingSum {
  int size;
  std::vector<int> node;

public:
  SlidingSum(int s) : size(s), node(std::vector<int>(s + 1, 0)) {}
  long long int sum(int index) const {
    int result = 0;
    for (int i = index + 1; i > 0; i -= i & -i)
      result += node[i];
    return result;
  }
  long long int sum(int left, int right) const {
    return sum(right) - sum(left - 1);
  }
  void update(int index, int value) {
    for (int i = index + 1; i <= size; i += i & -i)
      node[i] += value;
  }
};

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> word(n);
    for (int i = 0; i < n; ++i)
      word[i] = i;

    int r;
    for (cin >> r; r > 0; --r) {
      int s, t;
      cin >> s >> t;
      swap(word[s - 1], word[t - 1]);
    }

    long long int answer = 0;
    memo = vector<long long int>(n, NIL);
    SlidingSum ss(n);
    for (int i = 0; i < n - 1; ++i) {
      answer += functional(n - i - 1) * (word[i] - ss.sum(0, word[i]));
      answer %= M;
      ss.update(word[i], 1);
    }
    cout << answer << endl;
  }
}

Error: TLE

Fixed submission:

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

const long long int M = 1000000007;
const long long int NIL = -1;

vector<long long int> memo;

long long int functional(long long int n) {
  if (n <= 1)
    return 1;
  if (memo[n] != NIL)
    return memo[n];
  return memo[n] = ((n % M) * (functional(n - 1) % M)) % M;
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> original(n);
    for (int i = 0; i < n; ++i)
      original[i] = i;

    vector<int> word(original);
    int r;
    for (cin >> r; r > 0; --r) {
      int s, t;
      cin >> s >> t;
      swap(word[s - 1], word[t - 1]);
    }

    long long int answer = 0;
    memo = vector<long long int>(n, NIL);
    for (int i = 0; i < n - 1; ++i) {
      auto it = lower_bound(original.begin(), original.end(), word[i]);
      answer += functional(n - i - 1) * (it - original.begin());
      answer %= M;
      original.erase(it);
    }
    cout << answer << endl;
  }
}
Problem p00266 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
int main(void) {
  using namespace std;
  int i, j;
  long long int fact[100000] = {0};
  for (i = 0; i < 100000; i++) {
    if (i == 0)
      fact[i] = 1;
    else
      fact[i] = (fact[i - 1] * i) % 1000000007;
  }
  long int N, K;
  cin >> N;
  while (N != 0) {
    cin >> K;
    long int a, b;
    long int value = 0;
    vector<long int> data;
    vector<long int> index;
    for (i = 0; i < N; i++) {
      data.push_back(i);
      index.push_back(i);
    }
    for (i = 0; i < K; i++) {
      cin >> a >> b;
      a = a - 1;
      b = b - 1;
      if (a + 1 != b) {
        for (j = a + 1; j < b; j++) {
          if (data[j] < data[a] && data[b] < data[j]) {
            index[j] += 1;
            index[a] += 1;
          } else if (data[a] < data[j] && data[j] < data[b]) {
            index[j] -= 1;
            index[b] -= 1;
          } else if (data[j] < data[a] && data[j] < data[b]) {
            index[a] += 1;
            index[b] -= 1;
          }
        }
      }
      if (data[a] > data[b]) {
        index[a] += 1;
      } else if (data[a] < data[b]) {
        index[b] -= 1;
      }
      swap(index[a], index[b]);
      swap(data[a], data[b]);
    }
    for (i = 0; i < N; i++) {
      value += (data[i] - index[i]) * fact[N - 1 - i] % 1000000007;
    }
    value = value % 1000000007;
    cout << value << endl;
    cin >> N;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  clock_t startTime, endTime;
  using namespace std;
  int i, j;
  // 階乗の計算
  long long int fact[100000] = {0};
  for (i = 0; i < 100000; i++) {
    if (i == 0)
      fact[i] = 1;
    else
      fact[i] = (fact[i - 1] * i) % 1000000007;
  }

  long int N, K;
  cin >> N;
  while (N != 0) {
    cin >> K;
    long int a, b;
    long int k = 0, value = 0, index = -1;
    vector<long int> data;
    vector<long int> data2;
    vector<long int>::iterator itr;
    // 入れ替え前
    for (i = 0; i < N; i++) {
      data.push_back(i);
      data2.push_back(i);
    }

    // 入れ替え
    for (i = 0; i < K; i++) {
      cin >> a >> b;
      swap(data[a - 1], data[b - 1]);
    }

    // 探索
    for (i = 0; i < N; i++) {
      // data[i]のインデックス取得
      itr = find(data2.begin(), data2.end(), data[i]);
      index = itr - data2.begin();
      k = data[i] - index;
      value += (data[i] - k) * fact[N - 1 - i];
      // value = value % 1000000007;
      data2.erase(itr);
    }
    cout << value % 1000000007 << endl;
    cin >> N;
  }
  return 0;
}
Problem p00267 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
#pragma warning(disable : 4996)

int N, A[1 << 18], B[1 << 18], C[1 << 18];

void init() {
  N = 0;
  for (int i = 0; i < (1 << 18); i++) {
    A[i] = 0;
    B[i] = 0;
    C[i] = 0;
  }
}
int solve() {
  sort(A, A + N);
  reverse(A, A + N);
  sort(B, B + N);

  for (int i = 0; i < N; i++) {
    int pos1 = lower_bound(B, B + N, A[i] + 1) - B;
    C[i] = (N - pos1) - i;
  }
  int maxn = 0, ret = (1 << 30);
  for (int i = 0; i < N - 1; i++) {
    maxn = max(maxn, C[i / 2]);
    if (maxn > (i + 1) / 2) {
      ret = min(ret, i + 1);
      break;
    }
  }
  return ret;
}

int main() {
  while (true) {
    init();
    cin >> N;
    if (N == 0)
      break;
    for (int i = 0; i < N; i++)
      scanf("%d", &B[i]);
    for (int i = 0; i < N; i++)
      scanf("%d", &A[i]);
    int ans = solve();
    if (ans == (1 << 30))
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, a[100000], b[100000];
bool simulate(int p) {
  int kh = (p + 2) / 2, cnt1 = 0, K = 0;
  int num = 0;
  for (int i = n - p; i < n; i++) {
    while (K < n && a[i] > b[K])
      K++;
    if (K >= n) {
      cnt1 = p - num;
      break;
    }
    K++;
    num++;
  }
  if (cnt1 < kh)
    return false;
  return true;
}
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    for (int i = 0; i < n; i++)
      cin >> b[i];
    sort(a, a + n);
    sort(b, b + n);
    int cnt1 = 0, cnt2 = 0;
    bool flag = false;
    for (int i = 1; i < n; i++) {
      if (simulate(i) == true) {
        cout << i << endl;
        flag = true;
        break;
      }
    }
    if (flag == false)
      cout << "NA" << endl;
  }
  return 0;
}
Problem p00267 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> a(n), b(n);
    for (auto &i : a)
      cin >> i;
    for (auto &i : b)
      cin >> i;
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    //         for(auto& i: a) cout<<" "<<i;cout<<endl;
    //         for(auto& i: b) cout<<" "<<i;cout<<endl;

    int answer = -1;
    for (int i = 1; i < n; ++i) {
      int lowest = a[n - i];
      auto it = upper_bound(b.end() - i, b.end(), lowest - 1);
      int d = it - b.begin() - (n - i);
      //             cout << d << endl;
      //             cout << n-i<<endl;
      //             cout<<"+"<<*it<<endl;
      if (it - (b.end() - i) > i / 2) {
        answer = i;
        break;
      }
    }
    if (answer == -1)
      cout << "NA" << endl;
    else
      cout << answer << endl;
  }
}

Error: TLE

Fixed submission:

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

int win(vector<int> a, vector<int> b) {
  int win = 0;
  for (const auto &i : a) {
    auto it = upper_bound(b.begin(), b.end(), i);
    if (it == b.end()) {
      if (b.back() == i) {
        b.pop_back();
      } else {
        b.erase(b.begin());
        ++win;
      }
    } else {
      b.erase(it);
    }
  }
  return win;
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> a(n), b(n);
    for (auto &i : a)
      cin >> i;
    for (auto &i : b)
      cin >> i;
    sort(a.begin(), a.end(), greater<int>());
    sort(b.begin(), b.end());

    int answer = -1;
    for (int i = 1; i < n; ++i) {
      int w = win(vector<int>(a.begin(), a.begin() + i),
                  vector<int>(b.end() - i, b.end()));
      if (w > i / 2) {
        answer = i;
        break;
      }
      i += i / 2 - w;
    }
    if (answer == -1)
      cout << "NA" << endl;
    else
      cout << answer << endl;
  }
}
Problem p00267 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;

int N;
int a[40005];
int b[40005];

bool solve() {
  scanf("%d", &N);
  if (N == 0) {
    return false;
  }

  for (int j = 0; j < N; ++j) {
    scanf("%d", a + j);
  }
  for (int j = 0; j < N; ++j) {
    scanf("%d", b + j);
  }

  sort(a, a + N, greater<int>());
  sort(b, b + N, greater<int>());

  int wa = 0, wb = 0;

  for (int ia = 0, ib = 0; ia < N - 1;) {
    if (a[ia] > b[ib]) {
      ++wa;
      ++ia;
    } else {
      ++wb;
      ++ia;
      ++ib;
    }

    if (wa > wb) {
      printf("%d\\n", ia);
      return true;
    }
  }

  puts("NA");
  return true;
}

int main() {
  while (solve())
    ;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;

int N;
int a[40005];
set<int> b;

bool solve() {
  b.clear();
  scanf("%d", &N);
  if (N == 0) {
    return false;
  }

  for (int j = 0; j < N; ++j) {
    scanf("%d", a + j);
  }

  for (int j = 0; j < N; ++j) {
    int t;
    scanf("%d", &t);
    b.insert(t);
  }

  sort(a, a + N, greater<int>());

  int wa = 0, wb = 0;

  for (int j = 0; j < N - 1; ++j) {
    auto it = b.lower_bound(a[j]);
    if (it == end(b)) {
      ++wa;
      it = b.begin();
    } else {
      if (*it > a[j]) {
        ++wb;
      }
    }
    b.erase(it);

    if (wa > wb) {
      cout << (j + 1) << endl;
      return true;
    }
  }

  puts("NA");
  return true;
}

int main() {
  while (solve())
    ;
  return 0;
}
Problem p00267 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pint;
typedef vector<int> vint;
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T, class U> void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

int N;
int a[40000], b[40000];
int cnt[40000];
int main() {
  while (scanf("%d", &N), N) {
    rep(i, N) scanf("%d", &a[i]);
    rep(i, N) scanf("%d", &b[i]);
    sort(a, a + N);
    reverse(a, a + N);
    sort(b, b + N);
    reverse(b, b + N);

    memset(cnt, 0, sizeof(cnt));
    int cur = 0, sum = 0;
    rep(i, N - 1) {
      while (cur < N && a[cur] > b[i])
        cur++;

      if (cur != N)
        cnt[cur++] = 1;
      sum += cnt[i];
      if (i + 1 - sum > sum) {
        cout << i + 1 << endl;
        goto END;
      }
    }
    cout << "NA" << endl;

  END:;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pint;
typedef vector<int> vint;
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T, class U> void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

int N;
int a[40000], b[40000];

int main() {
  while (scanf("%d", &N), N) {
    rep(i, N) scanf("%d", &a[i]);
    rep(i, N) scanf("%d", &b[i]);
    sort(a, a + N);
    reverse(a, a + N);
    sort(b, b + N);
    reverse(b, b + N);

    rep(i, N - 1) {
      int idx = i, cnt = 0;
      for (int j = i; j >= 0; j--) {
        while (idx >= 0 && b[j] >= a[idx])
          idx--;
        if (i - idx - cnt)
          cnt++;
      }
      if ((i + 1) / 2 < i + 1 - cnt) {
        cout << i + 1 << endl;
        goto END;
      }
    }
    cout << "NA" << endl;

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

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define IINF (INT_MAX)
#define MAX_N 40001

using namespace std;

int N, a[MAX_N], b[MAX_N];

int main() {
  while (scanf("%d", &N), N) {
    rep(i, N) scanf("%d", a + i);
    rep(i, N) scanf("%d", b + i);
    sort(a, a + N, greater<int>());
    sort(b, b + N, greater<int>());
    bool won = false;
    int cnt = 0, cur = 0;
    rep(i, N - 1) {
      if (a[i] <= b[cur])
        cnt++, cur++;
      if ((i + 1) / 2.0 < (i + 1) - cnt) {
        won = true;
        cout << i + 1 << endl;
        break;
      }
    }
    if (!won)
      cout << "NA" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define IINF (INT_MAX)
#define MAX_N 40001

using namespace std;

int N, a[MAX_N], b[MAX_N];

inline bool check(int k) {
  int cnt = 0, cur = 0;
  rep(i, k) {
    if (a[i] <= b[cur])
      cnt++, cur++;
  }
  return (k / 2.0 < k - cnt);
}

int main() {
  while (scanf("%d", &N), N) {
    rep(i, N) scanf("%d", a + i);
    rep(i, N) scanf("%d", b + i);
    sort(a, a + N, greater<int>());
    sort(b, b + N, greater<int>());
    bool won = false;
    REP(k, 1, N) {
      if (check(k)) {
        won = true;
        cout << k << endl;
        break;
      }
    }
    if (!won)
      cout << "NA" << endl;
  }
  return 0;
}
Problem p00267 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N;
int main() {
  while (cin >> N && N) {
    vector<int> A(N);
    vector<int> B(N);
    for (int i = 0; i < N; i++)
      cin >> A[i];
    for (int i = 0; i < N; i++)
      cin >> B[i];
    sort(A.rbegin(), A.rend());
    sort(B.rbegin(), B.rend());

    int win = 0;
    int res = -1;
    priority_queue<int> q;
    for (int k = 0; k < N - 1; k++) {
      q.push(B[k]);
      if (A[k] <= q.top())
        q.pop();
      else
        win++;
      if ((k + 1) / 2 < win) {
        res = k + 1;
        break;
      }
    }
    if (res == -1)
      cout << "NA" << endl;
    else
      cout << res << endl;
  }
}

Error: TLE

Fixed submission:

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

const int MAX = 40000;

int N;
int A[MAX], B[MAX];

void input() {
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < N; i++)
    cin >> B[i];
}

void solve() {
  input();
  sort(A, A + N);
  sort(B, B + N);

  int win;
  int res = -1;

  for (int i = 0; i < N; i++) {
    win = 0;
    int s, pos;
    s = pos = N - 1 - i;
    for (int j = 0; j <= i; j++) {
      pos = lower_bound(B + pos, B + N, A[s + j]) - B;
      if (pos == N)
        break;
      win++;
      pos++;
    }
    if (((i + 1) - win) > (i + 1) / 2) {
      res = i + 1;
      break;
    }
  }

  if (res == -1 || res == N)
    cout << "NA" << endl;
  else
    cout << res << endl;
}

int main() {
  while (cin >> N, N)
    solve();
  return 0;
}
Problem p00267 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n;
int a[40000], b[40000];

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

    sort(a, a + n, greater<int>());
    sort(b, b + n, greater<int>());

    int i;
    for (i = 0; i < n / 2; i++) {
      if (a[2 * i] > b[i]) {
        cout << 2 * i + 1 << endl;
        break;
      }
    }
    if (i == n / 2)
      cout << "NA\\n";
  }
}

Error: TLE

Fixed submission:

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

int n;
int a[40000], b[40000];

bool check(int num) {
  int lose = 0;

  int pos = 0;
  for (int i = 0; i < num; i++) {
    while (pos < num && b[i] < a[pos])
      pos++;
    if (pos == num)
      break;
    lose++;
    if ((num - 1) / 2 < lose)
      break;
  }
  return (num - 1) / 2 >= lose;
}

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

    sort(a, a + n, greater<int>());
    sort(b, b + n, greater<int>());

    int i;
    for (i = 1; i < n; i++) {
      if (check(i)) {
        cout << i << endl;
        break;
      }
    }
    if (i == n)
      cout << "NA\\n";
  }
}
Problem p00271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
int main() {
  for (int a, b; scanf("%d%d", &a, &b) + 1;)
    printf("%d\\n", a - b);
}

Error: TLE

Fixed submission:

#include <cstdio>
int main() {
  for (int a, b; scanf("%d%d", &a, &b);)
    printf("%d\\n", a - b);
}