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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

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

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

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

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

priority_queue<pair<ll, int>> pq_w, pq_n;
int left_n = 0;
bool f_left[SIZE];
char name[SIZE][30];

void pq_ctrl(int member) {

  while (pq_w.size() && f_left[pq_w.top().second]) {
    pq_w.pop();
    left_n--;
  }

  while (pq_n.size() && f_left[pq_n.top().second])
    pq_n.pop();

  while ((int)pq_w.size() - left_n > member / 5) {

    ll a = -pq_w.top().first;
    int b = pq_w.top().second;
    pq_w.pop();

    pq_n.push({a, b});

    printf("%s is not working now.\\n", name[b]);

    while (pq_w.size() && f_left[pq_w.top().second]) {
      pq_w.pop();
      left_n--;
    }
  }

  while (pq_w.size() - left_n < member / 5) {

    ll a = pq_n.top().first;
    int b = pq_n.top().second;
    pq_n.pop();

    pq_w.push({-a, b});

    printf("%s is working hard now.\\n", name[b]);

    while (pq_n.size() && f_left[pq_n.top().second]) {
      pq_n.pop();
    }
  }
}

int main() {
  int n, m, member;
  ll a[SIZE];
  map<string, int> dic;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%s%lld", name[i], a + i);
    a[i] *= 1000000;
    a[i] += i;
    pq_w.push({-a[i], i});
    dic[name[i]] = i;
  }

  member = n;

  while (pq_w.size() > n / 5) {
    ll a = -pq_w.top().first;
    int b = pq_w.top().second;
    pq_w.pop();

    pq_n.push({a, b});
  }

  scanf("%d", &m);

  for (int i = n; i < n + m; i++) {
    char c[2];

    scanf("%s", c);

    if (c[0] == '+') {

      scanf("%s%lld", name[i], a + i);
      dic[name[i]] = i;
      member++;

      a[i] *= 1000000;
      a[i] += i;

      if ((pq_w.empty() && member / 5 >= 1 && pq_n.top().first < a[i]) ||
          (pq_w.size() && -pq_w.top().first < a[i]) ||
          (pq_n.size() && pq_n.top().first < a[i] && member % 5 == 0)) {

        pq_w.push({-a[i], i});
        printf("%s is working hard now.\\n", name[i]);

      } else {
        pq_n.push({a[i], i});
        printf("%s is not working now.\\n", name[i]);
      }

    } else {
      scanf("%s", name[i]);

      int id = dic[name[i]];

      f_left[id] = true;
      member--;

      if (pq_w.size() && -pq_w.top().first <= a[id]) {
        left_n++;
      }
    }

    pq_ctrl(member);
  }

  return 0;
}

Error: TLE

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 <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

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

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

priority_queue<pair<ll, int>> pq_w, pq_n;
int left_n = 0;
bool f_left[SIZE];
char name[SIZE][30];

void pq_ctrl(int member) {

  while (pq_w.size() && f_left[pq_w.top().second]) {
    pq_w.pop();
    left_n--;
  }

  while (pq_n.size() && f_left[pq_n.top().second])
    pq_n.pop();

  while (pq_w.size() - left_n > member / 5) {

    ll a = -pq_w.top().first;
    int b = pq_w.top().second;
    pq_w.pop();

    pq_n.push({a, b});

    printf("%s is not working now.\\n", name[b]);

    while (pq_w.size() && f_left[pq_w.top().second]) {
      pq_w.pop();
      left_n--;
    }
  }

  while (pq_w.size() - left_n < member / 5) {

    ll a = pq_n.top().first;
    int b = pq_n.top().second;
    pq_n.pop();

    pq_w.push({-a, b});

    printf("%s is working hard now.\\n", name[b]);

    while (pq_n.size() && f_left[pq_n.top().second]) {
      pq_n.pop();
    }
  }
}

int main() {
  int n, m, member;
  ll a[SIZE];
  map<string, int> dic;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%s%lld", name[i], a + i);
    a[i] *= 1000000;
    a[i] += i;
    pq_w.push({-a[i], i});
    dic[name[i]] = i;
  }

  member = n;

  while (pq_w.size() > n / 5) {
    ll a = -pq_w.top().first;
    int b = pq_w.top().second;
    pq_w.pop();

    pq_n.push({a, b});
  }

  scanf("%d", &m);

  for (int i = n; i < n + m; i++) {
    char c[2];

    scanf("%s", c);

    if (c[0] == '+') {

      scanf("%s%lld", name[i], a + i);
      dic[name[i]] = i;
      member++;

      a[i] *= 1000000;
      a[i] += i;

      if ((pq_w.empty() && member / 5 >= 1 && pq_n.top().first < a[i]) ||
          (pq_w.size() && -pq_w.top().first < a[i]) ||
          (pq_n.size() && pq_n.top().first < a[i] && member % 5 == 0)) {

        pq_w.push({-a[i], i});
        printf("%s is working hard now.\\n", name[i]);

      } else {
        pq_n.push({a[i], i});
        printf("%s is not working now.\\n", name[i]);
      }

    } else {
      scanf("%s", name[i]);

      int id = dic[name[i]];

      f_left[id] = true;
      member--;

      if (-pq_w.top().first <= a[id]) {
        left_n++;
      }
    }

    pq_ctrl(member);
  }

  return 0;
}