반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

코딩하기 좋은날

백준 2910 빈도 정렬 본문

백준(Baekjoon) 문제

백준 2910 빈도 정렬

huiung 2019. 1. 14. 14:07
반응형

문제와 채점은 위 사이트에서 확인 하실 수 있습니다.

 

이 문제는 list를 사용해서 값들을 입력받고 존재하는 값과 그 값의 빈도를 multimap 에다 저장 하였습니다. 

확인한 값은 remove로 다제거 시키기 위해서 list를 사용하였고 

처음에 map을 사용해서 틀렸었는데 동일 빈도인 경우가 존재하므로 multimap을 사용해서 맞았습니다.

 

 

#include <iostream>
#include <list>
#include <map>
#include <vector>

using namespace std;

int main(void) {
	list<int> l;
	multimap<int, int , greater<int> > m;
	int N,C; // N은 메시지 길이  C는 상수 
	cin>>N>>C;
	int num;
	int i;
	vector<int> v;
	
	while(N) { //리스트에 값 넣기 
		int x;
		cin>>x;
		l.push_back(x);
		N--;
	}
	
	while(!l.empty()) { //리스트가 빌때까지 반복 
		i = 0;
	for(auto n : l) { //리스트의 front원소를 기준으로 몇번 나오는지 확인 
		num = l.front();
		if(n == num)
			i++;
	}
	m.insert(make_pair(i, num)); //빈도와 값을 멀티맵에 삽임 
	l.remove(num); //리스트의 해당 값 모두 제거 
	}

	for(auto it = m.begin(); it != m.end(); it++) //리스트의 빈도수만큼 벡터에 값 삽입 
		for(int i = 0; i < it->first; i++)
			v.push_back(it->second);
		
	for(auto n : v) //출력 
		cout<<n<<" ";
	
	return 0;
}
반응형