※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 15235번 문제인 Olympiad Pizza이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/15235 

 

15235번: Olympiad Pizza

The contestants that get a slice are, in order: 1, 2, 3, 4, 2, 4, 2, 4, 4. So at t=1s the first contestant get all slices, at t=3s the third contestant gets a slice, at t=7s the second student finishes and finally at t=9s the fourth student gets the last s

www.acmicpc.net

N과 각 참가자들이 가져가기를 희망하는 피자조각의 수의 크기가 충분히 작음을 관찰하자.

 

입력의 크기가 충분히 작으므로, 문제의 상황을 각 참가자들이 앞으로 더 가져가기를 원하는 피자조각의 수와 큐 자료구조를 이용해 직접 시뮬레이션하는 것으로 문제를 해결할 수 있다.

 

아래는 제출한 소스코드이다.

#include <iostream>
#include <queue>
using namespace std;

int ans[1000];
int cnt[1000];
queue<int> que;

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

	int N; cin >> N;
	for (int i = 0; i < N; i++) cin >> cnt[i], que.push(i);

	int turn = 0;
	while (!que.empty()) {
		turn++;
		int cur = que.front(); que.pop();
		cnt[cur]--;
		if (cnt[cur]) que.push(cur);
		else ans[cur] = turn;
	}

	for (int i = 0; i < N; i++) cout << ans[i] << ' ';
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 9240 // C++] 로버트 후드  (0) 2023.05.20
[BOJ 1708 // C++] 볼록 껍질  (0) 2023.05.19
[BOJ 15237 // C++] Cipher  (0) 2023.05.17
[BOJ 15242 // C++] Knight  (0) 2023.05.16
[BOJ 15245 // C++] Boom!  (0) 2023.05.15

+ Recent posts