1..n
을 큐에 넣고, 맨 앞 버림 → 다음 카드 맨 뒤로 이동을 반복.pop
, push
, front
)은 모두 O(1) → 전체 O(n).1..n
을 큐에 삽입
큐 크기 > 1 동안:
pop()
(버림)push(front())
로 뒤에 붙이고 pop()
(앞에서 제거)남은 한 장 출력
#include <bits/stdc++.h>
using namespace std;
int n;
queue<int> q;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
int m = n;
int i = 1;
while (m--) {
q.push(i++);
}
while (q.size() > 1) {
q.pop();
q.push(q.front());
q.pop();
}
cout << q.front();
return 0;
}