• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    해결됨

연산자 오버로딩을 하면

22.05.24 16:21 작성 조회수 127

0

안녕하세요

구조체 내에 연산자 오버로딩을 하면 Sort함수에 쓰이는 원리가 궁금합니다.

참조형 매개변수로 B를 받고
A > B 이렇게하면 현재값이 앞에 놓이니깐 내림차순으로 정렬된다는게 어떤의미인지 이해가 잘 안됩니다 ㅠㅠ

#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
struct Data{
	int money;
	int when;
	Data(int a, int b){
		money=a;
		when=b;
	}
	bool operator<(const Data &b)const{
		return when>b.when;
	}	
};
int main(){
	freopen("input.txt", "rt", stdin);
	int n, i, j, a, b, res=0, max=-2147000000;	
	vector<Data> T;
	priority_queue<int> pQ;
	scanf("%d", &n);
	for(i=1; 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>=1; 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\n",res);
	return 0;
}

답변 1

답변을 작성해보세요.

0

안녕하세요^^

사실 저도 원리는 모릅니다. 그냥 왜워서 사용합니다.