2018牛客多校第四场J题 Hash Function(思维+并查集)

探讨如何从已知的哈希表结果反推出原始插入序列,并实现算法以找到字典序最小的可能序列。

题目描述 

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

题意:给出按照哈希规则哈出来的序列,给出一个字典序最小的输入顺序.

思路:我们用优先队列使字典序最小,对于没冲突的元素,随时可以放,所以我们一开始就可以把他压入优先队列.

对于有冲突的元素,只有当他的位置与他本应该在的位置(即%n的位置)之间放满了元素时他才可以放,也就是才可以压入优先队列.所以,当某个元素安放好之后,他后面的原来不能安放的元素是否可以因为他的安放而得到安放的机会.所以我们用并查集维护把安放的位置的父亲设置为他下一位置的父亲,这样当我们判断某个元素可不可以安放的时候只需要看看他与他本应该在的位置(即%n的位置)是不是一个父亲即可~是的话就压入队列~  

代码:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 2e5+5;
const double eps = 1e-12;
const int inf = 0x3f3f3f3f;
map<int,int>::iterator it;

struct node
{
    int val;
    int pos;
    node(){}
    node (int val,int pos):val(val),pos(pos){}
    friend bool operator < (node x,node y)
    {
    	return x.val> y.val;
	}
};

int n;
int a[maxn],pre[maxn],vis[maxn],ans[maxn];

int find(int x)
{
	return pre[x] == x?x:pre[x] = find(pre[x]);
}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		mem(vis,0);
		priority_queue<node> q;
		scanf("%d",&n);
		for(int i = 0;i< n;i++)
			scanf("%d",&a[i]),pre[i] = i;
		
		int num = 0;
		for(int i = 0;i< n;i++)
		{
			if(a[i] == -1) continue;
			if(a[i]%n == i)
			{
				q.push(node(a[i],i));//压入队列
				vis[i] = 1;//已经在队列了
			}
			num++;
		}
		
		int cnt = 0;
		while(!q.empty())
		{
			node tmp = q.top();
			q.pop();
			ans[++cnt] = tmp.val;
			pre[find(tmp.pos)] = find((tmp.pos+1)%n);//连接起来
			int np = pre[tmp.pos];//np为要安放的位置
			if(vis[np]||a[np] == -1||find(a[np]%n)!= np) continue;//a[np]%n 本应该在的位置
			q.push(node(a[np],np));
			vis[np] = 1;
		}
		
		if(cnt< num)
			printf("-1\n");
		else if(cnt == 0) printf("\n");
		else
		{
			for(int i = 1;i< cnt;i++)
				printf("%d ",ans[i]);
			printf("%d\n",ans[cnt]);
		}
	}
	
	return 0;
}

 

牛客网在 2018 年举办的 ACM 暑期训练营是一系列面向算法竞赛爱好者的在线编程赛事,旨在帮助参赛者提升算法能力、团队协作能力以及对复杂问的解决能力。比赛通常以团队为单位参与,每场比赛包含若干道编程目,涉及图论、数据结构、动态规划、字符串等个算法领域。 ### 比赛目与型分布 比赛目通常由个高或企业出,涵盖难度从简单到困难的目,例如: - **数学与数论**:包括模运算、素数判定、数列处理等。 - **图论**:涉及最短路径(Dijkstra、Floyd)、最小生成树(Kruskal、Prim)、拓扑排序等。 - **动态规划**:包括线性DP、区间DP、树形DP等。 - **字符串处理**:KMP、AC自动机、后缀数组等。 - **数据结构**:线段树、并查集、平衡树等。 - **组合数学与计算几何**:排列组合、概率期望、凸包等。 例如,在第六场的 G 中,涉及树的性质和图论的网络流问,需要计算所有点对之间的最大流之和,这要求对树上路径性质有深入理解[^1]。 ### 解与解思路 每场比赛结束后,牛客网通常会发布官方解,并鼓励选手在论坛或博中分享解思路和代码。例如: - 在第四场的 G 中,目要求找出删除若干元素后使得众数唯一的问解中提出从大到小枚举可能的众数候选,并判断是否可以通过删除不超过 M 个元素来实现目标。此需要注意内存初始化的问,因为 T 的范围未知,常规的 `memset` 可能导致超时,因此建议使用 `map` 或其他结构进行优化[^4]。 - 在第一场的 J 中,使用了将数组扩展为两倍长度的技巧,以便处理环形数组问,这种技巧在处理循环数组时非常常见且有效[^3]。 ### 比赛经验分享 参赛者通常会从以下几个方面总结经验: - **时间管理**:比赛通常为 3-5 小时,合理分配时间解出尽可能目是关键。 - **团队协作**:三人组队时,分工明确(如一人主写代码,一人读,一人调试)能显著提高效率。 - **模板积累**:熟练掌握常用算法模板(如快速幂、Dijkstra、线段树等)可大幅节省编码时间。 - **调试技巧**:使用断点调试、对拍工具等能快速发现代码中的错误。 - **赛后复盘**:分析未通过的目,理解官方解并尝试重新实现是提升的关键。 ### 示例代码 以下是一个用于处理二维字符矩阵对称性的代码片段,来自第四场 D 的解法: ```cpp #include <bits/stdc++.h> using namespace std; const int maxN = 2000 + 5; int n, m; char str[maxN][maxN]; int main() { int T; scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%s", str[i] + 1); int tp = -1; for (int i = 1; i <= n / 2; i++) { bool f = true; for (int j = 1; j <= m; j++) if (str[i][j] != str[n - i + 1][j]) { f = false; break; } if (!f) { tp = i; break; } } if (tp == -1) tp = n / 2; int tq = -1; for (int j = 1; j <= m / 2; j++) { bool f = true; for (int i = 1; i <= n; i++) if (str[i][j] != str[i][m - j + 1]) { f = false; break; } if (!f) { tq = j; break; } } if (tq == -1) tq = m / 2; int ans; if (tp == 1 || tq == 1) { ans = 0; } else { ans = (tp - 1) * (tq - 1); } printf("%d\n", ans); } return 0; } ``` 该代码通过逐行逐列判断矩阵的对称性,最终计算出可以保留的对称区域数量。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值