Hashing - Hard Version
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的更多相关文章
- 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 ...
- pat09-散列3. Hashing - Hard Version (30)
09-散列3. Hashing - Hard Version (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HE, Qin ...
- 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 基本思路 ...
- 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 ...
- 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 ...
- PTA 11-散列4 Hard Version (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/680 5-18 Hashing - Hard Version (30分) Given ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2017春
中国大学MOOC-陈越.何钦铭-数据结构-2017春 学习地址 详细学习内容 Github记录地址 欢迎fork和star,有惊喜值得学习! 参考学习笔记 参考AC代码 数据结构和算法学习笔记 学习内 ...
- A Universally Unique IDentifier (UUID) URN Namespace
w Network Working Group P. Leach Request for Comments: 4122 Microsoft Category: Standards Track M. M ...
- ASP.NET Core搭建多层网站架构【2-公共基础库】
2020/01/28, ASP.NET Core 3.1, VS2019,Newtonsoft.Json 12.0.3, Microsoft.AspNetCore.Cryptography.KeyDe ...
随机推荐
- 14. extjs中treepanel属性和方法
转自:http://www.cnblogs.com/connortang/p/4414907.html 1.Ext.tree.TreePanel 主要配置项: root:树的根节点. rootVisi ...
- contesthunter 6201 走廊泼水节【克鲁斯卡尔+并查集】
很有意思的题,所以还是截lyddalao的课件 #include<iostream> #include<cstdio> #include<algorithm> us ...
- P4556 [Vani有约会]雨天的尾巴(线段树合并)
传送门 一道线段树合并 首先不难看出树上差分 我们把每一次修改拆成四个,在\(u,v\)分别放上一个,在\(lca\)和\(fa[lca]\)各减去一个,那么只要统计一下子树里的总数即可 然而问题就在 ...
- 分布式消息通信(ActiveMQ)
分布式消息通信(ActiveMQ) 应用场景 异步通信 应用解耦 流量削峰 # ActiveMQ安装 下载 http://activemq.apache.org/ 压缩包上传到Linux系统 apac ...
- NetCore Netty 框架 BT.Netty.RPC 系列随讲 —(前序) REST API 与 RPC 经典网络基础服务架构
在服务体系架构内,我们所知道的,有两种请求模型: Http 请求模型,以及 RPC 请求模型.因此,在一个互联网请求模型架构上,都是这两种的请求模型的向互组合. 下面给出两种常见的互联网经典基础架构图 ...
- [ SPOJ RESTACK ] Restacking haybales
\(\\\) Description 给出一个环,每个位置有一个初值 \(A_i\),有一个目标值 \(B_i\),保证 \(\sum A_i=\sum B_i\) 每个位置只能把值分给隔壁的,每次分 ...
- [ CodeForces 1059 D ] Nature Reserve
\(\\\) \(Description\) 你现在有\(N\)个分布在二维平面上的整点\((x_i,y_i)\),现在需要你找到一个圆,满足: 能够覆盖所有的给出点 与\(x\)轴相切 现在需要你确 ...
- 关于加减和es6
console.log(1+ "2"+"2"); //对于加法来说,如果只有一个操作数是字符串,则将另一个操作数也转换为字符串,然后将两者拼接,为122 c ...
- 关于FLASK WEB开发8d 数据库迁移的问题
首先, 第一步,要删除data-dev.sqlite这个数据库 第二步,进行下面的重建 暂时的解决办法是: python manage.py shell In [2]: from app import ...
- MySQL——基本安装与使用
基本安装 下载地址:https://dev.mysql.com/downloads/mysql/ 选择解压版本:mysql-5.7.21-winx64.zip 以管理员身份打开cmd(除了安装服务不要 ...