【POJ3481】【splay】Double Queue

Description

The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank is identified by a positive integer K and, upon arriving to the bank for some services, he or she receives a positive integer priority P. One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed to break the tradition by sometimes calling the serving desk with the lowest priority instead of that with the highest priority. Thus, the system will receive the following types of request:

0The system needs to stop serving
1 K PAdd client K to the waiting list with priority P
2Serve the client with the highest priority and drop him or her from the waiting list
3Serve the client with the lowest priority and drop him or her from the waiting list

Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.

Input

Each line of the input contains one of the possible requests; only the last line contains the stop-request (code 0). You may assume that when there is a request to include a new client in the list (code 1), there is no other request in the list of the same client or with the same priority. An identifier K is always less than 106, and a priority P is less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority.

Output

For each request with code 2 or 3, the program has to print, in a separate line of the standard output, the identifier of the served client. If the request arrives when the waiting list is empty, then the program prints zero (0) to the output.

Sample Input

2
1 20 14
1 30 3
2
1 10 99
3
2
2
0

Sample Output

0
20
30
10
0

Source

【分析】
本来写一个splay模板,想先过了这题然后去刷3580和维修数列的...
结果交上去果断T了,我大惊,莫非双旋比单旋还慢!
最后D了半天...发现是内存池的原因,知道真相的我眼泪掉下来.....话说我写内存池为什么就没成功过一次....
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <vector>
  6 #include <utility>
  7 #include <iomanip>
  8 #include <string>
  9 #include <cmath>
 10 #include <queue>
 11 #include <assert.h>
 12 #include <map>
 13 #include <ctime>
 14 #include <cstdlib>
 15 
 16 const int MAXN = 100001 + 10;
 17 const int INF = 0x7fffffff;
 18 const int SIZE = 450;
 19 const int maxnode =  10000000 + 10;
 20 using namespace std;
 21 typedef int ll;
 22 struct SPLAY{
 23        struct Node{
 24               int val, size;
 25               int num;//num代表编号 
 26               Node *parent, *ch[2];
 27               
 28               Node(){
 29                      val = size = 0;
 30                      num = 0;
 31                      parent = ch[0] = ch[1] = NULL;       
 32               }
 33               int cmp(){
 34                   if (parent == NULL) return -1;
 35                   if (parent->ch[0]->num == num) return 0;
 36                   if (parent->ch[1]->num == num) return 1;
 37               }
 38               void update(){
 39                    size = 1;
 40                    if (ch[0] != NULL) size += ch[0]->size;
 41                    if (ch[1] != NULL) size += ch[1]->size;
 42               }
 43        }*root, *nil, _nil, mem[1222222];//我写内存池就没成功过
 44        
 45        /*struct MEMPOOR{//内存池 
 46               queue< Node >Q;
 47               Node mem[maxnode];
 48               Node *nil;
 49               
 50               void init(Node *&t){
 51                    for (int i = 0; i < maxnode; i++) Q.push( mem[i] );
 52                    nil = t;
 53               }
 54               Node *get(){
 55                    Node *p = &(Q.front());
 56                    Q.pop();
 57                    p->parent = p->ch[0] = p->ch[1] = nil;
 58                    p->num = p->val = 0;
 59                    p->size = 1;
 60                    return p;
 61               }
 62               //回收内存 
 63               void push(Node *&p){
 64                    Q.push(*(p));
 65                    p->parent->ch[p->cmp()] = nil;
 66               }
 67        }poor;*/
 68        int tot, cnt;//计数 
 69        /*void debug(){//测试内存池 
 70             poor.init();
 71             Node *a = poor.get();
 72             a->val = 10;
 73             Node *b = poor.get();
 74             b->val = 5;
 75             a->ch[0] = b;
 76             printf("%d\n", poor.Q.size());
 77             printf("%d\n", a->val);
 78             printf("%d\n", b->cmp());
 79        }*/
 80        Node *NEW(){
 81             Node *p = &mem[cnt++];
 82             p->parent = p->ch[0] = p->ch[1] = nil;
 83             p->num = p->val = 0;
 84             p->size = 1;
 85             return p;
 86        }
 87        void init(){
 88             //循环哨兵的定义 
 89             nil = &_nil;
 90             _nil.parent = _nil.ch[0] = _nil.ch[1] = nil;
 91             
 92             tot = 0;
 93             cnt = 0;
 94             root = nil;
 95             insert(root, -INF, -INF);
 96             insert(root, INF, INF);
 97        }
 98        void rotate(Node *t, int d){
 99             Node *p = t->parent;//t的右旋对于p来说也是右旋
100             t = p->ch[d ^ 1];
101             p->ch[d ^ 1] = t->ch[d];
102             t->ch[d]->parent = p;
103             t->ch[d] = p;
104             t->parent = p->parent;
105             //注意,这里要更新的原因在于t并不是引用 
106             if (t->parent != nil){
107                if (t->parent->ch[0] == p) t->parent->ch[0] = t;
108                else if (t->parent->ch[1] == p)  t->parent->ch[1] = t;
109             }
110             p->parent = t;
111             if (t->parent == nil) root = t;
112             //不用换回去了... 
113             p->update();
114             t->update();
115        }
116        //将x旋转为y的子树 
117        void splay(Node *x, Node *y){
118             while (x->parent != y){
119                   if (x->parent->parent == y) rotate(x, (x->cmp() ^ 1));
120                   else{//不然连转两下 
121                        rotate(x->parent, x->parent->cmp() ^ 1);
122                        rotate(x, x->cmp() ^ 1);
123                   }
124                   x->update();
125             }
126        }
127        //编号和权值 
128        void insert(Node *&t, int num, int val){
129             if (t == nil){
130                t = NEW();
131                t->val = val;
132                t->num = num;
133                return;
134             }
135             Node *x = t;
136             while (1){
137                   int dir = (val > x->val);
138                   if (x->ch[dir] == nil){
139                      x->ch[dir] = NEW();
140                      x->ch[dir]->val = val;
141                      x->ch[dir]->num = num;
142                      x->ch[dir]->parent = x;
143                      splay(x->ch[dir], nil);
144                      return;
145                   }else x = x->ch[dir];
146             }
147             return;
148        }
149        void debug(){
150             /*init();
151             root = nil;
152             insert(root, 1, 1);
153             //printf("%d", root->val);
154             insert(root, 2, 2);
155             insert(root, 0, 0);
156             insert(root, 4, 4);
157             print(root);
158             //printf("%d\n", root->size); */
159        }
160        //找到val值为k的数的名次 
161        int find(Node *t, int k){
162            if (t == nil) return -1;//-1代表找不到 
163            int tmp = t->ch[0]->size;
164            if (k == t->val) return tmp + 1;
165            else if (k < t->val) return find(t->ch[0], k);
166            else return find(t->ch[1], k) + tmp + 1;
167        }
168        //找到第k小的权值并将其splay到根 
169        void get(Node *t, Node *y, int k){
170            Node *x = t;
171            while (1){
172                  int tmp = x->ch[0]->size;
173                  if ((tmp + 1) == k) break;
174                  if (k <= tmp) x = x->ch[0];
175                  else {x = x->ch[1]; k -= tmp + 1;}
176            }
177            splay(x, y);
178        }
179        //删除val值为k的节点 
180        void Delete(int k){
181             int tmp = find(root, k);
182             get(root, nil, tmp - 1);
183             get(root, root, tmp + 1);
184             //卡出来 
185             //poor.push(root->ch[1]->ch[0]);
186             root->ch[1]->ch[0] = nil;
187             root->ch[1]->update();
188             root->update();
189             return;
190        }
191        void print(Node *t){
192             if (t == nil) return;
193             print(t->ch[0]);
194             printf("%d ", t->val);
195             print(t->ch[1]);
196        }
197        
198 }A;
199 int n, m;
200 
201 
202 void work(){
203      //A.root = nil;
204      A.init();//A.tot记录了A中的元素个数 
205      int t;
206      while (scanf("%d", &t) && t){
207             if (t == 2){
208                if (A.tot == 0) {printf("0\n");continue;}
209                A.get(A.root, A.nil, A.tot + 1);
210                printf("%d\n", A.root->num);
211                A.Delete(A.root->val);
212                A.tot--;
213             }else if (t == 3){
214                if (A.tot == 0) {printf("0\n");continue;}
215                A.get(A.root, A.nil, 2);
216                printf("%d\n", A.root->num);
217                A.Delete(A.root->val);
218                A.tot--;
219             }else{
220                   int val, num;
221                   scanf("%d%d", &num, &val);
222                   A.insert(A.root, num, val);
223                   A.tot++;
224             }
225      }
226 }
227 
228 
229 int main(){
230     #ifdef LOCAL
231     freopen("data.txt", "r", stdin);
232     freopen("out.txt", "w", stdout);
233     #endif
234     work();
235     //A.debug();
236     return 0; 
237 }
View Code

 

转载于:https://www.cnblogs.com/hoskey/p/4330490.html

【毕设实战】基于YOLOv8的HRSC2016船舶智能检测系统开发全指南(含可视化UI界面,打造海事领域高分毕设) 亲爱的同学,通过这篇教程,你已经掌握了“基于YOLOv8的HRSC2016微小船只检测系统”从数据集处理、模型训练到Tkinter界面开发的全流程。这个课题既贴合“海事智能化”的产业战略,又涵盖了“目标检测、深度学习、界面开发”等多个毕设评分点,非常适合作为计算机或海事工程专业的毕业设计。在实践中,你可能会遇到“海面背景复杂导致船只漏检”“微小船只识别精度不足”“UI与模型推理同步卡顿”等挑战——别担心,这些都是技术成长的必经之路。 阅读详情

相关推荐

UFS 功能描述:UFS boot

UFS 启动过程介绍:启动配置,启动代码下载过程和安全性

m0_51720747的博客 1601

POJ 3481:双端队列 ← 数组模拟

​虽然本题来源于三个刷题网站: POJ 3481:http://poj.org/problem?id=3481 HDU 1908:http://acm.hdu.edu.cn/showproblem.php?pid=1908 AcWing 5128:https://www.acwing.com/problem/content/5128/ 但是,下文使用数组模拟的代码仅在 POJ 3481 上通过,而在 HDU 1908 及 AcWing 5128 上均超时。故可考虑使用 STL map 来实现。

hnjzsyjyj的专栏 377

从ESP/ABS选型看轮速传感器:为什么你的车用了AK+PWM+7/14mA三种协议?

本文深入解析了汽车轮速传感器的三种主流协议(7/14mA电流型、PWM型和AK协议型)及其混合应用场景。通过对比不同协议的技术特点、成本效益和工程挑战,揭示了现代车辆采用多种传感器协议背后的设计逻辑,包括成本控制、性能平衡和冗余设计等关键因素。

weixin_29266679的博客 534

POJ 3481 Splay

传送门:POJ 3481题解 用二分查找建树, 然后把新节点放到root下, 这样能够保证查询复杂度平摊为O(logn); 查询后, 把查找的节点删掉(修改父亲和某个孩子指针, 数组模拟), 因为新建的时候旋转, 所以这里的节点不会又两个孩子 还有一种map解法 Splay code:#include<iostream> #include<cstring> #include<cstd

退役ACMer 637

麒麟信安3.3-6C电网版虚拟机安装避坑指南:从ISO下载到SSH配置全流程

本文提供麒麟信安3.3-6C电网版虚拟机的详细安装与配置指南。针对电力行业仿真测试需求,重点解析从ISO镜像获取、虚拟机环境准备、安装过程中的三大“雷区”(如多管理员密码逻辑、分区方案、网络配置)到首次启动后的网络、SSH及root策略配置的全流程避坑要点,帮助技术人员高效、安全地完成部署。

rainy的博客 855

信号完整性之阻抗(五)

一、什么是阻抗 阻抗的概念是从高中或者初中大家可能就会接触到,没想它一直陪伴我十年了。在我的印象中,提到阻抗我可能就会想到电阻,这样的思想局限也限制了我平常思考问题。所以今天也想把阻抗这个概念在顺理一遍,有错误或者欠缺的地方希望大家指正。 1.什么是阻抗: ==在具有电阻、电感和电容的电路里,对电路中的电流所起的阻碍作用叫做阻抗。阻抗常用Z表示,是一个复数,实部称为电阻,虚部称为电抗,其中电容在电路中对交流电所起的阻碍作用称为容抗 ,电感在电路中对交流电所起的阻碍作用称为感抗,电容和电感在电路中对交流电引

刘小同学的博客 2587

poj 3481 Double Queue(平衡树,SBT)

poj 3481 Double Queue 平衡树模板题,练手

yjCola 770

Algorithm:【算法进阶之路】之算法面试刷题集合—数据结构的简介、最强学习路线(逻辑结构【数组-链表-栈和队列/树-图-哈希】、物理结构【元素/关系】、数据运算【十大排序/四大查找-图三大搜索-

​ Algorithm:【算法进阶之路】之算法面试刷题集合—数据结构的简介、最强学习路线(逻辑结构【数组-链表-栈和队列/树-图-哈希】、物理结构【元素/关系】、数据运算【十大排序/四大查找-图三大搜索-树三大遍历】、高级算法【枚举/回溯/递归/分治/动规/贪心】)、问题为导向的十大类刷题算法总结之详细攻略 目录 相关文章 数据结构思维导图 基本术语 一、数据结构的概述 二、数据的逻辑结构 三、数据的物理结构 四、数据的运算

头部AI社区如有邀博主AI主题演讲请私信—心比天高,仗剑走天涯,保持热爱,奔赴向梦想!低调,专注,谦虚,自律,反思,成长,还算比较正能量的博主,公益免费传播…内心特别想在AI界做出一些可以推进历史进程影响力的技术(兴趣使然,有点小情怀,也有点使命感呀 6849

POJ 3481 Double Queue(STL)

题意:让你模拟一个队列,每次能够把一个新的单位加到队列中,或者是找到优先 级最高或最低的人并删除。 首先就应该知道我们需要一个可以自排序的数据结构,于是set乱搞。代码:// // Created by CQU_CST_WuErli // Copyright (c) 2016 CQU_CST_WuErli. All rights reserved. // #include <iostream>

只玩三国的程序猿的专栏 502

医院批量排班实战指南(三)日期列表生成与时段批量操作

在上一篇《批量排班功能实现指南(二):日期与时段设置》中,我们完成了日期范围选择和重复规则配置的界面搭建,实现了从第一步到第二步的跳转功能。现在我们已经可以选择日期范围和重复规则,但还缺少最关键的功能——根据规则生成日期列表并进行时段配置。本篇将继续深入批量排班的核心功能——日期列表生成与时段批量操作。根据日期范围和重复规则智能生成日期列表为每个日期配置上午、下午、夜诊时段批量时段操作提升配置效率日期列表的展示与管理。

低代码布道师的博客 1347

使用Zed 实现测距

此代码基于官方代码基础上进行改写,主要是获取zed相机深度画面的深度值,为yolo测距打基础。Zed相机是由Stereolabs公司开发的一种先进的立体视觉相机。这种相机专为计算机视觉和机器学习应用而设计,能够提供高质量的3D捕捉和分析功能。: Zed相机配备有一对垂直排列的传感器,能够捕捉两个略有偏移的图像,从而模拟人的双眼视觉,实现深度感知。: 利用立体成像技术,Zed相机能够实时生成环境的3D模型,这对于机器人导航、增强现实(AR)和虚拟现实(VR)等应用至关重要。

weixin_45303602的博客 912

POJ3481 Double Queue (map)

使用map,并将优先级值放在first以自动排序,如果输入的代码为2,就输出最后一组元素的second并删去,输入代码为3时同理。

2403_87149971的博客 360

告别JavaScript!C#通过WebAssembly在浏览器中实现“原生级“性能

C# + WebAssembly:前端开发的革命性替代方案 摘要:本文探讨了C#通过WebAssembly取代JavaScript开发前端应用的新趋势。文章分析了JavaScript的局限性(弱类型、回调地狱等),并详细介绍了C#+WebAssembly组合的优势:强类型系统、高性能、多线程支持和代码重用。技术实现方面,阐述了C#通过Mono/.NET编译为WebAssembly字节码的原理,重点介绍了Blazor框架的两种模式。文章还提供了实战指南,包括项目创建、多线程配置,以及一个与ChatGPT AP

墨夶的博客 946

POJ 3481 Double Queue(无旋treap/Splay)

题目传送门 解题思路 三种操作,插入一个数,取最大值并删除,取最小值并删除。 应该有挺多方法可以做的。 提供无旋treap和Splay这两种平衡树的数组的做法。 代码 无旋treap版本 #include<cstdio> #include<algorithm> using namespace std; #define ll long long #defi...

shadiao.的博客 370

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

POJ 3481 Double Queue [splay]

POJ 3481 [splay]

ACTerminate的博客 535

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

poj3481 Double Queue splay

题意:有3中草

AC_kereo的专栏 826

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Double Queue 【POJ - 3481】【Splay】

题目链接 这道题很多人会想到用set来解,当然是可以的,但是确实也可以写splay,因为可以用splay代替set来做到找到某个值,这里我们只需要找到最值问题去求解即可。然后我们对应的找到其与其父亲节点之间的关系,然后更新,就可以做到删除的操作了。 #include <iostream> #include <cstdio> #include <cmath&g...

既然弱小,就只顾变强就是了 332

POJ3481 Double Queue (Splay)

题意:给出三种操作,1表示加入一个值和一个优先级,2表示找到最大优先级的数的值并输出,然后去掉这个数,3表示找到最小优先级的值并输出然后去掉。。。 Splay的模板,写的时候除了一点问题,这里涉及到了插入和删除操作,插入按平衡二叉树的操作就差不多了,删除的时候先把要删除的移动到根节点,然后找第一个比根节点优先级小的数(如果没有直接删掉这个节点,把根节点右移下就好),然后把那个数旋转到根节点的左节

eeeaaaaa的专栏 541

POJ - 3481 Double Queue (Splay)

Double Queue After Handsome Qw Chen graduated from college, he went to work in a bank. There are too many people waiting in line at the bank every day, so H.Q.Chen has a lot of trouble. One day, d...

YOONGI 240

POJ - 3481 Double Queue 【Splay 模板题】

传送门 最基本的Splay模板题,没有任何花里胡哨的操作,可以用于加深理解模板中每个函数。 Splay学习博客:https://baijiahao.baidu.com/s?id=1613228134219334653&wfr=spider&for=pchttps://www.cnblogs.com/cjyyb/p/7499020.html ///推荐当...

qq_42211531的博客 201

POJ 3481 Double Queue(Splay)

原题链接Problem DescriptionThe new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern i

ZhaoYi1222的博客 473

poj 3481 Double Queue splay

Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10244   Accepted: 4651 Description The new founded Balkan Investment Group Bank (BIG-Bank) opene

G.D.Retop的专栏 844

Double Queue POJ - 3481(Splay、Set)

题意: 有3种操作,op=1时,添加一个优先级为p,编号为k的客户,op=2时表示为优先级最大的客户服务,op=3则为优先级最小的客户服务 题解: 这题连数据范围都没给,而且还是POJ的题,真的难受… 先看Set做法,直接放入pair即可,set会先按照first排序,再按照second排序,所以直接取头或尾即可 Set天下第一!!! 再看Splay做法,码量多了好多 可以考虑用Splay维护优...

qq_43544481的博客 178

poj-3481-Double Queue-splay树的水题

很水的splay树。 会简单的操作即可。。。 #include #include #include #include #include using namespace std; #define maxn 1100000 #define mem(a,b) memset(a,b,sizeof(a)) #define root10 ch[ch[root][1]][0] #define root1 c

青竹梦 2291

poj3481 Double Queue(splay)

题面在这里 题目大意: 有一些人,每个人有一个名字和优先级。初始时队列为空。现在有33种操作:插入一个人;弹出优先级最大的那个人;弹出优先级最小的那个人。做法: 平衡树练习。直接上代码。/************************************************************* Problem: poj 3481 Double Queue Use

juruo? juruo! 439
上一篇: 【转载】C++应用引用计数技术
下一篇: 【POJ3580】【splay版】SuperMemo
Gregory99174
博客等级 码龄10年 2粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值