Problem:
Problem G: Chairs
Problem
1から
N
の番号が割り当てられた
N
脚の椅子に、1から
N
のIDが割り当てられた
N
人の人が座ろうとしている。IDが
i
の人は椅子
p
i
に座りたいと思っている。
N
人の人はIDが小さい順に1列に並び、列の先頭の人が以下の行動をとる。
椅子
p
i
に誰も座っていなければ、その椅子に座る。
そうでなければ、
p
i
に1を加算し、列の最後尾に並び直す。ただし、
p
i
が
N
を超えた場合は
p
i
を1にする。
全ての人が椅子に座るまでこの行動が繰り返されたとき、最終的に各椅子に座っている人のIDを出力せよ。
Input
入力は以下の形式で与えられる。
N
p
1
p
2
...
p
N
1行目に整数
N
が与えられる。
2行目に
N
個の整数
p
1
,
p
2
, ...,
p
N
が空白区切りで与えられる。
Constraints
入力は以下の条件を満たす。
1 ≤
N
≤ 10
5
1 ≤
p
i
≤
N
Output
N
行に最終的な状態を出力する。
i
行目に椅子
i
に座っている人のIDを出力する。
Sample Input 1
5
1 2 3 4 5
Sample Output 1
1
2
3
4
5
Sample Input 2
5
3 3 4 4 5
Sample Output 2
4
2
1
3
5

Submitted solutions:

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<int> res(n,-1);
    vector<vector<int>> v(2*n);
    for (int i = 0; i < n; ++i) {
        int p; cin >> p; p--;
        v[p].push_back(i+1);
    }
    for (int i = 0; i < n; ++i) {
        reverse(v[i].begin(), v[i].end());
    }
    vector<int> st;
    for (int i = 0; i < 2*n; ++i) {
        for (int j : v[i]) st.push_back(j);
        if (st.size() and res[i%n] == -1) {
            res[i%n] = st.back(); st.pop_back();
        }
    }
    for (int i = 0; i < n; ++i) {
        cout << res[i] << "\n";
    }
}
accuracy: 1
time_ms: 10
memory_kb: 10128
final_rank: 1

code_snippet: /*  -*- coding: utf-8 -*-
 *
 * 3006.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_E2 = 1 << 18; // = 262144

/* typedef */

typedef queue<int> qi;

struct Elm {
  int v;
  Elm *nxt;
  Elm(): v(), nxt() {}
  Elm(int _v): v(_v), nxt() {}
  Elm(int _v, Elm *_nxt): v(_v), nxt(_nxt) {}
};

static Elm buf[MAX_N], *bpt = buf;

struct SList {
  Elm *head, *tail;
  int sz;
  SList(): head(), tail(), sz(0) {}

  void clear() { tail = head = NULL, sz = 0; }

  int size() { return sz; }
  bool empty() { return (sz == 0); }
  int front() { return (head != NULL) ? head->v : -1; }

  void add(int v) {
    Elm *ept = bpt++;
    ept->v = v, ept->nxt = NULL;

    if (head == NULL)
      head = tail = ept, sz = 1;
    else {
      tail->nxt = ept;
      tail = ept;
      sz++;
    }
  }

  void erase() {
    if (head != NULL) {
      head = head->nxt;
      sz--;
      if (head == NULL) tail = NULL;
    }
  }

  void append(const SList &sl) {
    if (head == NULL)
      *this = sl;
    else {
      tail->nxt = sl.head;
      tail = sl.tail;
      sz += sl.sz;
    }
  }
};

template <typename T, const int MAX_E2>
struct SegTreeMin {
  int n, e2;
  T nodes[MAX_E2], def;
  SegTreeMin() {}

  void init(int _n, T _def) {
    n = _n; def = _def;
    for (e2 = 1; e2 < n; e2 <<= 1);
    fill(nodes, nodes + MAX_E2, def);
  }

  T &get(int i) { return nodes[e2 - 1 + i]; }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = min<T>(nodes[j * 2 + 1], nodes[j * 2 + 2]);
    }
  }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = min<T>(nodes[j * 2 + 1], nodes[j * 2 + 2]);
  }

  T min_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return def;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = min_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = min_range(r0, r1, k * 2 + 2, im, i1);
    return min<T>(v0, v1);
  }
  T min_range(int r0, int r1) { return min_range(r0, r1, 0, 0, e2); }
};

/* global variables */

int ps[MAX_N], cs[MAX_N];
SList sls[MAX_N + 1];
SegTreeMin<int,MAX_E2> sta, stq;

/* subroutines */

/* main */

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

  sta.init(n, n);
  for (int i = 0; i < n; i++) sta.get(i) = i;
  sta.setall();
  stq.init(n + 1, n);
  
  memset(cs, -1, sizeof(cs));
  qi wq;

  for (int i = 0; i < n; i++) {
    int pi;
    scanf("%d", &pi);
    pi--;

    if (cs[pi] < 0) {
      cs[pi] = i;
      sta.set(pi, n);
    }
    else {
      if (sls[pi].empty()) wq.push(pi);
      sls[pi].add(i);
      stq.set(pi, pi);
    }
  }
  //printf("wq=%lu\n", wq.size());

  while (! wq.empty()) {
    int u = wq.front(); wq.pop();
    SList &slu = sls[u];
    if (slu.empty()) continue;

    int qu = (u >= n) ? 0 : u + 1;
    int v = stq.min_range(qu, n);
    //printf("u=%d, v=%d\n", u, v);

    while (! slu.empty()) {
      int ai = sta.min_range(qu, v);
      if (ai >= n) break;

      cs[ai] = slu.front();
      sta.set(ai, n);
      slu.erase();
    }

    stq.set(u, n);
    if (! slu.empty()) {
      if (stq.get(v) >= n) {
	stq.set(v, v);
	wq.push(v);
      }
      sls[v].append(slu);
      slu.clear();
    }
  }

  for (int i = 0; i < n; i++) printf("%d\n", cs[i] + 1);
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 9760
final_rank: 3

code_snippet: /*  -*- coding: utf-8 -*-
 *
 * 3006.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_E2 = 1 << 18; // = 262144

/* typedef */

typedef queue<int> qi;

struct Elm {
  int v;
  Elm *nxt;
  Elm(): v(), nxt() {}
  Elm(int _v): v(_v), nxt() {}
  Elm(int _v, Elm *_nxt): v(_v), nxt(_nxt) {}
};

static Elm buf[MAX_N], *bpt = buf;

struct SList {
  Elm *head, *tail;
  int sz;
  SList(): head(), tail(), sz(0) {}

  void clear() { tail = head = NULL, sz = 0; }

  int size() { return sz; }
  bool empty() { return (sz == 0); }
  int front() { return (head != NULL) ? head->v : -1; }

  void add(int v) {
    Elm *ept = bpt++;
    ept->v = v, ept->nxt = NULL;
    if (head == NULL)
      head = tail = ept, sz = 1;
    else {
      tail->nxt = ept;
      tail = ept;
      sz++;
    }
  }

  void erase() {
    if (head != NULL) {
      head = head->nxt;
      sz--;
      if (head == NULL) tail = NULL;
    }
  }

  void append(const SList &sl) {
    if (head == NULL)
      *this = sl;
    else {
      tail->nxt = sl.head;
      tail = sl.tail;
      sz += sl.sz;
    }
  }
};

template <typename T, const int MAX_E2>
struct SegTreeMin {
  int n, e2;
  T nodes[MAX_E2], def;
  SegTreeMin() {}

  void init(int _n, T _def) {
    n = _n; def = _def;
    for (e2 = 1; e2 < n; e2 <<= 1);
    fill(nodes, nodes + MAX_E2, def);
  }

  T &get(int i) { return nodes[e2 - 1 + i]; }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = min<T>(nodes[j * 2 + 1], nodes[j * 2 + 2]);
    }
  }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = min<T>(nodes[j * 2 + 1], nodes[j * 2 + 2]);
  }

  T min_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return def;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = min_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = min_range(r0, r1, k * 2 + 2, im, i1);
    return min<T>(v0, v1);
  }
  T min_range(int r0, int r1) { return min_range(r0, r1, 0, 0, e2); }
};

/* global variables */

int ps[MAX_N], cs[MAX_N];
SList sls[MAX_N];
SegTreeMin<int,MAX_E2> sta, stq;

/* subroutines */

/* main */

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

  sta.init(n, n);
  for (int i = 0; i < n; i++) sta.get(i) = i;
  sta.setall();
  stq.init(n + 1, n);
  
  memset(cs, -1, sizeof(cs));
  qi wq;

  for (int i = 0; i < n; i++) {
    int pi;
    scanf("%d", &pi);
    pi--;

    if (cs[pi] < 0) {
      cs[pi] = i;
      sta.set(pi, n);
    }
    else {
      if (sls[pi].empty()) wq.push(pi);
      sls[pi].add(i);
      stq.set(pi, pi);
    }
  }
  //printf("wq=%lu\n", wq.size());

  while (! wq.empty()) {
    int u = wq.front(); wq.pop();
    SList &slu = sls[u];
    if (slu.empty()) continue;

    int qu = (u >= n) ? 0 : u + 1;
    int v = stq.min_range(qu, n);
    //printf("u=%d, v=%d\n", u, v);

    while (! slu.empty()) {
      int ai = sta.min_range(qu, v);
      if (ai >= n) break;

      cs[ai] = slu.front();
      sta.set(ai, n);
      slu.erase();
    }

    stq.set(u, n);
    if (! slu.empty()) {
      if (stq.get(v) >= n) {
	stq.set(v, v);
	wq.push(v);
      }
      sls[v].append(slu);
      slu.clear();
    }
  }

  for (int i = 0; i < n; i++) printf("%d\n", cs[i] + 1);
  return 0;
}
accuracy: 0.6944444444444444
time_ms: 40
memory_kb: 9780
final_rank: 16

code_snippet: #include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <climits>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <map>
#include <tuple>
#include <iostream>
#include <deque>
#include <array>
#include <set>
#include <functional>
#include <memory>

class UnionFind {
	std::vector<int> _vec;
public:
	UnionFind(const int size) :_vec(size, -1) {};
	int find(const int a) {
		if (_vec[a] < 0) return a;
		else return _vec[a] = find(_vec[a]);
	}
	int is_same(const int a, const int b) {
		return find(a) == find(b);
	}
	void merge(const int a, const int b) {
		auto ar = find(a);
		auto br = find(b);
		if (ar != br) {
			if (_vec[ar] < _vec[br]) {
				_vec[ar] += _vec[br];
				_vec[br] = ar;
			}
			else {
				_vec[br] += _vec[ar];
				_vec[ar] = br;
			}
		}
	}

};

int main() {
	int n; std::cin >> n;
	std::vector<int> sheet(n);
	for (auto& p : sheet) std::cin >> p;
	std::vector<int> state(n, 0);
	std::vector<std::deque<int>> waiting(n);
	UnionFind union_find(n);
	std::deque<int> deque;
	for (auto i = 0; i < n; ++i) {
		if (state[sheet[i] - 1] == 0) {
			state[sheet[i] - 1] = i + 1;
		}
		else {
			if (waiting[sheet[i] - 1].empty()) {
				deque.push_back(sheet[i] - 1);
			}
			waiting[sheet[i] - 1].push_back(i);
		}
	}
	for (const auto pos : deque) {
		for (auto i = 1; i < n && !waiting[pos].empty(); ++i) {
			if (state[(pos + i) % n] == 0) {
				state[(pos + i) % n] = waiting[pos].front() + 1;
				waiting[pos].pop_front();
			}
			else if (!waiting[(pos + i) % n].empty()) {
				if (waiting[(pos + i) % n].size() < waiting[pos].size()) {
					while (!waiting[(pos + i) % n].empty()) {
						auto last = waiting[(pos + i) % n].back();
						waiting[(pos + i) % n].pop_back();
						waiting[pos].push_front(last);
					}
					waiting[(pos + i) % n].shrink_to_fit();
					waiting[(pos + i) % n].swap(waiting[pos]);
				}
				else {
					while (!waiting[pos].empty()) {
						auto front = waiting[pos].front();
						waiting[pos].pop_front();
						waiting[(pos + i) % n].push_back(front);
					}
					waiting[pos].shrink_to_fit();
				}
			}
		}
	}
	for (const auto s : state) {
		std::cout << s << '\n';
	}
}
accuracy: 1
time_ms: 70
memory_kb: 73820
final_rank: 12

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<utility>
#include<set>
#include<map>
#include<stack>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef vector<int> vec;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define stop char nyaa;cin>>nyaa;

void solve() {
	int n;
	cin >> n;
	vector<vector<int>> p(n);
	rep(i, n) {
		int x;
		cin >> x;
		--x;
		p[x].push_back(i+1);
	}
	vector<int> ans(n);
	stack<int> st;
	int cnt = 0;
	int cur = 0;
	while (cnt<n) {
		if (p[cur].size()) {
			int m = p[cur].size();
			rep(i, p[cur].size()) {
				st.push(p[cur][m - i - 1]);
			}
			p[cur].clear();
		}
		if (!ans[cur] && st.size()) {
			ans[cur] = st.top();
			++cnt;
			st.pop();
		}
		cur = (cur + 1) % n;
	}
	rep(i, n)cout << ans[i] << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	stop
		return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 7540
final_rank: 5

code_snippet: #include "bits/stdc++.h"

#pragma warning(disable:4996)
using namespace std;
using ld = long double;


int main() {
	int N;cin>>N;
	vector<vector<int>>vecs(N);
	for (int i = 0; i < N; ++i) {
		int a;cin>>a;a--;
		a=N-1-a;
		vecs[a].push_back(i);
	}
	list<int>li;
	vector<int>anss(N);
	auto k=li.begin();
	queue<decltype(k)>que;
	for (int i = 0; i < N; ++i) {
		li.push_back(-1-i);
		for (int j=0;j<vecs[i].size();++j){
			li.push_back(vecs[i][j]);
		}
	}

	for (auto it = li.begin(); it != li.end(); ++it) {
		auto jt=next(it);
		if (jt != li.end()) {
			int a=*it;
			int b=*jt;
			if (a < 0 && b >= 0) {
				que.push(jt);
			}
		}
	}
	while (!que.empty()) {
		auto it=que.front();
		que.pop();
		int a,b;
		if (it == li.begin()||it==li.end()) {
			a=li.back();
			b=li.front();
			anss[-a - 1] = b + 1;

			li.erase(li.begin());
			li.erase(prev(li.end()));

			if (!li.empty() && li.back() < 0 && li.front() >= 0) {
				que.push(li.begin());
			}
		}
		else {
			a=*prev(it);
			b = *it;

			anss[-a-1]=b+1;
			auto kt=li.erase(it);
			kt=li.erase(prev(kt));

			if (!li.empty()) {
				if (kt == li.end()||kt==li.begin()) {
					if (li.back() < 0 && li.front() >= 0) {
						que.push(li.begin());
					}
				}
				else {
					if (*prev(kt) < 0 && *kt >= 0) {
						que.push(kt);
					}
				}
			}
		}
	}

	for (int i = 0; i < anss.size(); ++i) {
		cout<<anss[N-1-i]<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 14404
final_rank: 8

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


int table[100001],ans[100001];
stack<int> First[100001];

int main(){

	int N;
	scanf("%d",&N);

	table[0] = 0;
	for(int i = 1; i <= N; i++)table[i] = -1;

	int tmp;

	for(int i = 1; i <= N; i++){
		scanf("%d",&tmp);
		First[tmp].push(i);
		table[tmp]++;
	}

	for(int i = 1; i <= N; i++)table[i] += table[i-1];

	int minimum = BIG_NUM,min_index;

	for(int i = 1; i <= N; i++){
		if(table[i] < minimum){
			minimum = table[i];
			min_index = i;
		}
	}

	int start_pos;

	if(min_index == N){
		start_pos = 1;
	}else{
		start_pos = min_index+1;
	}

	stack<int> S;
	int dealt_num = 0,loc = start_pos;

	while(dealt_num < N){

		while(!First[loc].empty()){
			S.push(First[loc].top());
			First[loc].pop();
		}

		if(S.empty()){
			if(loc == N){
				loc = 1;
			}else{
				loc++;
			}
			continue;
		}

		ans[loc] = S.top();
		S.pop();
		dealt_num++;

		if(loc == N){
			loc = 1;
		}else{
			loc++;
		}
	}

	for(int i = 1; i <= N; i++){
		printf("%d\n",ans[i]);
	}

	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 71420
final_rank: 11

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

int n, p, ans[100005];
deque<int> q, q2[100005];

int main(){
  
  cin>>n;
  
  for(int i=0;i<n;i++){
    scanf("%d",&p);
    q2[p-1].push_back(i+1);
  }
  
  for(int i=0;i<2;i++){
    for(int j=0;j<n;j++){
      
      while(!q2[j].empty()){
	q.push_front(q2[j].back());
	q2[j].pop_back();
      }
      
      if(ans[j]==0&&!q.empty()){
	ans[j]=q.front();
	q.pop_front();
      }
      
    }
  }
  
  for(int i=0;i<n;i++) cout<<ans[i]<<endl;
  
  return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 70948
final_rank: 13

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

int n, p, ans[100005];
deque<int> q, q2[100005];

int main(){
  
  cin>>n;
  
  for(int i=0;i<n;i++){
    scanf("%d",&p);
    q2[p-1].push_back(i+1);
  }
  
  for(int i=0;i<2;i++){
    for(int j=0;j<n;j++){
      
      while(!q2[j].empty()){
	q.push_front(q2[j].back());
	q2[j].pop_back();
      }
      
      if(ans[j]==0){
	ans[j]=q.front();
	q.pop_front();
      }
      
    }
  }
  
  for(int i=0;i<n;i++) cout<<ans[i]<<endl;
  
  return 0;
}
accuracy: 0.1388888888888889
time_ms: 50
memory_kb: 70456
final_rank: 20

code_snippet: #include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
#include <iostream>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <iostream>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
int res[100001];
vector<int> col[100001];
int sum[100005];

int main(void){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		int p;
		scanf("%d",&p);
		col[p-1].push_back(i);
	}
	for(int i=0;i<n;i++){
		sum[i]=-1;
	}
	int mini=0;
	for(int i=0;i<n;i++){
		sum[i]+=col[i].size();
		if(i>0)sum[i]+=sum[i-1];
		if(sum[i]<sum[mini])mini=i;
	}
	stack<int> sta;
	for(int i=0;i<n;i++){
		mini=(mini+1)%n;
		if(col[mini].size()>0){
			res[mini]=col[mini][0];
			int N=col[mini].size();
			for(int i=N-1;i>=1;i--){
				sta.push(col[mini][i]);
			}
		}else{
			int v=sta.top();
			res[mini]=v;
			sta.pop();
		}
	}
	for(int i=0;i<n;i++){
		printf("%d\n",res[i]+1);
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 8256
final_rank: 2

code_snippet: #include <cstdio>
#include <vector>
#include <stack>

int main()
{
    int N, p;
    scanf("%d", &N);
    
    std::vector<std::stack<int>> G(N);
    for (int i = 0; i < N; i++) {
        scanf("%d", &p);
        G[p - 1].push(i);
    }

    std::vector<int> res(N, -1);
    std::stack<int> st;
    for (int t = 0; t < 2; t++) {
        for (int i = 0; i < N; i++) {
            while (!G[i].empty()) {
                st.push(G[i].top()); G[i].pop();
            }
            if (res[i] == -1 && !st.empty()) {
                res[i] = st.top(); st.pop();
            }
        }
    }
    for (int i = 0; i < N; i++) {
        printf("%d\n", res[i] + 1);
    }    
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 70552
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define MAX_N 100005

int N;

stack<int> G[MAX_N];

int ans[MAX_N];

int main(){
  cin>>N;
  for(int i=1;i<=N;i++){
    int x;
    cin>>x;
    G[x].push(i);
  }
  stack<int> Q;
  for(int i=1;i<=N;i++){
    while(!G[i].empty()){
      Q.push(G[i].top());
      G[i].pop();
    }
    if(ans[i]==0 && !Q.empty()){
      ans[i]=Q.top();
      Q.pop();
    }
  }
  for(int i=1;i<=N;i++){
    if(ans[i]==0){
      ans[i]=Q.top();
      Q.pop();
    }
  }
  for(int i=1;i<=N;i++){
    cout<<ans[i]<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 70972
final_rank: 14

code_snippet: #include <cstdio>
#include <vector>
#include <stack>

int main()
{
    int N, p;
    scanf("%d", &N);
    
    std::vector<int> s(N, -1);    
    std::vector<std::stack<int>> G(N);
    for (int i = 0; i < N; i++) {
        scanf("%d", &p);
        s[p - 1] += 1;
        G[p - 1].push(i);
    }

    for (int i = 1; i < N; i++) {
        s[i] += s[i - 1];
    }
    
    int min = N, min_idx = -1;
    for (int i = 0; i < N; i++) {
        if (s[i] < min) {
            min = s[i];
            min_idx = i;
        }
    }

    min_idx = (min_idx + 1) % N;
    
    std::vector<int> res(N);
    std::stack<int> st;
    for (int i = 0; i < N; i++) {
        int p = (i + min_idx) % N;
        while (!G[p].empty()) {
            int t = G[p].top();
            G[p].pop();
            st.push(t);            
        }
        res[p] = st.top();
        st.pop();
    }

    for (int num : res) {
        printf("%d\n", num + 1);
    }    
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 70956
final_rank: 10

code_snippet: #include <cstdio>
#include <vector>
#include <set>
#include <list>

void simulate(int, std::vector<std::list<int>>&);

int main()
{
    int N, p;
    scanf("%d", &N);
    std::vector<std::list<int>> pl(N); /* index???????????°??§??????????????? */
    for (int i = 0; i < N; i++) {
        scanf("%d", &p);
        pl[p - 1].push_back(i + 1);
    }
    simulate(N, pl);    
    return 0;
}

void simulate(int N, std::vector<std::list<int>>& pl)
{
    std::vector<int> seats(N, -1); /* ??????????????§?????¨ */
    
    std::set<int> next;
    std::set<int> vacant_seats; /* ??????????????? */
    
    for (int i = 0; i < N; i++) {
        if (!pl[i].empty()) {
            next.insert(i);
        }
        vacant_seats.insert(i);
    }
    
    while (!vacant_seats.empty()) {
        int n = *next.begin();        
        
        next.erase(n);
        seats[n] = pl[n].front();       
        pl[n].pop_front();

        vacant_seats.erase(n);
        auto it = vacant_seats.upper_bound(n);
        if (it == vacant_seats.end()) {
            it = vacant_seats.begin();
        }
                
        if (!pl[n].empty()) {
            pl[*it].splice(pl[*it].end(), pl[n]);
            next.insert(*it);
        }
    }

    for (int num : seats) {
        printf("%d\n", num);
    }
}
accuracy: 1
time_ms: 60
memory_kb: 15084
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
stack<int> st[111111],s;
int a[111111],n;

int main() {
  cin >> n;
  for(int i=0,x; i<n; i++) {
    cin >> x;
    st[x-1].push(i+1);
  }
  for(int k=0; k<2; k++) {
    for(int i=0; i<n; i++) {
      while(!st[i].empty()) s.push(st[i].top()),st[i].pop();
      if(!a[i]&&!s.empty()) a[i]=s.top(),s.pop();
    }
  }
  for(int i=0; i<n; i++) cout << a[i] << endl;
  return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 78436
final_rank: 15

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

#define CIN_ONLY if(1)
struct cww{cww(){
    CIN_ONLY{
        ios::sync_with_stdio(false);cin.tie(0);
    }
}}star;
#define fin "\n"
#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define DEBUG if(0)
#define REC(ret, ...) std::function<ret (__VA_ARGS__)>
template <typename T>inline bool chmin(T &l,T r)
{bool a=l>r;if(a)l=r;return a;}
template <typename T>inline bool chmax(T &l,T r)
{bool a=l<r;if(a)l=r;return a;}
template <typename T>
istream& operator>>(istream &is,vector<T> &v){
    for(auto &it:v)is>>it;
    return is;
}
    
int main(){
    int N;
    cin>>N;
    vector<int> p(N);
    cin>>p;
    vector<int> a(N,-1);
    vector<vector<int>> g(N);
    stack<int> s;
    FOR(i,1,N+1){
        int id=N-i;
        g[p[id]-1].pb(id);
    }
    REP(i,N){
        for(auto &it:g[i])s.push(it);
        if(s.size()&&a[i]==-1){
            a[i]=s.top();s.pop();
        }
    }
    REP(i,N){
        if(s.size()&&a[i]==-1){
            a[i]=s.top();s.pop();
        }
    }
    REP(i,N)cout<<a[i]+1<<endl;
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 7876
final_rank: 6

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}
int N;
const int MN=100000;
vector<int> a[MN];
int ans[MN];
stack<int> st;
int main(){
	cin>>N;
	rep(i,N) ans[i]=-1;
	rep(i,N){
		int x;
		cin>>x;
		x--;
		a[x].pb(i);
	}
	rep(i,N) reverse(all(a[i]));
	rep(i,2*N){
		if(i<N) for(int x:a[i]) st.push(x);
		if(!st.empty() && ans[i%N]==-1){
			ans[i%N]=st.top();
			st.pop();
		}
	}
	rep(i,N) cout<<ans[i]+1<<endl;
}
accuracy: 1
time_ms: 110
memory_kb: 7848
final_rank: 7

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}
int N;
const int MN=100000;
vector<int> a[MN];
int ans[MN];
stack<int> st;
int main(){
	cin>>N;
	assert(N<=MN);
	rep(i,N){
		int x;
		cin>>x;
		x--;
		a[x].pb(i);
	}
	rep(i,N) reverse(all(a[i]));
	rep(i,2*N){
		if(i<N) for(int x:a[i]) st.push(x);
		if(!st.empty()){
			ans[i%N]=st.top();
			st.pop();
		}
	}
	rep(i,N) cout<<ans[i]+1<<endl;
}
accuracy: 0.3888888888888889
time_ms: 110
memory_kb: 7392
final_rank: 17

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}
int N;
const int MN=200000;
vector<int> a[MN];
int ans[MN];
stack<int> st;
int main(){
	cin>>N;
	rep(i,N){
		int x;
		cin>>x;
		x--;
		a[x].pb(i);
	}
	rep(i,N) reverse(all(a[i]));
	rep(i,2*N){
		if(i<N) for(int x:a[i]) st.push(x);
		if(!st.empty()){
			ans[i%N]=st.top();
			st.pop();
		}
	}
	rep(i,N) cout<<ans[i]+1<<endl;
}
accuracy: 0.3888888888888889
time_ms: 110
memory_kb: 9820
final_rank: 19

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}
int N;
const int MN=100000;
vector<int> a[MN];
int ans[MN];
stack<int> st;
int main(){
	cin>>N;
	rep(i,N){
		int x;
		cin>>x;
		x--;
		a[x].pb(i);
	}
	rep(i,N) reverse(all(a[i]));
	rep(i,2*N){
		if(i<N) for(int x:a[i]) st.push(x);
		if(!st.empty()){
			ans[i%N]=st.top();
			st.pop();
		}
	}
	rep(i,N) cout<<ans[i]+1<<endl;
}
accuracy: 0.3888888888888889
time_ms: 110
memory_kb: 7548
final_rank: 18