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 ...
随机推荐
- 我的开发小tip
开发原则:1.谁开发,谁负责到底.自己的开发的模块自己维护,不要让别人替你维护,否则很麻烦:2.合理分配时间3.谨慎的处理遇到的bug和问题,不是自己开发的不要轻举妄动,提交到待办中即可4.万勿过度设 ...
- js DomContentLoaded 和 load 的区别
如题:DOMContentLoaded和load都是页面加载的时候触发的事件.区别在于触发的时机不一样. 浏览器渲染页面DOM文档加载的步骤: 1.解析HTML结构. 2.加载外部脚本和css文件. ...
- thinkphp两表联查并且分页
ThinkPHP中关联查询(即多表联合查询)可以使用 table() 方法或和join方法,具体使用如下例所示: 1.原生查询示例: $Model = new Model(); $sql = 'sel ...
- 学习javascript怎么入门,初学者5条建议
你是否已经初步掌握了html和css,但完全不知道从何入手Java?如果是,这里总结了5条建议,帮助JavaScript初学者总结学习方法,提高学习效率. 一.多看视频少看书 对初学者而言,看书的效率 ...
- PAT 甲级 1019 General Palindromic Number(20)(测试点分析)
1019 General Palindromic Number(20 分) A number that will be the same when it is written forwards or ...
- BZOJ1217或洛谷2279 [HNOI2003]消防局的设立
BZOJ原题链接 洛谷原题链接 该题有两种做法,树形\(DP\)和贪心. 先讲贪心. 先将所有点按深度从大到小排序,然后从大到小依次取出点,若已经被覆盖则跳过,否则就在它的祖父点建立消防站. 考虑如何 ...
- MySQL用户及权限管理
查看用户 mysql>SELECT user, host FROM mysql.user; # 检索mysql数据库中的user表 % 表示所有主机的IP 查看当前用户 mysql> se ...
- laravel错误1071 Specified key was too long; max key length is 1000 bytes
Laravel 5.5 环境,php artisan migrate 之后,出现错误如题. 检查了一下,代码是这样的: $table->increments('id'); $table-> ...
- jquery单行文字上下循环滚动
html代码: <div class="box"> <div class="t_news"> <b>已关联奖励账号.昵称:& ...
- MySQL复制(Replication)
引自:http://www.cnblogs.com/hustcat/archive/2009/12/19/1627525.html 1.复制概述 1.1.复制解决的问题数据复制技术有以下一些特点:(1 ...