09-散列3. Hashing - Hard Version (30)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
HE, Qinming

Given a hash table of size N, we can define a hash function H(x) = x%N. 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

提交代码

主要思路:

哈希表规模为n。对于输入的位置编号为i的元素a,如果:

1.i==a%n。说明a放i位置不是由于冲突,将a放入优先队列。

2.i>a%n。如果编号为a%n到n-1的元素之前已经填入哈希表(h1[j].exist=true),则第i个元素可以进入优先队列。否则,如果编号为a%n到n-1的元素在位置编号为i的元素填入之后填入哈希表,意味着位置编号为i的元素插入哈希表时,发生冲突向后转移不可能到达位置i,而应该在i位置之前填入,矛盾。

3.i<a%n。与2同理。

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
struct node{
bool exist;
node(){
exist=false;
}
};
node h1[];
priority_queue< pair<int,int>,vector<pair<int ,int> >,greater<pair<int,int> > > pq;//记录值和放的位置,按pair的第一个元素升序排列
pair<int,bool> input[];//标记元素有没有进入队列
queue<int> q;
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n,num,i,count=;
scanf("%d",&n);
for(i=;i<n;i++){
scanf("%d",&num);
input[i]=make_pair(num,false);
if(num>=){//记录不等于负数的元素个数
count++;
}
}
int j,k,post;
for(i=;i<count;i++){//复杂度n^2:扫数列count次,每次将当前满足条件的最小元素进入队列q
for(j=;j<n;j++){//遍历数列的每个元素
if(!input[j].second&&input[j].first>=){//寻找没有进入队列并且不等于-1的元素
post=input[j].first%n;//元素本来应该在哈希表中的位置
if(post==j){//本来要放的位置和现在在哈希表中的位置相同
pq.push(make_pair(input[j].first,j));
//h1[post].exist=true;//标记
input[j].second=true;
continue;
}
if(post<j){//本来要放的位置在现在的位置前面
for(k=post;k<j;k++){
if(!h1[k].exist){//前面没有元素填充
break;
}
}
if(k==j){//可以进队pq
pq.push(make_pair(input[j].first,j));
//h1[j].exist=true;
input[j].second=true;
}
}
else{//post>j
for(k=post;k<n;k++){//先由post开始向后遍历
if(!h1[k].exist){
break;
}
}
if(k==n){//满足条件后,由0开始向j遍历
for(k=;k<j;k++){
if(!h1[k].exist){
break;
}
}
if(k==j){
pq.push(make_pair(input[j].first,j));
//h1[j].exist=true;
input[j].second=true;
}
}
}
}
}
pair<int,int> top=pq.top();
pq.pop();
q.push(top.first);//找到本次满足条件:在队列pq且最小的元素,进入输出队列q
h1[top.second].exist=true;//意味着top.second位置已经放了元素
}
printf("%d",q.front());//对输出队列q进行输出
q.pop();
while(!q.empty()){
printf(" %d",q.front());
q.pop();
}
return ;
}

pat09-散列3. Hashing - Hard Version (30)的更多相关文章

  1. 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 ...

  2. 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 ...

  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. PTA 11-散列4 Hard Version (30分)

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

  5. 散列(Hash)表入门

    一.概述 以 Key-Value 的形式进行数据存取的映射(map)结构 简单理解:用最基本的向量(数组)作为底层物理存储结构,通过适当的散列函数在词条的关键码与向量单元的秩(下标)之间建立映射关系 ...

  6. Algorithms - Data Structure - Perfect Hashing - 完全散列

    相关概念 散列表 hashtable 是一种实现字典操作的有效数据结构. 在散列表中,不是直接把关键字作为数组的下标,而是根据关键字计算出相应的下标. 散列函数 hashfunction'h' 除法散 ...

  7. Hashing散列注意事项

    Hashing散列注意事项 Numba支持内置功能hash(),只需__hash__()在提供的参数上调用成员函数即可 .这使得添加对新类型的哈希支持变得微不足道,这是因为扩展APIoverload_ ...

  8. PTA 逆散列问题 (30 分)(贪心)

    题目链接:https://pintia.cn/problem-sets/1107178288721649664/problems/1107178432099737614 题目大意: 给定长度为 N 的 ...

  9. PAT A1145 Hashing - Average Search Time (25 分)——hash 散列的平方探查法

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

随机推荐

  1. [vscode] github travis 集成问题

    问题log $ npm install -npm ERR! 404 Not Found npm ERR! 404 npm ERR! 404 'types/mocha' is not in the np ...

  2. ping包,支持ip录入

    @echo off ::等待用户输入需要监控IP set /p ip=Input the IP required to monitor: echo executing...... :start ech ...

  3. angular 模板表单

  4. SSH的三个组件ssh、sftp、scp介绍

    SSH  包含3个组件 (1) ssh 远程登录节点 : ssh 用户名@IP地址 ① 不允许空密码或错误密码认证登录 ② 不允许root用户登录 ③ 有两个版本 ssh,ssh2安全性更高 (2)  ...

  5. pdo + 事务处理 处理线性事务

    /* * 事物处理线性操作. * 以转账为例 */ header('Content-type:text/html;charset=utf-8'); $opt = array(PDO::ATTR_PER ...

  6. Django权限控制进阶

    一.一级菜单的排序 我们用字典存放菜单信息,而字典是无序的,当一级菜单过多时可能会出现乱序情况,因此需要给一级菜单排序 1.给一级菜单表的model中加一个weight权重的字段 ,权重越大越靠前 w ...

  7. [原创]用WinRAR实现VC源代码快速压缩创建

    [原创]用WinRAR实现VC源代码快速压缩创建 by edata @ cnblogs.com/edata 2017-5-8 22:31:57 我们有的时候需要对vc项目压缩打包,因为vc项目的无关文 ...

  8. Ubuntu16.04LTS安装集成开发工具IDE: CodeBlocks 和Eclipse-cdt

    上文中,我们已经介绍了QT5.10.0在Ubuntu下的安装 https://www.cnblogs.com/si-lei/p/9240230.html, 接下来我们介绍CodeBlocks以及Ecl ...

  9. XAF实现交叉分析

    如何实现如图的交叉分析? In this lesson, you will learn how to add the Analysis functionality to your applicatio ...

  10. N1 Armbian 安装 OpenMediaVault

    前言 接上一篇继续折腾,这次在 N1 上进行一些本地化设置并安装使用 OpenMediaVault 步骤 使用 ssh 连接到 N1,修改系统源 cd /etc/apt cp sources.list ...