技术标签: ******比赛****** 模拟 ACM的进阶之路
Chiaki has just learned hash in today's lesson. A hash function is any function that can be used to map data of arbitrary size to data of fixed size. As a beginner, Chiaki simply chooses a hash table of size n with hash function .
Unfortunately, the hash function may map two distinct values to the same hash value. For example, when n = 9 we have h(7) = h(16) = 7. It will cause a failure in the procession of insertion. In this case, Chiaki will check whether the next position is available or not. This task will not be finished until an available position is found. If we insert {7, 8, 16} into a hash table of size 9, we will finally get {16, -1, -1, -1, -1, -1, -1, 7, 8}. Available positions are marked as -1.
After done all the exercises, Chiaki became curious to the inverse problem. Can we rebuild the insertion sequence from a hash table? If there are multiple available insertion sequences, Chiaki would like to find the smallest one under lexicographical order.
Sequence a1, a2, ..., an is lexicographically smaller than sequence b1, b2, ..., bn if and only if there exists i (1 ≤ i ≤ n) satisfy that ai < bi and aj = bj for all 1 ≤ j < i.
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case: The first line of each case contains a positive integer n (1 ≤ n ≤ 2 x 105) -- the length of the hash table. The second line contains exactly n integers a1,a2,...,an (-1 ≤ ai ≤ 109). It is guaranteed that the sum of all n does not exceed 2 x 106.
For each case, please output smallest available insertion sequence in a single line. Print an empty line when the available insertion sequence is empty. If there's no such available insertion sequence, just output -1 in a single line.
示例1
复制
3 9 16 -1 -1 -1 -1 -1 -1 7 8 4 8 5 2 3 10 8 10 -1 -1 34 75 86 55 88 18
复制
7 8 16 2 3 5 8 34 75 86 55 88 18 8 10
给定一个Hash数列,求原数列,要求最后输出的字典序最小。
Emmmmmm,最近不wa10+都不好意思过题,模拟过的,看其他大佬还有并查集,拓扑过的,感觉思维跟不上,我也就做个模拟题了。
首先发现当s[i]%n == i的时候,说明这个数是第一次就满足原序列要求的,所以先把所有这类数字归到一个集合中去,然后去找此时的待选集合中最小数字的下一个空位置,也就是未进入集合的位置,判断他是否可以进入集合,最开始待选集合中只有 s[i]% n == i这类数字的最小值s[i],不断向其中加数字,维护每次取集合数的最小值,得到答案。
维护集合数的最小值可以用优先队列维护,每次找此数字的下一个空位置可以将下标放入set维护,刚开始可以把s[i]%n == i的数全部放入优先队列,这样不用找到那个最小值,后面判断一下,不重复进入队列就ok。
代码实现:
/*
Look at the star
Look at the shine for U
*/
#include<bits/stdc++.h>
#define sl(x) scanf("%lld",&x)
using namespace std;
typedef long long ll;
const int N = 1e6+5;
const ll mod = 1e9+7;
const ll INF = 1e18;
int ans[N],cnt;
struct node{
int num,index;
friend bool operator < (node f1, node f2)
{
if(f1.num == f2.num) return f1.index > f2.index;
return f1.num > f2.num;
}
}p[N];
int main()
{
int n,i,j,k,t;
scanf("%d",&t);
while(t--)
{
set <int> s;
scanf("%d",&n);
cnt = 0;k = 0;
priority_queue <node> q;
for(i = 0;i < n;i++)
{
scanf("%d",&p[i].num);p[i].index = i;
if(p[i].num%n == i) q.push(p[i]);
if(p[i].num != -1) k++;
s.insert(i);
}
set <int> ::iterator it;
set <int> ::iterator lt;
while(!q.empty())
{
node temp = q.top(); q.pop();
if(!s.count(temp.index)) continue;
ans[cnt++] = p[temp.index].num;
s.erase(temp.index);
/* -------pay attention-------*/
if(!s.empty())
{
it = s.upper_bound(temp.index);
if(it == s.end()) it = s.begin();
lt = s.lower_bound((p[*it].num)%n);
if(lt == s.end()) lt = s.begin();
if(lt == it && p[*it].num != -1) q.push(p[*it]);
}
}
if(cnt != k) puts("-1");
else
{
for(i = 0;i < cnt;i++)
{
if(i) printf(" "); printf("%d",ans[i]);
}
puts("");
}
}
}
/*
3 3
-1 1 1
*/
1.APIjava.lang包下的内容不需要导包,其他包需要import;引用类型步骤:(1)导入包,(2)创建,(3)使用2.匿名对象的说明//匿名对象就是只有右边的对象,没有左边的名字和赋值运算符new person().name="huhu";//匿名对象只使用唯一的一次,只使用一次可以匿名对象。Scanner sc = new Scanner(System.in);methodParam(sc);//使用匿名对象来进行传参methodParam(new Scanner(Syst
非官方板卡应用非官方板卡也需要在官方提供的历程上进行修改,这样节省时间,而且AD936X的IP也需要参考官方的IP。下载并编译Xilinx u-boot源码选用的U-boot的网站为htt...
CDN是什么CDN的全称是Content Delivery Network,即内容分发网络。其目的是广泛采用各种缓存服务器,将这些缓存服务器分布到用户访问相对集中的地区或网络中,在用户访问网站时,利用全局负载技术将用户的访问指向距离最近的工作正常的缓存服务器上,由缓存服务器直接响应用户请求。CDN的组成部分是什么1、内容缓存设备内容缓存为CDN网络节点,位于用户接入点,是面向最终用户的...
ps5给人很大亮眼的地方就是超快的硬盘,的确加载速度也是最近游戏硬件猛烈升级的重要领域;刚好最近gdc22中ForSpoken的share中也聊到了DirectStorage,终于pc上也可以开始享受加载速度提升的快乐了;时间线19年nvidia就在high performance computing那部分,cuda里面提出了GPUDirectStorage...
三河讲python首先来告诉大家下面的Python程序实现了通过从网页抓取一篇文章,然后根据这篇文章来生成新的文章,这其中的原理就是基于概率统计的文本分析。过程大概就是网页抓取数据->统计分析->生成新文章。网页抓取数据是通过BeautifulSoup库来抓取网页上的文本内容。统计分析这个首先需要使用ngram模型来把文章进行分词并统计频率。因为文章生成主要依据马尔可夫模型,所以使用了...
1.ubuntu 默认面板恢复命令昨天裝了Ubuntu,折腾来折腾去,面板不见了,不是默认的布局了,添加面板不是解決办法。Google 了下,解决办法如下:打开终端,终端窗口打开之后,立即在提示符后面输入下列命令:gconftool --recursive-unset /apps/panel(注意:每个斜杠 “/” 后面没有空格)接下来输入下列命令:rm
为什么80%的码农都做不了架构师?>>> ...
文章目录简要用法用法auto 是C++程序设计语言的关键字。自C++11以来,auto关键字用于两种情况:声明变量时根据初始化表达式自动推断该变量的类型、声明函数时函数返回值的占位符。C++98标准中auto关键字用于自动变量的声明,但由使用极少且多余,在C++11中已删除了这一用法。简要用法auto可以在声明变量时根据变量初始值的类型自动为此变量选择匹配的类型。C++语言类似的关键字还有decltype。举例:对于值x=1;即可以声明:int x = 1或long x = 1,也可以直接声明aut
很多框架都可以完成,同理。Laravel的Eloquent ORM 获取当前记录的上一篇下一篇然后,当时在答案里面简单写了一下解决方案。不过由于这个取得下一条和取得上一条的记录其实在日常的开发当中还是会经常遇到,最常见的场景可能就是取得一篇文章的上一篇文章和下一篇文章了。其实这个在Laravel的Eloquent中实现还是挺容易的,不过由于Laravel并没有直接提供给我们相应的方法,我们...
2019独角兽企业重金招聘Python工程师标准>>> ...
1. 简述 在 Windows2000/xp 下,安装 VS2005, QT 4.5.2 ;并在 VS2005上建立 QT 的集成开发环境, 利用 VS2005 开发环境开发,调试 QT 程序;2. 所需程序 VS2005 // VS2005 的安装程序; qt-win-opensource-src-4.5.2.zi
由于项目需要,需要了解一下电路的设计,因此特地使用Multisim软件,绘制了一个简单的电路图。如下:一开始在绘制的时候,感觉找那些元器件,挺麻烦的,不懂元器件不知道它的英语文字。还有在选择7段显示管的时候,一开始我使用阳性的显像管(anode),没有反应,后来换成阴极的显像管(cathode),结果就可以了。 以后还是要多学些电子方面的知识了。