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 ...
随机推荐
- Vue 自定义header
第一种,全局设置: Vue.http.headers.common['token'] = 'YXBpOnBhc3N3b3Jk'; 第二种,拦截器设置 Vue.http.interceptors.pus ...
- kali linux之sqlmap
一款开源的命令行自动SQL注入工具,它能够对多种主流数据库进行扫描支持,基于Python环境. 检测动态页面中get/post参数,cookie,http头 数据榨取/文件系统访问 操作系统命令执行 ...
- python基础之条件判断
一.简单的if语句 举例 if money > 10000: print '我们去旅游吧' #左侧一定要有缩进,一般4个空格 print '请输入学生的考试成绩' score = print ( ...
- eclipse中查看java源码时,出现source not found问题
- 堆排序 思想 JAVA实现
已知数组 79.52.48.51.49.34.21.3.26.23 ,请采用堆排序使数组有序. “什么是堆” 堆是一颗完全二叉树,N层完全二叉树是一颗,除N-1层外其节点数都达到最大,且第N层子节点全 ...
- javascript jquery console调试方法说明
控制台(Console)是Firebug的第一个面板,也是最重要的面板,主要作用是显示网页加载过程中产生各类信息. 一.显示信息的命令 Firebug内置一个console对象,提供5种方法,用来显示 ...
- FPGA基础学习(3) -- 跨时钟域处理方法
文章主要是基于学习后的总结. 1. 时钟域 假如设计中所有的触发器都使用一个全局网络,比如FPGA的主时钟输入,那么我们说这个设计只有一个时钟域.假如设计有两个输入时钟,如图1所示,一个时钟给接口1使 ...
- requests库安装
1.运行cmd输入pip install requests C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Scripts ...
- python基础语法之基础语法规则以及设置
1. 编码格式 在python3以上版本中,py文件默认采用UTF-8格式编码,所有的字符串都是unicode字符串.当然,我们也可以自己为源码文件指定不同的编码,以utf-8为例,相关代码如下所示: ...
- redis设置允许远程连接
#redis默认只允许本地访问# #根据自己的配置文件启动,linux环境下同理# #远程访问的话需要设置配置文件,在redis安装根目录下xxx.config# #修改三个地方# 地址绑定 保护模式 ...