7-18 Hashing - Hard Version
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的更多相关文章
- pat09-散列3. Hashing - Hard Version (30)
09-散列3. Hashing - Hard Version (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HE, Qin ...
- Hashing - Hard Version
Hashing - Hard Version Given a hash table of size N, we can define a hash function . Suppose that th ...
- 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 ...
- 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 基本思路 ...
- 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 (30 分)
Given a hash table of size N, we can define a hash function H(x)=x%N. Suppose that the linear probin ...
- PTA 11-散列4 Hard Version (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/680 5-18 Hashing - Hard Version (30分) Given ...
- python 2.7.10 找不到 libmysqlclient.18.dylib 解决方案
Mac os x 升级到最新版后出现 python MysqlDB 无法找到 libmysqlclient.18.dylib 的问题,尝试的解决方案如下: 1. 升级更新 mysql 到最新版,无效 ...
- 第18课 类型萃取(2)_获取返回值类型的traits
1. 获取可调用对象返回类型 (1)decltype:获取变量或表达式的类型(见第2课) (2)declval及原型 ①原型:template<class T> T&& d ...
随机推荐
- [z]cxf生成javaclient
\apache-cxf-3.1.6\bin>wsdl2java -client http://192.168.129.224:8000/PricingEngine?wsdl
- sqlserver 分区排序之partition
例如:按照课程分组取各个课程最高成绩的记录,使用partition分区,然后按照成绩倒序排列,需要注意的是考虑到可能出现多个相同最高分,使用dense_rank来实现连续排序. 参考链接:https: ...
- javascript 重构alert()
javascript问题,关于重构window.alert()后,然后调用window原本的window.alert()的方法 大神们,问个问题,如果在script标签的第一行散写,重构了window ...
- div中的img垂直居中的方法,最简单! 偷学来的,,,不要说我抄袭啊(*^__^*)
让div中的img垂直居中,水平居中很简单,用text-align:center; 让div中img垂直居中的方法其实也很简单 重点是: display:table-cell; 让标签具有表格的属 ...
- linux操作系统-两台linux服务器SSH免密码登录
A为本地主机(即用于控制其他主机的机器) ; B为远程主机(即被控制的机器Server), ip为192.168.100.247 ; A和B的系统都是Linux 在A上的命令 # ssh-keyg ...
- HDU_1142(最短路 + dfs)
Jimmy experiences a lot of stress at work these days, especially since his accident made working dif ...
- [转]slf4j 与log4j 日志管理
log4j简易入门 package test.log4j; import org.apache.log4j.Logger; public class HelloLog4j { private stat ...
- Python中的类方法、实例方法、静态方法
类方法 @classmethod 在python中使用较少,类方法传入的第一个参数是 cls,是类本身: 类方法可以通过类直接调用或者通过实例直接调用,但无论哪种调用方式,最左侧传入的参数一定是类本身 ...
- Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用
Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019 ...
- linux上的工具或软件
1.下载软件 yum install axelaxel http://mirror.cse.iitk.ac.in/archlinux/iso/2015.04.01/archlinux-2015.04. ...