[백준] 2910 빈도 정렬

0

백준

목록 보기
229/271
post-thumbnail

[백준] 2910 빈도 정렬

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

typedef long long ll;
map <ll, int> cnt;

bool cmp(ll a, ll b){
	return cnt[a] > cnt[b];
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int N, C;
	cin >> N >> C;

	vector<ll> vec;

	for (int i = 0; i < N; ++i) {
		ll input;
		cin >> input;

		if (cnt.find(input) == cnt.end()) {
			cnt[input] = 1;
			vec.push_back(input);
		}
		else {
			cnt[input] = cnt[input] + 1;
		}
	}

	stable_sort(vec.begin(), vec.end(), cmp);

	for (int i = 0; i < vec.size(); ++i) {
		for (int j = 0; j < cnt[vec[i]]; ++j) {
			cout << vec[i] << " ";
		}
	}
	return 0;
}

profile
Be able to be vulnerable, in search of truth

0개의 댓글