牛客网多校第4场 J Hash Function 【思维+并查集建边】
题目链接:戳这里
学习博客:戳这里
题意:
有n个空位,给一个数x,如果x%n位数空的,就把x放上去,如果不是空的,就看(x+1)%n是不是空的。
现在给一个已经放过数的状态,求放数字的顺序。(要求字典序最小
解题思路:
用一个优先队列。如果序列里的x的位子就是x%n,证明这个数是最开始就放好的。加入优先队列。
每次出一个最小元素。把它填入位子a,然后用并查集把a和a+1合并,看看现在a的祖先能不能被加入队列。
用优先队列维护很容易想到,关键是怎么维护选出的数的位置,这里用并查集建边很巧妙的解决了这个问题。
附ac代码:
1 #include<iostream>
2 #include<algorithm>
3 #include<stdio.h>
4 #include<string.h>
5 #include<string>
6 #include <cmath>
7 #include <vector>
8 #include <queue>
9 using namespace std;
10 typedef long long ll;
11 const ll mod = 1e9 + 7;
12 const int maxn = 2e6 + 10;
13 int pre[maxn];
14 int nu[maxn];
15 int ans[maxn];
16 bool vis[maxn];
17
18 struct nod
19 {
20 int pos;
21 int val;
22 nod(){};
23 nod(int x, int y)
24 {
25 pos = x;
26 val = y;
27 }
28 bool friend operator < (nod a, nod b)
29 {
30 return a.val > b.val;
31 }
32
33 };
34 void init(int n)
35 {
36 for(int i = 0; i <= n; ++i)
37 pre[i] = i;
38 }
39 int find(int x)
40 {
41 if(pre[x] == x) return x;
42 else return pre[x] = find(pre[x]);
43 }
44 int main()
45 {
46 int t;
47 scanf("%d", &t);
48 int n;
49 while(t--)
50 {
51 int cnt = 0;
52 int len = 0;
53 scanf("%d", &n);
54
55 priority_queue<nod>q;
56 memset(vis, 0, sizeof(vis));
57 init(n);
58 for(int i = 0; i < n; ++i)
59 {
60 scanf("%d", &nu[i]);
61 }
62 for(int i = 0; i < n; ++i)
63 {
64 if(nu[i] == -1) continue;
65 ++cnt;
66 if(nu[i] % n == i)
67 {
68 q.push(nod(i, nu[i]));
69 vis[i] = 1;
70 }
71 }
72
73 while(!q.empty())
74 {
75 nod u = q.top(); q.pop();
76 ans[++len] = u.val;
77 int to = pre[find(u.pos)] = find((u.pos + 1) % n);
78 if(vis[to] || nu[to] == -1 || find(nu[to]%n) != to) continue;
79 q.push(nod(to, nu[to]));
80 vis[to] = 1;
81 }
82 if(len != cnt)
83 {
84 puts("-1");
85 continue;
86 }
87
88 for(int i = 1; i <= len; ++i)
89 {
90 if(i > 1) printf(" ");
91 printf("%d", ans[i]);
92 }
93 printf("\n");
94 }
95 }
牛客网多校第4场 J Hash Function 【思维+并查集建边】的更多相关文章
- 牛客网多校训练第一场 J - Different Integers(树状数组 + 问题转换)
链接: https://www.nowcoder.com/acm/contest/139/J 题意: 给出n个整数的序列a(1≤ai≤n)和q个询问(1≤n,q≤1e5),每个询问包含两个整数L和R( ...
- 牛客网多校第7场 J Sudoku Subrectangles 【构造】
题目:戳这里 题意:给一个n*m的矩阵,里面由a~z及A~Z构成,问有多少个子矩阵满足任意一行或一列中都没有相同的字母. 解题思路:左上角和右下角两点可以确定一个矩阵.可以先预处理出来每个点作为一个矩 ...
- 牛客网多校第5场 F take 【思维+数学期望】
题目:戳这里 思路来源:视频讲解 题意:有n个箱子按1...n标号,每个箱子有大小为di的钻石概率为pi,我们初始有个大小为0的钻石,从1到n按顺序打开箱子,遇到比手中大的箱子就换,求交换次数的数学期 ...
- 牛客多校第四场 J.Hash Function(线段树优化建图+拓扑排序)
题目传送门:https://www.nowcoder.com/acm/contest/142/J 题意:给一个hash table,求出字典序最小的插入序列,或者判断不合法. 分析: eg.对于序列{ ...
- 牛客网多校第3场C-shuffle card 平衡树或stl(rope)
链接:https://www.nowcoder.com/acm/contest/141/C 来源:牛客网 题目描述 Eddy likes to play cards game since there ...
- 牛客网多校第3场Esort string (kmp)
链接:https://www.nowcoder.com/acm/contest/141/E 来源:牛客网 题目描述 Eddy likes to play with string which is a ...
- 牛客网多校赛第九场A-circulant matrix【数论】
链接:https://www.nowcoder.com/acm/contest/147/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...
- 牛客网多校训练第二场D Kth Minimum Clique
链接:https://ac.nowcoder.com/acm/contest/882/D来源:牛客网 Given a vertex-weighted graph with N vertices, fi ...
- 牛客网多校第5场 H subseq 【树状数组+离散化】
题目:戳这里 学习博客:戳这里 题意:给n个数为a1~an,找到字典序第k小的序列,输出该序列所有数所在位置. 解题思路:先把所有序列预处理出来,方法是设一个数组为dp,dp[i]表示以i为开头的序列 ...
随机推荐
- Kubernetes调整Node节点快速驱逐pod的时间
在高可用的k8s集群中,当Node节点挂掉,kubelet无法提供工作的时候,pod将会自动调度到其他的节点上去,而调度到节点上的时间需要我们慎重考量,因为它决定了生产的稳定性.可靠性,更快的迁移可以 ...
- python(pymysql操作数据库)
第一种方式 import pymysql # 打开数据库连接 db = pymysql.connect(host="192.168.88.11", user="root& ...
- 文件的上传/下载+在线游览(转化html)--不需要在线插件//自己写的小方法
1 /// <summary> 2 /// 文件上传下载帮助类 3 /// </summary> 4 public static class FileHelper 5 { 6 ...
- [从源码学设计] Flume 之 memory channel
[从源码学设计] Flume 之 memory channel 目录 [从源码学设计] Flume 之 memory channel 0x00 摘要 0x01 业务范畴 1.1 用途和特点 1.2 C ...
- Nginx上安装SSL证书
准备 参考 :链接 下载的Nginx证书压缩文件解压后包含: .pem:证书文件.PEM文件的扩展名为CRT格式. .key:证书密钥文件.申请证书时如果未选择自动创建CRS,则下载的证书文件压缩包中 ...
- (六)整合 QuartJob ,实现定时器实时管理
整合 QuartJob ,实现定时器实时管理 1.QuartJob简介 1.1 核心API 2.SpringBoot整合QuartJob 2.1 项目结构 2.2 定时器配置 2.3 定时器管理工具 ...
- 《》——8幅图图解Java机制
String对象不可改变的特性 String s = "abcd"; s = s.concat"ef"; equals()与hashCode()方法协作约定 H ...
- Java实现发送HTTP的POST请求,返回数据以及请求状态
/** * @param url:请求url * @param content: 请求体(参数) * @return errorStr:错误信息;status:状态码,response:返回数据 */ ...
- python yield初探 (转)
1. iterator叠代器最简单例子应该是数组下标了,且看下面的c++代码: int array[10]; for ( int i = 0; i < 10; i++ ) printf(& ...
- java 去掉重复的数字
public static void main(String[] args) { String s="1,2,2,2,2,2,3,3,3"; String[] array = s. ...