Skip to content

Commit c20e236

Browse files
committed
[Gold V] Title: 가희와 프로세스 1, Time: 256 ms, Memory: 4452 KB -BaekjoonHub
1 parent 9f30e0e commit c20e236

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# [Gold V] 가희와 프로세스 1 - 21773
2+
3+
[문제 링크](https://www.acmicpc.net/problem/21773)
4+
5+
### 성능 요약
6+
7+
메모리: 4452 KB, 시간: 256 ms
8+
9+
### 분류
10+
11+
자료 구조, 우선순위 큐
12+
13+
### 제출 일자
14+
15+
2026년 4월 11일 22:32:28
16+
17+
### 문제 설명
18+
19+
<p>가희는 스케쥴러를 구현하라는 과제를 받았습니다. 스케쥴러가 <strong>실행시킬 프로세스를 선택하는 기준</strong>은 아래와 같습니다.</p>
20+
21+
<ul>
22+
<li>우선 순위 값이 제일 큰 프로세스</li>
23+
<li>우선 순위 값이 제일 큰 프로세스가 여러 개라면, <em>id</em>가 가장 작은 프로세스</li>
24+
</ul>
25+
26+
<p>가희가 만든 스케쥴러는 다음 알고리즘으로 실행됩니다.</p>
27+
28+
<ol>
29+
<li>실행시킬 프로세스를 기준에 따라 선택합니다. 선택된 프로세스의 <em>id</em>를 <em>id<sub>s</sub></em>라 합니다. <em>id</em><sub><em>s</em></sub>를 실행시킵니다.</li>
30+
<li>1초가 지난 후, 프로세스 <em>id</em>가 <em>id<sub>s</sub></em>인 프로세스를 제외한 <strong>나머지 프로세스들의 우선 순위가 1 상승합니다.</strong> <br>
31+
프로세스 <em>id</em>가 <em>id<sub>s </sub></em>인 프로세스의 <strong>실행을 마치는 데 필요한 시간은 1 감소</strong>합니다.</li>
32+
<li>실행 시간이 남은 프로세스가 있다면 1로 돌아가고, 그렇지 않으면 종료합니다.</li>
33+
</ol>
34+
35+
<p>동시에 실행되는 프로세스는 1개이고, 1초일 때 가희가 만든 스케쥴러는 최초로 선택한 프로세스를 실행시키는 작업을 합니다.</p>
36+
37+
<p>가희는 1초일 때 부터 <em>T</em>초일 때 까지, 스케쥴러가 선택한 프로세스의 <em>id</em>를 알고 싶습니다. 가희를 도와주세요.</p>
38+
39+
### 입력
40+
41+
<p>첫 번째 줄에 <em>T</em>, <em>n</em>이 주어집니다.</p>
42+
43+
<p>두 번째 줄 부터 n+1번째 줄까지 다음과 같은 형식으로 주어집니다.</p>
44+
45+
<p><em>A<sub>i</sub> B<sub>i</sub> C<sub>i</sub></em></p>
46+
47+
<p>이것은 i번째 process의 <em>id</em>가 <em>A<sub>i</sub></em>이고, 프로세스 <em>id</em>가 실행을 마치는 데 필요한 시간이 <em>B<sub>i</sub></em>초이고, 초기 우선 순위가 <em>C<sub>i</sub></em>임을 의미합니다.</p>
48+
49+
### 출력
50+
51+
<p>T개의 정수를 T개의 줄에 출력합니다.</p>
52+
53+
<p>i번째 줄에는 <strong>가희가 만든 스케쥴러가 <em>i</em>초가 되었을 때 선택한 프로세스의 <em>id</em>를 출력</strong>해 주세요.</p>
54+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <iostream>
2+
#include <queue>
3+
using namespace std;
4+
5+
int t, n;
6+
struct Process
7+
{
8+
int id, time, priority;
9+
bool operator<( const Process& other ) const
10+
{
11+
if ( priority == other.priority )
12+
return id > other.id;
13+
14+
return priority < other.priority;
15+
}
16+
};
17+
18+
int main()
19+
{
20+
ios::sync_with_stdio( 0 );
21+
cin.tie( 0 );
22+
cout.tie( 0 );
23+
24+
cin >> t >> n;
25+
priority_queue< Process > pq;
26+
while ( n-- )
27+
{
28+
int a, b, c; cin >> a >> b >> c;
29+
pq.push( { a, b, c } );
30+
}
31+
32+
while ( t-- )
33+
{
34+
Process process = pq.top(); pq.pop();
35+
int id = process.id, time = process.time, priority = process.priority;
36+
cout << id << '\n';
37+
if ( --time )
38+
pq.push( { id, time, priority - 1 } );
39+
}
40+
}

0 commit comments

Comments
 (0)