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. angular 重定向路由

    const routes: Routes = [ { path: '', redirectTo: '/home', pathMatch: 'full' }, { path: 'home', compo ...

  2. 关于MultiDataTrigger和MultiTrigger的一些注意事项

    他俩有着相同的语法. 都是在conditions中编写触发条件. 因为都是同一个触发类. 在conditions中有Property和Binding这两个属性.那么这两个可以同时使用吗?当然是不可以的 ...

  3. 趣图:后端工程师是怎样调试CSS的

      一大波趣图:CSS的力量 趣图:前端 VS 后端

  4. Mysql高性能优化规范建议

    数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据库对象名称禁止使用mysql保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) 数据库对象的命名要能做到见名识意 ...

  5. Spring Boot多数据源

    我们在开发过程中可能需要用到多个数据源,我们有一个项目(MySQL)就是和别的项目(SQL Server)混合使用了.其中SQL Server是别的公司开发的,有些基本数据需要从他们平台进行调取,那么 ...

  6. 【bzoj5093】 [Lydsy1711月赛]图的价值 组合数+斯特林数+NTT

    Description "简单无向图"是指无重边.无自环的无向图(不一定连通). 一个带标号的图的价值定义为每个点度数的k次方的和. 给定n和k,请计算所有n个点的带标号的简单无向 ...

  7. 【bzoj3601】一个人的数论 莫比乌斯反演+莫比乌斯函数性质+高斯消元

    Description Sol 这题好难啊QAQ 反正不看题解我对自然数幂求和那里是一点思路都没有qwq 先推出一个可做一点的式子: \(f(n)=\sum_{k=1}^{n}[(n,k)=1]k^d ...

  8. Bootstrap 基本css样式

    1.标题1级标题<h1> 38px 是默认大小的2.7倍2级标题<h2> 32px 是默认大小的2.25倍3级标题<h3> 24px 是默认大小的1.70倍4级标题 ...

  9. 微信支付的SDK曝出重大漏洞(XXE漏洞)

    一.背景 昨天(2018-07-04)微信支付的SDK曝出重大漏洞(XXE漏洞),通过该漏洞,攻击者可以获取服务器中目录结构.文件内容,如代码.各种私钥等.获取这些信息以后,攻击者便可以为所欲为,其中 ...

  10. 关于Execel 2007 连接到 hive odbc

    官方给出的都是 2010 或2012版的连接方案,看起来与2007有些不同,但我相信这些功能都是通用的. 下载 odbc hive 驱动.http://hortonworks.com/products ...