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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <unordered_map>
#define inputt                                                                 \\
  int t;                                                                       \\
  cin >> t;
#define pi acos(-1.0)
#define lson o * 2, l, m
#define rson o * 2 + 1, m + 1, r
#define INF 0x7f7f7f7f
#define lowbit(X) ((X) & (-X))
#define clr(X, Y) memset(X, Y, sizeof(X))
typedef long long ll;
using namespace std;

struct node {
  char s[25];
  int val;
  int time;
} a[70005];

struct cmp1 {
  bool operator()(const node &a, const node b) const {
    return a.val < b.val || a.val == b.val && a.time < b.time;
  }
};
struct cmp2 {
  bool operator()(const node &a, const node b) const {
    return a.val > b.val || a.val == b.val && a.time > b.time;
  }
};
unordered_map<string, int> mp;
set<node, cmp1> maxheap;
set<node, cmp2> minheap;
set<node, cmp1>::iterator it1;
set<node, cmp2>::iterator it2;
void print() {
  printf("-------------\\n");
  printf("max\\n");
  for (it2 = minheap.begin(); it2 != minheap.end(); it2++) {
    node p = *it2;
    printf("%s %d %d\\n", p.s, p.val, p.time);
  }
  printf("min\\n");
  for (it1 = maxheap.begin(); it1 != maxheap.end(); it1++) {
    node p = *it1;
    printf("%s %d %d\\n", p.s, p.val, p.time);
  }
  printf("-------------\\n");
}
int main() {
  int n;
  scanf("%d", &n);
  int now = n;
  for (int i = 0; i < n; i++) {
    scanf("%s%d", a[i].s, &a[i].val);
    mp[a[i].s] = i;
    a[i].time = i;
    minheap.insert(a[i]);
    if (minheap.size() > n * 0.2) {
      it2 = minheap.end();
      it2--;
      maxheap.insert(*it2);
      minheap.erase(it2);
    }
  }
  // print();
  int m;
  char op[2];
  char ts[25];
  scanf("%d", &m);
  for (int i = n; i < n + m; i++) {
    scanf("%s", op);
    if (op[0] == '+') {
      scanf("%s%d", a[i].s, &a[i].val);
      a[i].time = i;
      mp[a[i].s] = i;
      if (int((now + 1) * 0.2) - int(now * 0.2) == 0) {
        minheap.insert(a[i]);
        if (minheap.size() > int((now + 1) * 0.2)) {
          it2 = minheap.end();
          it2--;
          if ((*it2).time == a[i].time) {
            printf("%s is not working now.\\n", a[i].s);
            maxheap.insert(*it2);
            minheap.erase(it2);
          } else {
            printf("%s is working hard now.\\n%s is not working now.\\n", a[i].s,
                   (*it2).s);
            maxheap.insert(*it2);
            minheap.erase(it2);
          }
        }
      } else {
        it1 = maxheap.end();
        it1--;
        if ((*it1).val > a[i].val) {
          printf("%s is not working now.\\n%s is working hard now.\\n", a[i].s,
                 (*it1).s);
          minheap.insert((*it1));
          maxheap.erase(it1);
          maxheap.insert(a[i]);
        } else {
          printf("%s is working hard now.\\n", a[i].s);
          minheap.insert(a[i]);
        }
      }
      now++;
    } else {
      scanf("%s", ts);
      int idx = mp[ts];
      if (int(now * 0.2) - int((now - 1) * 0.2) ==
          0) { //???????? working??????±?
        if (minheap.find(a[idx]) != minheap.end()) {
          minheap.erase(a[idx]);
          it1 = maxheap.end();
          it1--;
          minheap.insert(*it1);
          printf("%s is working hard now.\\n", (*it1).s);
          maxheap.erase(it1);
        } else if (maxheap.find(a[idx]) != maxheap.end()) {
          maxheap.erase(a[idx]);
        }
      } else { //????????
        if (minheap.find(a[idx]) !=
            minheap.end()) { // ??working??????£¬?±???????´??
          minheap.erase(a[idx]);
        } else if (maxheap.find(a[idx]) != maxheap.end()) { //??not
                                                            //working??????£¬?????¢´?work??????????not
                                                            //working
          maxheap.erase(a[idx]);
          it2 = minheap.end();
          it2--;
          maxheap.insert(*it2);
          printf("%s is not working now.\\n", (*it2).s);
          minheap.erase(it2);
        }
      }
      now--;
    }
    // print();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

int N, M;

struct Person {
  char name[25];
  int time;
  int level;
  bool tleft;
  bool operator<(Person &p) {
    return level > p.level || level == p.level && time > p.time;
  }
};

vector<Person> vec;

int main() {
  // freopen("in.txt", "r", stdin);
  scanf("%d", &N);
  int number = N;
  int t = number * 0.2;
  for (int i = 1; i <= N; i++) {
    Person tmp;
    scanf("%s", tmp.name);
    scanf("%d", &tmp.level);
    tmp.time = i;
    tmp.tleft = false;
    vec.push_back(tmp);
  }

  sort(vec.begin(), vec.end());

  for (int i = 0; i < t; i++) {
    vec[i].tleft = true;
  }

  scanf("%d", &M);
  for (int i = 1; i <= M; i++) {
    char c[5];
    scanf("%s", c);

    if (strcmp(c, "+") == 0) {
      number++;
      t = number * 0.2;

      Person tmp;
      scanf("%s", tmp.name);
      scanf("%d", &tmp.level);
      tmp.time = N + i;
      tmp.tleft = false;

      int j;
      for (j = 0; j < vec.size(); j++) {
        if (tmp < vec[j]) {
          vec.insert(vec.begin() + j, tmp);

          if (j < t) {
            printf("%s is working hard now.\\n", tmp.name);
            vec[j].tleft = true;
          } else {
            printf("%s is not working now.\\n", tmp.name);
          }
          break;
        }
      }

      if (j == vec.size()) {
        vec.push_back(tmp);
        printf("%s is not working now.\\n", tmp.name);
      }
    } else {
      number--;
      t = number * 0.2;

      char tmp[25];
      scanf("%s", tmp);
      for (int i = 0; i < vec.size(); i++) {
        if (strcmp(vec[i].name, tmp) == 0) {
          vec.erase(vec.begin() + i);
          break;
        }
      }
    }

    for (int j = 0; j < t; j++) {
      if (vec[j].tleft == false) {
        printf("%s is working hard now.\\n", vec[j].name);
        vec[j].tleft = true;
      }
    }
    for (int j = t; j < vec.size(); j++) {
      if (vec[j].tleft == true) {
        printf("%s is not working now.\\n", vec[j].name);
        vec[j].tleft = false;
      }
    }
  }

  return 0;
}