题意: 给出了hashing的结果,而且是用的linear probing线性探测解决冲突,要求给出初始序列。负数直接跳过,序列中的数都是非负数。所以遍历给出序列,如果说当前位置的s[i] % n等于i那么就是说不存在冲突,如果不相等就是存在冲突的,经过线性探测才存在当前位置,所以从s[i] % n位置到i之前的数都是比是s[i]先进行操作的,如此我们可以找到序列中数的顺序,进行拓扑排序,如果存在多种可能要求输出最小的,所以用优先队列
tip:优先队列+拓扑排序
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int n,s[1000],l[1000],flag;
vector<int> v[1000];
struct cmp {
bool operator () (const int &a,const int &b) {
return s[a] > s[b];
}
};
int main() {
scanf("%d",&n);
for(int i = 0; i < n; ++i) {
scanf("%d",&s[i]);
}
priority_queue<int,vector<int>,cmp> q;
for(int i = 0; i < n; ++i) {
if(s[i] < 0) continue;
if(s[i] % n != i) {
int d = s[i] % n > i ? i + n : i;//确定hash值初始时在i的那边
for(int j = s[i] % n; j < d; ++j) {
if(s[j % n] < 0) continue;
v[j % n].push_back(i);
l[i] ++;
}
} else q.push(i);
}
while(!q.empty()) {
int temp = q.top();
q.pop();
if(flag) putchar(' ');
else flag = 1;
printf("%d",s[temp]);
for(int i = 0; i < v[temp].size(); ++i) {
int d = v[temp][i];
l[d] --;
if(!l[d]) q.push(d);
}
}
return 0;
}