Untitled
unknown
plain_text
5 years ago
1.3 kB
4
Indexable
#include <iostream>
#include <map>
#include <algorithm>
using std::cin;
using std::cout;
const int M = 1e5;
std::map<int, int> mymap;
bool func(int a, int b) {
return mymap[a] > mymap[b];
}
void testcase(int* a, int n) {
//stable sort bao luu thu tu xuat hien va sap xep theo hang func
std::stable_sort(a, a+n, func);
//in ra cac gia tri
for (int i = 0; i < n; ++i)
for (int j = 0; j < mymap[a[i]]; ++j)
cout << a[i] << " ";
cout << "\n";
}
int main() {
int t;
cin >> t;
while (t--) {
int n, a[M+1];
mymap.clear();
cin >> n;
int cnt = 0;
for (int i = 0; i < n; ++i) {
//doc gia tri v
int v;
cin >> v;
//neu v chua co mat thi them vao mang a
if (mymap[v] == 0) {
a[cnt] = v;
++cnt;
mymap[v] = 1;
} else
++mymap[v]; //tang tan suat xuat hien v
//vd input: 8 5 8 5 2 3
//kq mang a: 8 5 2 3
//ket qua mymap:
//8: 2
//5: 2
//2: 1
//3: 1
}
testcase(a, cnt);
}
}Editor is loading...