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 ...
随机推荐
- CSS背景background图片
一.CSS背景background图片 - TOP 1.背景图片语法background-image:url() 引入背景图片background-repeat:no-repeat 设置背景图 ...
- 9.4-9.19 h5日记总结
总结: 1.标签 (1)单.双标签 (2)块级.行级标签 (3)标签的属性 2.CSS (1)选择器 *.id.class.标签.后代.子代.并集.交集.伪类.结构 (2)层叠性,即选择器权重的计算 ...
- c# 对list 操作的写法总结
1:统计list 内重复值的数量 List<, , , , , , , }; var g = list.GroupBy(i => i); foreach (var item in g) { ...
- istio promethus收集不到数据
1.重新apply istio.yaml kubectl apply -f install/kubernetes/istio.yaml kubectl get metrics.config.istio ...
- MFC动态按钮的创建及其消息响应(自定义消息)
动态按钮(多个)的创建: 1.在类中声明并定义按钮控件的ID #define IDC_D_BTN 10000 2.在类的OnInitDialog()函数中动态创建按钮(建立按钮对象时最好建立对象的指针 ...
- ubuntu12.04下安装Apache+PHP+MySQL
一.Apache1.安装apache2: sudo apt-get install apache2 2.重启apache2: sudo /etc/init.d/apache2 restart 3.在浏 ...
- LibreOJ 6004. 「网络流 24 题」圆桌聚餐 网络流版子题
#6004. 「网络流 24 题」圆桌聚餐 内存限制:256 MiB时间限制:5000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数 ...
- Lazarus下面的javascript绑定另外一个版本bug修正
Lazarus下面的javascript绑定另外一个版本bug修正 从svn 检出的代码有几个问题 1.fpcjs.pas 单元开始有 {$IFDEF FPC} {$MODE delphi} {$EN ...
- PS故障风海报制作技术分享
1.首先找一张看起来很酷的图(也可以选择自己喜欢的图片): 2. 复制图层,点击添加图层样式,选择混合选项,在高级混合里面的通道选项,有R.G.B三个通道选项,默认是全部勾选的状态,选择其中一个勾掉( ...
- Eclipse新建tld文件
tld(tag lib description文件)就是以.tld结尾的XML文件 选好目录右键 --> New --> Other -->找到XML FIle --> Nex ...