Problem p00487 (C++) — buggy submission, verdict: Time Limit Exceeded

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

struct BUG {
  LL out, in;

  bool operator<(const BUG &t) const { return out < t.out; }
};

bool cmp(const BUG &a, const BUG &b) { return a.in > b.in; };

int n;
BUG bug[300001];

bool solve(int x) {
  priority_queue<BUG> que;
  LL sum = 0;
  int i;
  for (i = 0; i < x; i++) {
    que.push(bug[i]);
    sum += bug[i].out;
  }

  for (i--; i < n; i++) {
    if (i >= x) {
      que.push(bug[i]);
      sum += bug[i].out;
      BUG b = que.top();
      que.pop();
      sum -= b.out;
    }
    if (sum <= bug[i].in * x) {
      return true;
    }
  }

  return false;
}

/*
bool solve(int x)
{
    int lim = x - 1;

    while (true){
        priority_queue<BUG> que;
        for (int i = 0; i <= lim; i++){
            que.push(bug[i]);
        }

        puts("");
        LL sum = 0;
        for (int i = 0; i < x; i++){
            BUG b = que.top(); que.pop();
                printf("%lld\\n", b.out);
            sum += b.out;
        }

        if (sum <= bug[lim].in * x) return true;
        else {
            lim++;
            if (lim == n) return false;
        }
    }
}
*/

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

  for (int i = 0; i < n; i++) {
    scanf("%lld %lld", &bug[i].out, &bug[i].in);
  }

  sort(bug, bug + n, cmp);

  int l = 1, r = n;
  while (l <= r) {
    int m = (l + r) / 2;

    if (solve(m))
      l = m + 1;
    else
      r = m - 1;
  }

  if (r == -1)
    puts("0");
  else
    printf("%d\\n", r);

  return 0;
}

Error: TLE

Fixed submission:

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

struct BUG {
  LL out, in;

  bool operator<(const BUG &t) const { return out > t.out; }
};

bool cmp(const BUG &a, const BUG &b) { return a.in > b.in; };

int n;
BUG bug[300001];

bool solve(int x) {
  priority_queue<BUG> Q;
  int lim = 0;
  for (; lim < x - 1; lim++) {
    Q.push(bug[lim]);
  }

  while (true) {
    priority_queue<BUG> que = Q;
    if (lim == n)
      return false;
    que.push(bug[lim]);
    Q.push(bug[lim]);
    while (bug[lim].in == bug[lim + 1].in) {
      lim++;
      que.push(bug[lim]);
      Q.push(bug[lim]);
    }

    // puts("");
    LL sum = 0;
    for (int i = 0; i < x; i++) {
      BUG b = que.top();
      que.pop();
      //	printf("%d\\n", b.out);
      sum += b.out;
    }

    if (sum <= bug[lim].in * x)
      return true;
    lim++;
  }
}

/*
bool solve(int x)
{
    int lim = x - 1;

    while (true){
        priority_queue<BUG> que;
        for (int i = 0; i <= lim; i++){
            que.push(bug[i]);
        }

        puts("");
        LL sum = 0;
        for (int i = 0; i < x; i++){
            BUG b = que.top(); que.pop();
                printf("%lld\\n", b.out);
            sum += b.out;
        }

        if (sum <= bug[lim].in * x) return true;
        else {
            lim++;
            if (lim == n) return false;
        }
    }
}
*/

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

  for (int i = 0; i < n; i++) {
    scanf("%lld %lld", &bug[i].out, &bug[i].in);
  }

  sort(bug, bug + n, cmp);

  int l = 1, r = n;
  while (l <= r) {
    int m = (l + r) / 2;

    if (solve(m))
      l = m + 1;
    else
      r = m - 1;
  }

  if (r == -1)
    puts("0");
  else
    printf("%d\\n", r);

  return 0;
}