当前位置:网站首页>ZCMU--1367: Data Structure
ZCMU--1367: Data Structure
2022-06-26 17:31:00 【小小小Why】
Description
给出一个集合,初始为空,进行N次操作,操作分为三种:
1 往集合中添加一个元素,如果集合中已经存在,则无需重复添加
2 从集合中删除一个元素,如果集合中不存在该元素,则无需删除
3 判断元素在集合中排行老几(最小的是老大),如果元素不存在请输出:"sorry"(不含双引号)
Input
多组测试数据
每组第1行:1个整数N,表示操作的次数。(2<=N<=10000)
第2 - (N+1)行:每行2个整数k和s对应操作的方式和被操作的元素(1<=k<=3,1<=s<=1000000)
Output
对应与每一个操作3,给出相应的结果
Sample Input
8
3 1
2 1
1 1
1 1
1 2
3 2
2 2
3 2
5
1 1
1 100
1 1000000
2 99999
3 1000000
Sample Output
sorry
2
sorry
3
解析:查找元素是否存在,我们可以利用数组来快速判断,但是后面排老几,暴力1~N遍历肯定就超时了,所以我们得用set来存存储,然后利用迭代器遍历,初始设置c=1,表示排老几,遍历,c++,直到找到该元素,输出c就是该元素在集合中的排名。
#include <bits/stdc++.h>
using namespace std;
int a[1000005]; //用来直接判断一个元素是否存在
int main()
{
int n,z,p,c;
while(~scanf("%d",&n)){
set<int>st;
set<int>::iterator it; //迭代器,为后面排名做准备
memset(a,0,sizeof(a)); //初始化0
while(n--){
c=1; //排老几
scanf("%d%d",&z,&p);
if(z==1){ //指令1
if(a[p]==0) st.insert(p),a[p]=1;//如果不存在该元素,存入set,并且a[p]变为1
}else if(z==2){ //指令2
if(a[p]==1){ //如果存在就要删除
st.erase(st.find(p)); //删除
a[p]=0; //a[p]置为0,该元素不存在了
}
}else if(z==3){
if(a[p]==1){ //存在
for (it=st.begin();it!=st.end();it++){ //迭代器遍历
if(*it==p){ //*it表示值
printf("%d\n",c); //找到了该元素,输出排名即可
break;
}
c++; //没找到,排名++
}
}else printf("sorry\n"); //a[p]=0,该元素不存在,直接输出sorry
}
}
st.clear();
}
return 0;
}边栏推荐
- LeetCode——226. Flip binary tree (BFS)
- 直播预告|程序员进击,如何提升研发效能?6月21日晚视频号、B站同步直播,不见不散!
- LeetCode——226. 翻转二叉树(BFS)
- 关于FlowUs这一款国民好笔记
- Demonstrate to Xiaobai the case of sub database and sub table
- 如何将应用加入到deviceidle 白名单?
- 防火 疏散 自救…这场安全生产暨消防培训干货满满!
- 接水面试题
- Leetcode topic [array] -268- missing numbers
- Basic requirements: 7 problems in singleton mode
猜你喜欢

7 views on NFT market prospect

Classical synchronization problem

Redis' 43 serial cannons, try how many you can carry

玩转Linux,轻松安装配置MySQL

Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)

20: Chapter 3: develop the pass service: 3: get through the redis server in the program; (it only connects with the redis server and does not involve specific business development)

14《MySQL 教程》INSERT 插入数据

MySQL add column failed because there was data before, not null by default

Programmer's essential toolkit, please collect!

Live broadcast preview | how can programmers improve R & D efficiency? On the evening of June 21, the video number and station B will broadcast live at the same time. See you or leave!
随机推荐
玩转Linux,轻松安装配置MySQL
Viteconfigure project path alias
20:第三章:开发通行证服务:3:在程序中,打通redis服务器;(仅仅是打通redis服务器,不涉及具体的业务开发)
离婚协议中的几个重点
SIGIR 2022 | 港大等提出超图对比学习在推荐系统中的应用
如何将应用加入到deviceidle 白名单?
What is the difference between digital collections and NFT
[recommendation system learning] technology stack of recommendation system
[buuctf.reverse] 126-130
Incomplete line spacing adjustment of formula display in word
背包问题求方案数
Programmer's essential toolkit, please collect!
SQL injection for Web Security (3)
ACL 2022 | 基于神经标签搜索的零样本多语言抽取式文本摘要
Romance of the Three Kingdoms: responsibility chain model
Uncover the secret of Agora lipsync Technology: driving portraits to simulate human speech through real-time voice
Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!
ACL 2022 | zero sample multilingual extracted text summarization based on neural label search
[buuctf.reverse] 126-130
Leetcode daily [2022 - 02 - 16]