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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int _loop_int;
#define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b)                                                          \\
  for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define CHMIN(a, b) a = min((a), (b))
#define CHMAX(a, b) a = max((a), (b))

int n;
int p[125252];
int ans[125252];

map<int, queue<int>> Q;

int data[125252];
void init() { REP(i, n) data[i] = -1; }
int next(int x) { return data[x] < 0 ? x : data[x] = next(data[x]); }
void unite(int x) {
  int y = (x + 1) % n;
  y = next(y);
  data[y] += data[x];
  data[x] = y;
}

int main() {
  scanf("%d", &n);
  init();
  REP(i, n) {
    scanf("%d", p + i);
    --p[i];
    Q[p[i]].push(i);
  }
  REP(i, n) ans[i] = -1;
  int rest = n;

  priority_queue<pii> S;
  for (auto &P : Q) {
    S.push(pii(-0, -P.first));
  }
  while (!S.empty()) {
    pii P = S.top();
    S.pop();
    int id = -P.second;
    int shft = -P.first;
    int pos = (id + shft) % n;
    if (ans[pos] == -1) {
      ans[pos] = Q[id].front();
      Q[id].pop();
      --rest;
      if (rest)
        unite(pos);
    }
    if (Q[id].size()) {
      int to = next(pos);
      int nsft = (to - id + n) % n;
      S.push(pii(-nsft, -id));
    }
  }
  REP(i, n) printf("%d\\n", ans[i] + 1);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int _loop_int;
#define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b)                                                          \\
  for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define CHMIN(a, b) a = min((a), (b))
#define CHMAX(a, b) a = max((a), (b))

int n;
int p[125252];
int ans[125252];

map<int, queue<int>> Q;
int shft;

int main() {
  scanf("%d", &n);
  REP(i, n) {
    scanf("%d", p + i);
    --p[i];
    Q[p[i]].push(i);
  }
  REP(i, n) ans[i] = -1;
  shft = 0;

  while (!Q.empty()) {
    vi el;
    for (auto &P : Q) {
      int pos = (P.first + shft) % n;
      auto &q = P.second;
      if (ans[pos] == -1) {
        int id = q.front();
        q.pop();
        ans[pos] = id;
        if (q.empty())
          el.push_back(P.first);
      }
    }
    for (int x : el) {
      Q.erase(x);
    }
    shft++;
  }
  REP(i, n) printf("%d\\n", ans[i] + 1);

  return 0;
}