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 ...
随机推荐
- HTTP协议介绍(POST、GET、Content-Type)
什么是HTTP?超文本传输协议(HyperText Transfer Protocol -- HTTP)是一个设计来使客户端和服务器顺利进行通讯的协议.HTTP/1.1 协议规定的 HTTP 请求方法 ...
- avalon 双向绑定在新版chrome中不能同步中文输入
1>1.x和2.x都有这样的问题,输入中文无法同步到VM,演示地址 http://codepen.io/roscoe054/pen/XXKYMj?editors=1111 chrome 版本 5 ...
- ubuntu 无法挂载U盘
问题描述: usb 1-1: device descriptor read/64,error 18usb 1-1: device descriptor read/64,error 18usb 1-1: ...
- sql建立一种,自定义的执行作业
USE [chongwu] GO /****** Object: StoredProcedure [dbo].[p_createjob] Script Date: 01/21/2016 14:32:0 ...
- linux执行系统命令时挂起
现象:使用mock构建时出现挂起现象 1.排除内存不足和构建工作空间所在磁盘分区不足情形: 2.执行任何系统命令异常卡顿 原因: 1.系统根分区空间严重不足: 解决办法: 清理根分区无用文件 1> ...
- Luogu 2575 高手过招-SG函数
Solution SG函数跑一遍就过了ouo Code #include<cstring> #include<cstdio> #include<algorithm> ...
- swift 总结
结构体(struct)属于值类型, 当值类型的实例被声明为常量的时候,它的所有属性也就成了常量. 属于引用类型的类(class)则不一样.把一个引用类型的实例赋给一个常量后,仍然可以修改该实例的变量属 ...
- Spring ApplicationContext(一)初始化过程
Spring 容器 ApplicationContext(一)初始化过程 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) ...
- 【附源文件】软件工具类Web原型制作分享 - Sketch
Sketch是一款轻量,易用的矢量设计工具.专门为UI设计师开发,让UI设计更简单.更高效. 本原型由国产原型工具-Mockplus制作完成. 非常适合工具类产品官网使用,本模板的交互有通过使用面板组 ...
- zookeeper相关
1.zookeeper应用:集群节点间的数据同步(资源管理),分布式锁(主要是利用客户端在一个会话中在zookeeper中创建一个znode节点,然后再去执行自己的业务代码,比如去更新数据库,其他客户 ...