UESTC 1339 郭大侠与线上游戏 (STL插入删除/迭代器)
来源:互联网 发布:gitv电视直播软件好吗 编辑:程序博客网 时间:2024/06/03 18:45
STL erase后指针变化
- 对于用数组实现的数据结构(vector,deque),除一个元素后之后所有的迭代器都会失效
- 对于关联型数据结构,删除后只会对当前节点有影响
- 删除节点前应该先取得下一个节点或上一个节点的位置
for( itList = List.begin(); itList != List.end(); ) { if(shouldDel( *itList)) itList = List.erase( itList); //List.erase( itList++); set/map/.. else itList++;}
题意
每次在队列后加入一个值或删除队列首部一个值,或者询问队列中的数的中位数
思路
一开始还在想,要是有自动排序又能瞬间取出中值的数据结构就好了,结果发现操作很简单,可以用stl来完成
代码
需要考虑删除的是当前迭代器指向的元素的情况
#include <bits/stdc++.h>#define mem(a,b) memset(a,b,sizeof(a))#define rep(i,a,b) for(int i=a;i<b;i++)#define sc(a) scanf("%d",&a)const int INF=0x3f3f3f3f;const int maxn=1e6+50;const int mod=1e9+7;const double eps=1e-8;#define pii pair<int,int>typedef long long ll;typedef unsigned int ui;using namespace std;int main(){#ifndef ONLINE_JUDGE //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout);#endif int n; while(~sc(n)){ set<int> se; queue<int> qu; set<int>::iterator itm,ite; rep(i,0,n){ int op,x; sc(op); if(op==1){ sc(x); qu.push(x); se.insert(x); int sz=se.size(); if(sz==1) itm=se.begin(); else{ int cur=*itm; if(x<cur && (sz&1)) itm--; else if(x>cur && !(sz&1)) itm++; } }else if(op==2){ x=qu.front(); qu.pop(); ite=se.find(x); int cur=*itm; int sz=se.size(); if(x<cur && (sz&1)) itm++; else if(x>cur && !(sz&1)) itm--; else if(x==cur && (sz&1)) itm++; else if(x==cur && !(sz&1)) itm--; se.erase(ite); }else{ printf("%d\n",*itm); } } } return 0;}
0 0
- UESTC 1339 郭大侠与线上游戏 (STL插入删除/迭代器)
- UESTC 1339 郭大侠与线上游戏
- 【CDOJ 1339】郭大侠与线上游戏
- 郭大侠与线上游戏
- 2016 UESTC Training for Data Structures I - 郭大侠与线上游戏 CDOJ 1339 pb_ds黑科技
- 2016 UESTC Training for Data Structures I - 郭大侠与线上游戏 2个set
- 【cdoj 1339】郭大侠与线上游戏 set
- 郭大侠与线上游戏(set)
- CDOJ1339 郭大侠与线上游戏
- 【Wannafly Daily】20170412 A 郭大侠与线上游戏
- [UESTC 1342]郭大侠与甲铁城
- 【UESTC 1334 郭大侠与Rabi-Ribi】+ 贪心 + 优先队列
- STL-list插入与删除
- 2016 UESTC Training for Data Structures J - 郭大侠与Rabi-Ribi 优先队列
- 2016 UESTC Training for Data Structures K - 郭大侠与甲铁城 树状数组+离线操作
- 2016 UESTC Training for Data Structures G - 郭大侠与阴阳家 CDOJ 1337 强行map
- 2016 UESTC Training for Data Structures J - 郭大侠与Rabi-Ribi CDOJ 1334 优先队列
- 2016 UESTC Training for Data Structures K - 郭大侠与甲铁城 CDOJ 1342 离线树状数组
- [牛客]可查询最值的栈练习题
- POI 文档 Excel导出功能实现
- SurfaceView在宿主窗口“挖洞”过程要点
- IMWeb训练营作业,一个小的todo list
- 笔记本win7系统鼠标总是乱跳解决方法
- UESTC 1339 郭大侠与线上游戏 (STL插入删除/迭代器)
- 使用阻塞式队列处理大数据
- echars-数据视图自定义
- 断点续传 app版本更新
- vagrant 配置选择生成就是那么简单
- ImageNet Classification with Deep Convolutional Neural Networks 译文
- 注释驱动的 Spring cache 缓存介绍
- mysql 导入txt文件 详解
- hibernate应用开发步骤