7-18 Hashing - Hard Version (30 分)

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.

知识点:

拓扑排序

priority_queue的用法:

  • priority_queue<int,vector<int>,greater<int> > q; 建立小顶堆
  • priority_queue<int,vector<int>,less<int> > q; 建立大顶堆

map 的一些用法:

  • 遍历一个 map  
    for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++){
    if(list[it->second]==){
    q.push(it->first);
    }
    }

思路:

这是一个拓扑排序问题。建立一个优先队列(最小堆)。寻找所有元素,将入度为0的入队,然后将所有以它为前置节点的项,入度都减1。

入度的计算:考虑到是线性探测,每个元素的入度就是(它目前在的位置 - 应该在的位置),如负,加hash表长。

用 map 建立每个数的索引,不然空间不够。

 #include <iostream>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
using namespace std;
const int maxn = ;
int n;
int a[maxn];
vector<int> zu[maxn];
priority_queue<int,vector<int>,greater<int> > q;
int list[maxn];
map<int,int> mp; int main(){
scanf("%d",&n);
fill(list,list+maxn,-);
for(int i=;i<n;i++){
scanf("%d",&a[i]);
if(a[i]<=-) continue;
mp[a[i]] = i;
list[i] = i-a[i]%n;
if(list[i]<) list[i]+=n;
}
for(int i=;i<n;i++){
if(a[i]<=-) continue;
int j=a[i]%n;
while(j!=i){
zu[mp[a[j]]].push_back(a[i]);
j+=;
if(j>=n) j-=n;
}
}
for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++){
if(list[it->second]==){
q.push(it->first);
}
}
//printf("12 %d\n",list[12]);
vector<int> out;
while(q.size()){
int tmp = q.top();
q.pop();
out.push_back(tmp);
for(int i=;i<zu[mp[tmp]].size();i++){
list[mp[zu[mp[tmp]][i]]]--;
//printf("32: %d\n",list[mp[32]]);
if(list[mp[zu[mp[tmp]][i]]]==){
q.push(zu[mp[tmp]][i]);
}
}
}
for(int i=;i<out.size();i++){
if(i!=) printf(" ");
printf("%d",out[i]);
}
}

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

7-18 Hashing - Hard Version的更多相关文章

  1. pat09-散列3. Hashing - Hard Version (30)

    09-散列3. Hashing - Hard Version (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HE, Qin ...

  2. Hashing - Hard Version

    Hashing - Hard Version Given a hash table of size N, we can define a hash function . Suppose that th ...

  3. 2017 FVDI2 ABRITES Commander with 18 Softwares FULL Version + FLY OBD Terminator + J2534 DrewTech Softwares

    Highlights of FVDI2 Abrites Commander Full Version: 1.Free update online. 2.This is full version FVD ...

  4. 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 基本思路 ...

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

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

  7. PTA 11-散列4 Hard Version (30分)

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

  8. python 2.7.10 找不到 libmysqlclient.18.dylib 解决方案

    Mac os x 升级到最新版后出现 python MysqlDB 无法找到 libmysqlclient.18.dylib 的问题,尝试的解决方案如下: 1.  升级更新 mysql 到最新版,无效 ...

  9. 第18课 类型萃取(2)_获取返回值类型的traits

    1. 获取可调用对象返回类型 (1)decltype:获取变量或表达式的类型(见第2课) (2)declval及原型 ①原型:template<class T> T&& d ...

随机推荐

  1. android 开发概述以及相关背景知识

    参考链接:http://www.runoob.com/android/android-architecture.html http://www.runoob.com/android/android-a ...

  2. 在threejs中添加两个场景和相机是需要注意render的参数设置

    问题:我刚开始设置了两个场景和相机 但是第二个一直将第一个场景给覆盖了一直找不到原因 解决: 问题出在 renderer.autoClear = false;上 设置render的参数如下: rend ...

  3. 长时间没有操作putty就会断开连接是怎么回事?

    seconds between keepalives 设置为10就好了, 这个值有什么含义,服务器为了节省资源采取了一些措施,其中一条就是如果检测一个会话(session)几分钟或者几小时没有数据流入 ...

  4. 递归函数 day17

    一 递归函数 n = 1 金老板 38+2 =40n = 2 alex n+2= 金老板 36+2 = 38n = 3 wusir n+2 = alex wusir 36 def age(n): #n ...

  5. Centos7上卸载openJdk安装,安装自己的JDK1.8

    1.下载jdk-1.8,官网下载1.8 注意:不要使用wget命令去下载jdk,若能够下载下来,解压的时候也会报错的: 2.卸载openjdk (1)使用  rpm -qa | grep java  ...

  6. SVN查看最新几次提交日志的命令

    Windows下,使用svn客户端查看日志很方便,linux下查看的记录条数的小技巧: 1>查看某个目录下的所有日志记录 svn log [PATH] 上面的方式,对于命令行下查看时是很不方便的 ...

  7. 一种基于URL数据源的WEB报表插件

    完全支持所见所得的报表设计, 支持 PHP ,Java 等所有支持JSON格式的后端. 立即下载测试版本 需要正式版本?请QQ联系:1565498246 或者留言

  8. Eclipse的下载及安装

    Eclipse的下载地址: https://www.eclipse.org/downloads/ 下载完成后,双击安装包即可安装 选择 Eclipse IDE for Java EE Decelope ...

  9. Spring Environment(二)源码分析

    Spring Environment(二)源码分析 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Envi ...

  10. web前端面试题库

    web前端面试题及答案   1.常用那几种浏览器测试?有哪些内核(Layout Engine)? 答: (Q1) 浏览器:IE,Chrome,FireFox,Safari,Opera.    (Q2) ...