lecture/algorithm - c++
[c++] 최대수입 스케줄
tonirr
2021. 1. 15. 23:50
최대수입 스케줄
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
struct Data {
int money;
int when;
Data(int a, int b){
money=a;
when=b;
}
bool operator<(Data &b){
return when>b.when;
}
};
int main() {
freopen("input.txt", "rt", stdin);
int a, b, i, j, n, res=0, max=-2147000000;
priority_queue<int> pQ;
vector<Data> T;
scanf("%d", &n);
for(i=0; i<n; i++){
scanf("%d %d", &a, &b);
T.push_back(Data(a, b));
if(b>max){
max=b;
}
}
sort(T.begin(), T.end());
j=0;
for(i=max; i>0; i--){
for( ; j<=n; j++){
if(T[j].when<i) break;
pQ.push(T[j].money);
}
if(!pQ.empty()){
res+=pQ.top();
pQ.pop();
}
}
printf("%d", res);
return 0;
}