-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy path7-42_整型关键字的散列映射 (25).cpp
44 lines (43 loc) · 1.15 KB
/
7-42_整型关键字的散列映射 (25).cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n, p, e;
bool first = true;
cin >> n >> p;
vector<int> elems(p, -1);
unordered_map<int, bool> vis; // duplicate elements regard as same
unordered_map<int, int> pos;
for (int i = 0; i < n; i++) {
cin >> e;
int key = e % p;
if (vis[e]) cout << " " << pos[e];
else {
if (elems[key] == -1) {
elems[key] = e;
vis[e] = true;
pos[e] = key;
if (first) {
first = false;
cout << key;
} else {
cout << " " << key;
}
} else {
for (int j = e + 1;; j++) {
if (elems[j % p] == -1) {
cout << " " << j % p;
elems[j % p] = e;
vis[e] = true;
pos[e] = j % p;
break;
}
}
}
}
}
cout << endl;
return 0;
}