Hashing - Hard Version

Given a hash table of size N, we can define a hash function . Suppose that the linear probing is used to solve collisions, we can easily obtain the status of the hash table with a given sequence of input numbers.

However, now you are asked to solve the reversed problem: reconstruct the input sequence from the given status of the hash table. Whenever there are multiple choices, the smallest number is always taken.

Input Specification:

Each input file contains one test case. For each test case, the first line contains a positive integer N (≤1000), which is the size of the hash table. The next line contains N integers, separated by a space. A negative integer represents an empty cell in the hash table. It is guaranteed that all the non-negative integers are distinct in the table.

Output Specification:

For each test case, print a line that contains the input sequence, with the numbers separated by a space. Notice that there must be no extra space at the end of each line.

Sample Input

11
33 1 13 12 34 38 27 22 32 -1 21

Sample Output:

1 13 12 21 33 34 38 27 22 32

分析

参考Messier

可以使用拓扑排序来解这道题。基本思路如下:将输入保存在散列表后,遍历每个元素,如果元素刚好在它对应余数的位置上,则入度为0,可直接输出;否则,从余数位置出发,用线性探测法到达该位置,对于经过的所有的非空元素位置,生成一条到该元素位置的边,并将该位置入度加1;拓扑排序时,可以采用优先队列,优先输出数值较小的元素

代码如下

#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
vector<int> G[1000]; // 连接表
int N;
int indegree[1000]={0}; // 入度
int a[1000];
struct mycompare{ // 为优先队列自定义操作
bool operator()(int m,int n){
return a[m]>a[n];
}
};
void toposort(){ // 拓扑排序
int tag=0;
priority_queue<int,vector<int>,mycompare> q;
for(int i=0;i<N;i++)
if(indegree[i]==0&&a[i]>=0)
q.push(i);
while(!q.empty()){
int t=q.top();
if(tag++==0) cout<<a[t];
else cout<<" "<<a[t];
q.pop();
for(int i=0;i<G[t].size();i++){
int v=G[t][i];
indegree[v]--;
if(indegree[v]==0)
q.push(v);
}
}
}
int main(){
cin>>N;
for(int i=0;i<N;i++)
cin>>a[i];
for(int i=0;i<N;i++){ // 建图
if(a[i]%N==i||a[i]<0)
continue;
else
{
int t=a[i]%N;
while(t!=i){
G[t].push_back(i);
indegree[i]++;
t=(t+1)%N;
}
}
}
toposort();
return 0;
}

最新文章

  1. PHP计算程序运行时间的类
  2. asp.net mvc本地程序集和GAC的程序集冲突解决方法
  3. 【ros】.bag文件
  4. [Java] 解决spring的xml标签内不能自由增加说明的难题,方便调试、部署时进行批量屏蔽
  5. java用代理访问
  6. ImageView中XML属性src和background的区别
  7. android学习笔记31——ADB命令
  8. coffeeScript 语法总结
  9. Java中finalize方法用途何在?
  10. 用户控件(.ascx)与&lt;ul&gt;&lt;li&gt;以及&lt;a&gt;布局之小结
  11. hdu4105  Electric wave
  12. Lorenzo Von Matterhorn
  13. javascript中类式继承和原型式继承的实现方法和区别
  14. 编写高质量代码:改善Java程序的151个建议(第一章:JAVA开发中通用的方法和准则)
  15. Fiddler显示服务器IP的方法
  16. v-bind特性
  17. Ubuntu18.04格式化U盘为NTFS的方法
  18. Linux----------rsync的介绍及安装使用
  19. 【UOJ#422】【集训队作业2018】小Z的礼物(min-max容斥,轮廓线dp)
  20. [Tensorflow] **Android Meets TensorFlow

热门文章

  1. 24. [Ext JS 4] 实战之Load Mask(加载遮罩)的显示与隐藏
  2. linux守护进程的编写
  3. 数学 FZU 2074 Number of methods
  4. WCF 相关配置
  5. A8ERP配送管理系统
  6. Django--1、MTV及基本应用
  7. Selenium IDE的第一个测试用例——路漫长。。。
  8. DatePickerDialog日期对话框以及回调函数的用法
  9. (转)SqlServer2008开启客户端远程连接
  10. 一个完整的网站记录(springmvc hibernate juery bootstrap)