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;
}

Hashing - Hard Version的更多相关文章

  1. 7-18 Hashing - Hard Version

    7-18 Hashing - Hard Version (30 分) Given a hash table of size N, we can define a hash function . Sup ...

  2. pat09-散列3. Hashing - Hard Version (30)

    09-散列3. Hashing - Hard Version (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HE, Qin ...

  3. 11-散列4 Hashing - Hard Version

    题目 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 基本思路 ...

  4. 11-散列4 Hashing - Hard Version (30 分)

    Given a hash table of size N, we can define a hash function (. Suppose that the linear probing is us ...

  5. 11-散列4 Hashing - Hard Version (30 分)

    Given a hash table of size N, we can define a hash function H(x)=x%N. Suppose that the linear probin ...

  6. PTA 11-散列4 Hard Version (30分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/680 5-18 Hashing - Hard Version   (30分) Given ...

  7. 中国大学MOOC-陈越、何钦铭-数据结构-2017春

    中国大学MOOC-陈越.何钦铭-数据结构-2017春 学习地址 详细学习内容 Github记录地址 欢迎fork和star,有惊喜值得学习! 参考学习笔记 参考AC代码 数据结构和算法学习笔记 学习内 ...

  8. A Universally Unique IDentifier (UUID) URN Namespace

    w Network Working Group P. Leach Request for Comments: 4122 Microsoft Category: Standards Track M. M ...

  9. ASP.NET Core搭建多层网站架构【2-公共基础库】

    2020/01/28, ASP.NET Core 3.1, VS2019,Newtonsoft.Json 12.0.3, Microsoft.AspNetCore.Cryptography.KeyDe ...

随机推荐

  1. obs nginx-rtmp-module搭建流媒体服务器实现直播 ding

    接下来我就简单跟大家介绍一下利用nginx来搭建流媒体服务器. 我选择的是腾讯云服务器 1.下载nginx-rtmp-module: nginx-rtmp-module的官方github地址:http ...

  2. ODB(C++ ORM)用Mingw的完整编译过程

    用mingw官方的GCC4.7.2编译libodb后,并用odb compiler对hello示例生成odb的"包裹"代码,编译链接总是不能通过,下面是编译example/hell ...

  3. gerrit+gitlab整合调试

  4. 【149】ArcGIS Desktop 10.0 & Engine 10.0 安装及破解

    写在前面:可能会出现按照此方法无法破解的情况,那请确保您有将 ArcGIS 10.0 已经完全卸载干净,直接通过控制面板进行卸载的时候并不能将其卸载干净,需要进行更深层次的卸载,包括删除注册表,各种文 ...

  5. fck 属性配置大全

    优化FCKeditor文件夹和文件: 下载FCKeditor并解压之后,会产生_samples和 editor两个文件夹和几个文件,全部删除以_开头的文件夹和文件,因为这些都是FCKeditor的一些 ...

  6. bzoj1051受欢迎的牛(Tarjan)

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4776  Solved: 2542 Description ...

  7. python中多线程(1)

    一多线程的概念介绍 threading模块介绍 threading模块和multiprocessing模块在使用层面,有很大的相似性. 二.开启多线程的两种方式 1.创建线程的开销比创建进程的开销小, ...

  8. sublime 3 最新注册码

    http://9iphp.com/web/html/sublime-text-3-license-key.html

  9. UNICODE本地编译freescale的i.MX6Q的android4.2.2&android4.4.2 && 全志a80的步骤x1

    20151031本地编译freescale的i.MX6Q的android4.2.2&android4.4.2 && 全志a80的步骤x1 2015/10/31 15:07 开始 ...

  10. C# HttpWebRequest Post Get 请求数据

    Post请求 1 //data 2 string cookieStr = "Cookie信息"; 3 string postData = string.Format("u ...