pat09-散列3. Hashing - Hard Version (30)
09-散列3. 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 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)的更多相关文章
- 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 ...
- 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
题目 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 基本思路 ...
- PTA 11-散列4 Hard Version (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/680 5-18 Hashing - Hard Version (30分) Given ...
- 散列(Hash)表入门
一.概述 以 Key-Value 的形式进行数据存取的映射(map)结构 简单理解:用最基本的向量(数组)作为底层物理存储结构,通过适当的散列函数在词条的关键码与向量单元的秩(下标)之间建立映射关系 ...
- Algorithms - Data Structure - Perfect Hashing - 完全散列
相关概念 散列表 hashtable 是一种实现字典操作的有效数据结构. 在散列表中,不是直接把关键字作为数组的下标,而是根据关键字计算出相应的下标. 散列函数 hashfunction'h' 除法散 ...
- Hashing散列注意事项
Hashing散列注意事项 Numba支持内置功能hash(),只需__hash__()在提供的参数上调用成员函数即可 .这使得添加对新类型的哈希支持变得微不足道,这是因为扩展APIoverload_ ...
- PTA 逆散列问题 (30 分)(贪心)
题目链接:https://pintia.cn/problem-sets/1107178288721649664/problems/1107178432099737614 题目大意: 给定长度为 N 的 ...
- 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 ...
随机推荐
- SQL Server 2012自动备份
SQL 2012和2008一样,都可以做维护计划,来对数据库进行自动的备份. 现在做这样一个数据库维护的计划,每天0点对数据库进行差异备份,每周日0点对数据库进行完全备份,并且每天晚上10点删除一次过 ...
- xmlreader与xmlwriter里的几个坑与解决方案
加载超过100M的xml文件时(可能不是很常见),XmlDocument这种全部加载到内存里的模式就有点不友好了,耗时长.内存高. 这时用xmlreader就会有自行车换超跑的感觉,但其间遇到几个坑, ...
- 【C#】MVC+EF+LINQ 综合小项目
第一,创建数据库 create table category(id int primary key,name nvarchar(20)) create table news(id int primar ...
- c++缓冲区------c++ Primer Plus
通常,通过使用缓冲区可以更高效地处理输入和输出.缓冲区是用作中介的内存块,它是将信息从设备传输到程序或从程序传输给设备的临时存储工具.通常,像硬盘驱动器这样的设备以512字节(或更多)的块为单位来传输 ...
- 第一篇 Python的数据类型
Python的标准数据类型有五种: (1)字符串 (2)数字(包括整数,浮点数,布尔,复数) (3)列表(list) (4)元组(tuple) (5)字典(dict) 注:使用type函数可以查看对象 ...
- <id name="ID"> <generator class="assigned" /> </id>
<generator>表示一个主键的生成机制.即具体通过何种方式来生成.生成方式包括:increment:生成long, short或者int类型的主键,不能在cluster环境下使用.适 ...
- Maven配置与安装
最近重装了一下系统,便重新安装与配置了maven,记录这个过程并分享出来. 注意:maven安装需要Java依赖,我这里使用的是jdk1.8. 1.安装并配置环境变量 首先在 maven 官网下载 m ...
- postgreSQL PL/SQL编程学习笔记(四)
Errors and Messages 1. Reporting Errors and Messages Use the RAISE statement to report messages and ...
- JS中Math函数的常用方法
Math 是数学函数,但又属于对象数据类型 typeof Math => ‘object’ console.dir(Math) 查看Math的所有函数方法. 1,Math.abs() 获取绝对值 ...
- 解析图书 XML
Java代码: package com.thinkgem.jeesite.test; import org.dom4j.Attribute; import org.dom4j.Document; im ...