当前位置:网站首页>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;
}边栏推荐
- #26class中get和set设置
- [recommendation system learning] recommendation system architecture
- 7 views on NFT market prospect
- How does Guosen Securities open an account? Is it safe to open a stock account through the link
- Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!
- 一起备战蓝桥杯与CCF-CSP之大模拟炉石传说
- Cache breakdown! Don't even know how to write code???
- js强制转换
- COMP5216 Mobile Computing Assignment 1 - Extending ToDoList app
- Detailed contract quantification system development scheme and technical description of quantitative contract system development
猜你喜欢

sparksql如何通过日期返回具体周几-dayofweek函数

有依赖的背包问题

丰富专业化产品线, 江铃福特领睿·极境版上市

Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)
![[ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers](/img/77/715454c8203d722e246ed70e1fe0d8.png)
[ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers

14《MySQL 教程》INSERT 插入数据

What is the difference between digital collections and NFT

7 views on NFT market prospect

Concurrent thread safety

类型多样的石膏PBR多通道贴图素材,速来收藏!
随机推荐
ACL 2022 | zero sample multilingual extracted text summarization based on neural label search
Jouer avec Linux et installer et configurer MySQL facilement
【推荐系统学习】推荐系统的技术栈
Preparing for the Blue Bridge Cup and ccf-csp
Concurrent thread safety
mysql Add column 失败 因为之前有数据,不是默认null 不行
Niuke network: Design LRU cache structure design LFU cache structure
ACL 2022 | 基于神经标签搜索的零样本多语言抽取式文本摘要
[recommendation system learning] recommendation system architecture
9、智慧交通项目(2)
Redis and database data consistency
Discussion: the next generation of stable coins
Viteconfigure project path alias
【NPOI】C#跨工作薄复制Sheet模板导出Excel
Uncover the secret of Agora lipsync Technology: driving portraits to simulate human speech through real-time voice
Deployment and operation of mongodb partitioned cluster
背包问题求方案数
Inspirational. In one year, from Xiaobai to entering the core Department of Alibaba, his counter attack
Environment setup mongodb
[code Capriccio - dynamic planning] t583. Deleting two strings