hdu 1908 Double Queue
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=1908
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:
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 $10^6$, and a priority P is less than $10^7$. 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.
SampleInput
2
1 20 14
1 30 3
2
1 10 99
3
2
2
0
SampleOutput
0
20
30
10
0
红黑树:
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::set;
using std::map;
using std::pair;
using std::vector;
using std::multiset;
using std::multimap;
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
const int Max_N = ;
typedef unsigned long long ull;
struct Node {
int data, s, client;
bool color;
Node *fa, *ch[];
inline void setc(int _v, int c, int i, bool _color, Node *ptr) {
data = _v, color = _color, s = i, client = c;
fa = ch[] = ch[] = ptr;
}
inline void push_up() {
s = ch[]->s + ch[]->s + ;
}
inline void push_down() {
for (Node *x = this; x->s; x = x->fa) x->s--;
}
};
struct RedBlackTree {
int top;
Node *root, *null;
Node stack[Max_N], *tail, *pool[Max_N];
inline void init() {
top = ;
tail = &stack[];
null = tail++;
null->setc(, , , , NULL);
root = null;
}
inline Node *newNode(int v, int client) {
Node *x = !top ? tail++ : pool[--top];
x->setc(v, client, , , null);
return x;
}
inline void rotate(Node* &x, bool d) {
Node *y = x->ch[!d];
x->ch[!d] = y->ch[d];
if (y->ch[d] != null) y->ch[d]->fa = x;
y->fa = x->fa;
if (x->fa == null) root = y;
else x->fa->ch[x->fa->ch[] != x] = y;
y->ch[d] = x;
x->fa = y;
y->s = x->s;
x->push_up();
}
inline void insert(int v, int client) {
Node *x = root, *y = null;
while (x->s) {
x->s++;
y = x, x = x->ch[v > x->data];
}
x = newNode(v, client);
if (y != null) y->ch[v > y->data] = x;
else root = x;
x->fa = y;
insert_fix(x);
}
inline void insert_fix(Node* &x) {
while (x->fa->color) {
Node *par = x->fa, *Gp = par->fa;
bool d = par == Gp->ch[];
Node *uncle = Gp->ch[d];
if (uncle->color) {
par->color = uncle->color = ;
Gp->color = ;
x = Gp;
} else if (x == par->ch[d]) {
rotate(x = par, !d);
} else {
Gp->color = ;
par->color = ;
rotate(Gp, d);
}
}
root->color = ;
}
inline Node *find(Node *x, int data) {
while (x->s && x->data != data) x = x->ch[x->data < data];
return x;
}
inline void erase_fix(Node* &x) {
while (x != root && !x->color) {
bool d = x == x->fa->ch[];
Node *par = x->fa, *sibling = par->ch[d];
if (sibling->color) {
sibling->color = ;
par->color = ;
rotate(x->fa, !d);
sibling = par->ch[d];
} else if (!sibling->ch[]->color && !sibling->ch[]->color) {
sibling->color = , x = par;
} else {
if (!sibling->ch[d]->color) {
sibling->ch[!d]->color = ;
sibling->color = ;
rotate(sibling, d);
sibling = par->ch[d];
}
sibling->color = par->color;
sibling->ch[d]->color = par->color = ;
rotate(par, !d);
break;
}
}
x->color = ;
}
inline void erase(int data) {
Node *z = find(root, data);
if (!z->s) return;
Node *y = z, *x = null;
if (z->ch[]->s && z->ch[]->s) {
y = z->ch[];
while (y->ch[]->s) y = y->ch[];
}
x = y->ch[!y->ch[]->s];
x->fa = y->fa;
if (!y->fa->s) root = x;
else y->fa->ch[y->fa->ch[] == y] = x;
if (z != y) z->data = y->data, z->client = y->client;
y->fa->push_down();
if (!y->color) erase_fix(x);
pool[top++] = y;
}
inline Node *kth(int k) {
int t = ;
Node *x = root;
for (; x->s;){
t = x->ch[]->s;
if (k == t + ) break;
else if (k <= t) x = x->ch[];
else k -= t + , x = x->ch[];
}
return x;
}
inline Node *operator[](int k) {
return kth(k);
}
inline void go(int n) {
int a, b;
Node *ret = null;
if ( == n || == n) {
if ( == n && root->s) ret = rbt[root->s];
else if ( == n && root->s) ret = rbt[];
if (!ret->s) printf("0\n");
else printf("%d\n", ret->client), erase(ret->data);
} else {
scanf("%d %d", &a, &b);
insert(b, a);
}
}
}rbt;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
rbt.init();
while (~scanf("%d", &n) && n) {
rbt.go(n);
}
return ;
}
sb树:
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::set;
using std::map;
using std::pair;
using std::vector;
using std::multiset;
using std::multimap;
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
const int Max_N = ;
typedef unsigned long long ull;
struct Node {
int v, p, s;
Node *ch[];
inline void setc(int _v, int _p, int _s, Node *ptr) {
v = _v, s = _s, p = _p;
ch[] = ch[] = ptr;
}
inline void push_up() {
s = ch[]->s + ch[]->s + ;
}
inline int cmp(int x) const {
return p == x ? - : x > p;
}
};
struct SBT {
int top;
Node *null, *root, *tail;
Node stack[Max_N], *pool[Max_N];
inline void init() {
top = ;
tail = &stack[];
null = tail++;
null->setc(, , , NULL);
root = null;
}
inline Node *newNode(int v, int p) {
Node *x = !top ? tail++ : pool[--top];
x->setc(v, p, , null);
return x;
}
inline void rotate(Node *&x, int d) {
Node *k = x->ch[!d]; x->ch[!d] = k->ch[d], k->ch[d] = x;
k->s = x->s;
x->push_up();
x = k;
}
inline void Maintain(Node *&x, int d) {
if (!x->s) return;
if (x->ch[d]->ch[d]->s > x->ch[!d]->s) rotate(x, !d);
else if (x->ch[d]->ch[!d]->s > x->ch[!d]->s) rotate(x->ch[d], d), rotate(x, !d);
else return;
Maintain(x, ), Maintain(x, );
}
inline void insert(Node *&x, int v, int p) {
if (!x->s) { x = newNode(v, p); return; }
x->s++;
int d = x->cmp(p);
insert(x->ch[d], v, p);
x->push_up();
Maintain(x, d);
}
inline void erase(Node *&x, int p) {
if (!x->s) return;
x->s--;
int d = x->cmp(p);
if (- == d) {
if (!x->ch[]->s || !x->ch[]->s) {
pool[top++] = x;
x = x->ch[]->s ? x->ch[] : x->ch[];
} else {
Node *ret = x->ch[];
for (; ret->ch[]->s; ret = ret->ch[]);
x->v = ret->v, x->p = ret->p;
erase(x->ch[], x->p);
}
} else {
erase(x->ch[d], p);
}
}
inline Node *kth(int k){
int t = ;
Node *x = root;
for (; x->s;){
t = x->ch[]->s;
if (k == t + ) break;
else if (k <= t) x = x->ch[];
else k -= t + , x = x->ch[];
}
return x;
}
inline Node *operator[](int k) {
return kth(k);
}
inline void go(int n) {
int a, b;
Node *ret = null;
if ( == n || == n) {
if ( == n && root->s) ret = sbt[root->s];
else if ( == n && root->s) ret = sbt[];
if (!ret->s) printf("0\n");
else printf("%d\n", ret->v), erase(root, ret->p);
} else {
scanf("%d %d", &a, &b);
insert(root, a, b);
}
}
}sbt;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
sbt.init();
while (~scanf("%d", &n) && n) {
sbt.go(n);
}
return ;
}
hdu 1908 Double Queue的更多相关文章
- POJ 3481 & HDU 1908 Double Queue (map运用)
题目链接: PKU:http://poj.org/problem?id=3481 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1908 Descript ...
- HDU 1908 Double Queue(set)
Problem Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in B ...
- 【HDOJ】1908 Double Queue
双端队列+二分. #include <cstdio> #define MAXN 1000005 typedef struct { int id; int p; } node_st; nod ...
- poj 3841 Double Queue (AVL树入门)
/****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...
- treap树---Double Queue
HDU 1908 Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office i ...
- POJ 3481 Double Queue STLmap和set新学到的一点用法
2013-08-08 POJ 3481 Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...
- 【Map】Double Queue
Double Queue Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13258 Accepted: 5974 Des ...
- POJ 3481 Double Queue(STL)
题意 模拟银行的排队系统 有三种操作 1-加入优先级为p 编号为k的人到队列 2-服务当前优先级最大的 3-服务当前优先级最小的 0-退出系统 能够用stl中的map 由于map本身 ...
- POJ 3481 Double Queue(set实现)
Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Buchares ...
随机推荐
- Azure磁盘的吞吐量测试
Azure的高级存储具有吞吐量大,延迟低的特点,非常适合时间关键型的应用程序(如SQL Server, Oracle, Redis等). 但高级存储同时具有价格高的特点,用户往往对其实际的性能数据较为 ...
- VS集成Qt环境搭建
环境:VS2010 + Qt5.2 关于VS的下载.安装,这里就不再做过多阐述. 一.下载Qt5.2安装包(qt-windows-opensource)与Qt插件(Visual Studio Add- ...
- notepad++ tab键用空格缩进
从工作那天开始到现在,写python代码一直用notepad++来写,尝试几次都改不回eclipse.o(╯□╰)o python脚本中,如果用制表符缩进,经常会报错,必须改用空格缩进代替. 之前设置 ...
- 没有终结点在侦听可以接受消息的*这通常是由于不正确的地址或者 SOAP操作导致的
引发原因:项目启动时,前端调用 wcf地址,引用的地址访问无法在 IIS Express找到导致该错误 解决方法,找出前端的web.config 查看引用的项目是什么地址开头,如 localho ...
- url 参数的加号变成空格处理
今天在调试客户端向服务器传递参数时,参数中的“+”全部变成了空格,原因是URL中默认的将“+”号转义了. 解决方法如下: 方法一.修改客户端 将客户端带“+”的参数中的“+”全部替换为“2B%”,这 ...
- iOS7 iOS8 UITableviewCell处于编辑状态,dismiss或者back崩溃
今天在项目中遇到一个坑爹的 Crash , 在 iOS7 iOS8 UITableviewCell处于编辑状态,dismiss或者back崩溃 iOS9不会 原因:苹果的BUG代码 解决:在视图消失 ...
- 8051学习笔记——IIC与EEPROM实验
main.c #include <reg51.h> #include "iic.h" #define AT24C02 0xa0 //AT24C02 地址 sbit LS ...
- Linux平台下Lotus Domino服务器部署案例
Linux平台下Lotus Domino服务器部署案例 几年前我写了篇<RHAS2.1下安装中文LotusDominoR6.5图解>这篇文档被多个大型网站转载,曾帮助过很多公司系统管理员部 ...
- javascript 实现HashTable(哈希表)
一.javascript哈希表简介 javascript里面是没有哈希表的,一直在java,C#中有时候用到了这一种数据结构,javascript里面若没有,感觉非常不顺手.细细看来,其实javasc ...
- ios开发之OC基础-ios开发学习路线图
本系列的文章主要来自于个人在学习前锋教育-欧阳坚老师的iOS开发教程之OC语言教学视频所做的笔记,边看视频,边记录课程知识点.建议大家先过一遍视频,在看视频的过程中记录知识点关键字,把把握重点,然后再 ...