当前位置:网站首页>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;
}边栏推荐
- 【推荐系统学习】推荐系统架构
- 国信证券怎么开户?通过链接办理股票开户安全吗
- Demonstrate to Xiaobai the case of sub database and sub table
- 二分查找-2
- In those years, interview the abused red and black trees
- Redis and database data consistency
- When I was in the library, I thought of the yuan sharing mode
- 【uniapp】uniapp手机端使用uni.navigateBack失效问题解决
- Prometeus 2.34.0 新特性
- Rich professional product lines, and Jiangling Ford Lingrui · Jijing version is listed
猜你喜欢

Redis and database data consistency

Over the weekend: 20000 words! Summary of JVM core knowledge, 18 serial cannons as a gift

Daily record 2

Introduction to minimal API

Platform management background and merchant menu resource management: access control design of platform management background

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

Treasure and niche CTA animation material website sharing

【推荐系统学习】推荐系统架构

Viewing the task arrangement ability of monorepo tool from turborepo

SIGIR 2022 | University of Hong Kong and others proposed the application of hypergraph comparative learning in Recommendation System
随机推荐
Here comes the hero League full skin Downloader
9、智慧交通项目(2)
14 MySQL tutorial insert insert data
Turtle cartography
js强制转换
Preparing for the Blue Bridge Cup and ccf-csp
[suggested collection] 11 online communities suitable for programmers
Teach you to learn dapr - 9 Observability
[dynamic planning] Jianzhi offer II 091 Paint the house
【万字总结】以终为始,详细分析高考志愿该怎么填
Programmer's essential toolkit, please collect!
Several key points in divorce agreement
丰富专业化产品线, 江铃福特领睿·极境版上市
ACL 2022 | 基于神经标签搜索的零样本多语言抽取式文本摘要
[uniapp] the uniapp mobile terminal uses uni Troubleshooting of navigateback failure
Prometeus 2.34.0 新特性
Today, I met a "migrant worker" who took out 38K from Tencent, which let me see the ceiling of the foundation
Notes on flowus
Demonstrate to Xiaobai the case of sub database and sub table
LeetCode——226. 翻转二叉树(BFS)