2019 ICPC 徐州网络赛 B.so easy (并查集)
计蒜客链接:https://nanti.jisuanke.com/t/41384
题目大意:给定n个数,从1到n排列,其中有q次操作,操作(1) 删除一个数字 // 操作(2)求这个数字之后第一个没有被删除的数字(包括自己)。
题解:考虑到实践复杂度问题,n范围是1e9,而q的范围是1e6,所以可以从q入手。用并查集的思路模拟出一个链表,用hashmap存储一个数距离它最近没有被删除的数(即并查集的父亲节点),初始化每个点的map的value存储下一个没有被删除的点,若删除一次,则x的父亲节点变为x+1的父亲,依次以并查集的思想递归下去。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#define inf 0x3f3f3f3f
#include<tr1/unordered_map>
using namespace std::tr1;
unordered_map<int,int> m;//hashmap
int findfa(int x){//并查集find函数
if(!m.count(x)){
return x;
}
else{
return m[x] = findfa(m[x]);
}
}
int main(){
int n,q;
scanf("%d%d",&n,&q);
while(q--){
int z,x;
scanf("%d%d",&z,&x);
if(z==1){
m[x] = findfa(x+1);
}
else{
if(m.count(x) == 0){
printf("%d\n",x);
}
else{
int res = findfa(x);
if(res>n){
printf("%d\n",-1);
}
else{
printf("%d\n",res);
}
}
}
}
return 0;
}
2019 ICPC 徐州网络赛 B.so easy (并查集)的更多相关文章
- 2019 ICPC徐州网络赛 E. XKC's basketball team(二分)
计蒜客题目链接:https://nanti.jisuanke.com/t/41387 题目大意:给定一组无序序列,从第一个数开始,求最远比这个数大m的数,与这个数之间相隔多少数字?如果没有输出-1,否 ...
- 2018 ICPC 徐州网络赛
2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution ...
- 2019 ICPC 南昌网络赛
2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 ...
- 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)
query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...
- HDU 4750 Count The Pairs (2013南京网络赛1003题,并查集)
Count The Pairs Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 徐州网络赛 B so easy
题目链接:https://nanti.jisuanke.com/t/41384 这题暴力能过,我用的是并查集的思想,这个题的数据是为暴力设置的,所以暴力挺快的,但是当他转移的点多了之后,我觉得还是我这 ...
- 计蒜客 41387.XKC's basketball team-线段树(区间查找大于等于x的最靠右的位置) (The Preliminary Contest for ICPC Asia Xuzhou 2019 E.) 2019年徐州网络赛
XKC's basketball team XKC , the captain of the basketball team , is directing a train of nn team mem ...
- HDU 5475(2015 ICPC上海站网络赛)--- An easy problem(线段树点修改)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5475 Problem Description One day, a useless calculato ...
- 2018 icpc 徐州网络赛 F Features Track
这个题,我也没想过我这样直接就过了 #include<bits/stdc++.h> using namespace std; ; typedef pair<int,int> p ...
随机推荐
- 深入浅出Mybatis系列一-Mybatis入门
注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 最近两年 springmvc + mybatis 的在这种搭配还是蛮火的,楼主我呢, ...
- Linux服务器部署.Net Core笔记:目录
目录 Linux服务器部署.Net Core笔记:一.开启ssh服务 Linux服务器部署.Net Core笔记:二.安装FTP Linux服务器部署.Net Core笔记:三.安装.NetC ...
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- 爬取漫画DB上的JoJo的奇妙冒险 第七部 飙马野郎
SBR是JOJO系列我最喜欢的一部,所以今天把漫画爬取到本地,日后慢慢看. import re import time import requests from requests import cod ...
- phpstudy+phpstorm 浏览器没有解析php文件,直接显示源码
用phpstorm编辑完项目,右键浏览器预览时页面报错:502 Bad Gateway PhpStorm 2019.1 此时默认打开的地址是:localhost:63339/开头的一长串…… 查资料说 ...
- mui 时间选择器和上传图片
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <meta name= ...
- Jungle Roads POJ - 1251 模板题
#include<iostream> #include<cstring> #include<algorithm> using namespace std; cons ...
- windows环境下安装JDK
一.环境准备 Windows10 jdk-9.0.1 二.下载并安装JDK 到Java的官网下载JDK安装包,地址:http://www.oracle.com/technetwork/java/jav ...
- 我的python笔记05
Python 之路 Day5 - 常用模块学习 本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve ...
- 巨杉Tech | SequoiaDB虚机镜像正式上线
数据库云化架构需求 随着云架构的发展和流行,在业务和应用进行“云化”的过程中,云数据库因为在整体架构中的重要地位,在云化改造中的重要性不言而喻.云数据库需要满足这些技术要求,除了在功能上的具体提升,在 ...