开散列。当产生重复映射时,在这个位置维护一个链表来记录这些值。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
const int N = 20000;
const int HASH = 13331;
struct P
{
int val;
int cnt = 0;
P* ne;
P(int val, P* ne):val(val), ne(ne){}
};
P* head[N];
bool cmp(pii a, pii b){
return a.first < b.first;
}
void init(){
for(int i = 0; i < N; i++) head[i] = nullptr;
}
int H(int x){
return (x % HASH) + 1;
}
void insert(int x){
P* cur = head[H(x)];
bool fg = true;
while(cur != nullptr){
if(cur->val == x){
fg = false;
break;
}
cur = cur->ne;
}
if(fg){
cur = new P(x, head[H(x)]);
head[H(x)] = cur;
}
cur->cnt++;
}
int main(){
init();
int n;
cin >> n;
for(int i = 0; i < n; i++){
int x;
cin >> x;
insert(x);
}
vector<pii>res;
for(int i = 0; i <= HASH + 1; i++){
P* cur = head[i];
while(cur != nullptr){
res.push_back({cur->val, cur->cnt});
cur = cur->ne;
}
}
sort(res.begin(), res.end(), cmp);
for(auto it : res){
cout << it.first << ' ' << it.second<<endl;
}
}