끄적끄적
백준 [13904] 과제 본문
출처 : https://www.acmicpc.net/problem/13904
13904번: 과제
예제에서 다섯 번째, 네 번째, 두 번째, 첫 번째, 일곱 번째 과제 순으로 수행하고, 세 번째, 여섯 번째 과제를 포기하면 185점을 얻을 수 있다.
www.acmicpc.net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<queue> | |
using namespace std; | |
int main() { | |
ios::sync_with_stdio(false); | |
cin.tie(0); | |
cout.tie(0); | |
int N, d, w; // d - 과제마감일, w - 과제점수 | |
priority_queue<pair<int, int>> pq; | |
bool visited[1001] = {}; | |
int ans = 0; | |
cin >> N; | |
for (int i = 0; i < N; i++) { | |
cin >> d >> w; | |
pq.push({ w, d }); | |
} | |
while (!pq.empty()) { | |
int score = pq.top().first; | |
int deadline = pq.top().second; | |
pq.pop(); | |
for (int i = deadline; i >= 1; i--) { | |
if (!visited[i]) { | |
visited[i] = true; | |
ans += score; | |
break; | |
} | |
} | |
} | |
cout << ans << '\n'; | |
return 0; | |
} |
우선순위 큐를 사용해서 푸는 문제
점수가 높은 순으로 정렬해준다음, 과제를 하루에 하나씩 할 수 있다고 했으므로 각 과제를 할 날을 지정한다.
해당 일자에 다른 과제를 이미 하기로 했으면(방문한 적이 있다면), 날짜를 줄여가면서 찾기.
+ 배열을 선언할 때 전역으로 선언하면 초기화 해주지 않아도 되지만, 로컬로 선언할 경우 쓰레기값이 들어가서 초기화해줘야함.
'코테준비 > 백준' 카테고리의 다른 글
백준 [1012] 유기농 배추 (0) | 2022.08.18 |
---|---|
백준 [20166] 문자열 지옥에 빠진 호석 (0) | 2022.08.16 |
백준 [11000] 강의실 배정 (0) | 2022.08.14 |
백준 [2002] 추월 (0) | 2022.08.13 |
백준 [15903] 카드 합체 놀이 (0) | 2022.08.13 |