[C++] 2164 : 카드2

리폐·2023년 11월 3일

백준

목록 보기
10/18

2164 : 카드2

#include <iostream>
using namespace std;

const int MX = 500005;
int que[MX];
int head = 0, tail = 0;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		tail = (tail + 1) % MX;
		que[tail] = i;
		//cout << que[tail] << " "; //큐검증
	}
	//cout << "\n";
    
	while (1) {
		head = (head + 1) % MX; //pop 
		if ((head - tail + MX) % MX == 1) break; //pop 할때마다 큐 크기가 1인지 확인
		head = (head + 1) % MX; // pop후 뒤에 넣기
		if ((head - tail + MX) % MX == 1) break;
		//cout << "head : " << head << " \nque[head] : " << que[head] << "\n";
		tail = (tail + 1) % MX;
		que[tail] = que[head];
		//cout << "tail : " << tail << " \nque[tail] : " << que[tail] << "\n";
	}
	cout << que[tail] << "\n";
}

2번 pop하고 마지막 pop은 que[tail]값에 넣기

profile
Unreal 5, Unity 공부

0개의 댓글