백준 문제 풀이 & C++ 공부
백준 2109 C++
daisy0461
2024. 6. 23. 23:30
https://www.acmicpc.net/problem/2109
이건 별로 어려움 없이 풀었다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n; //강연 요청을 한 대학 횟수
int maxDay = 0;
ll result;
//int d; //d일안에 강의해라
//int p; //강연료
vector<pair<int, int>> lecs; //day, pay순
vector<int> days;
priority_queue<pair<int, int>> pq;
priority_queue<int> paypq;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
int d, p;
for (int i = 0; i < n; i++) {
cin >> p >> d;
if (d > maxDay) maxDay = d;
//lecs.push_back({ d, p });
pq.push({ d, p }); //day가 큰 순, day가 똑같다면 pay가 큰 순
}
//pq 출력
//for (int i = 0; i < n; i++) {
// cout << " day : " << pq.top().first << " pay : " << pq.top().second << "\n";
// pq.pop();
//}
while (maxDay) {
while (pq.size() && pq.top().first == maxDay) { //pq에 요소가있으면서 pq.top()에 해당하는 날짜가 maxDay와 같다면 -> 같은 이유가 어차피 -1씩 할거라서 모든 경우에 다 들어갈거다.
paypq.push(pq.top().second); //가격만 넣는 pq를 만들어서 가격이 높은 순으로 정렬한다.
pq.pop();
}
if (paypq.size()) { //paypq에 요소가 존재한다면?
result = result + paypq.top();
paypq.pop();
}
maxDay--;
}
cout << result;
}