题目连接

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的更多相关文章

  1. POJ 3481 &amp; HDU 1908 Double Queue (map运用)

    题目链接: PKU:http://poj.org/problem?id=3481 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1908 Descript ...

  2. HDU 1908 Double Queue(set)

    Problem Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in B ...

  3. 【HDOJ】1908 Double Queue

    双端队列+二分. #include <cstdio> #define MAXN 1000005 typedef struct { int id; int p; } node_st; nod ...

  4. poj 3841 Double Queue (AVL树入门)

    /****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...

  5. treap树---Double Queue

    HDU   1908 Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office i ...

  6. POJ 3481 Double Queue STLmap和set新学到的一点用法

    2013-08-08 POJ 3481  Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...

  7. 【Map】Double Queue

    Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13258   Accepted: 5974 Des ...

  8. POJ 3481 Double Queue(STL)

    题意  模拟银行的排队系统  有三种操作  1-加入优先级为p 编号为k的人到队列  2-服务当前优先级最大的   3-服务当前优先级最小的  0-退出系统 能够用stl中的map   由于map本身 ...

  9. POJ 3481 Double Queue(set实现)

    Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Buchares ...

随机推荐

  1. mistral 工作流组件之二 思维导图

    Mistral 思维导图

  2. org.springframework.core.NestedIOException

    今天遇到的一个小异常,报错信息如下: 2013-11-13 10:00:32 org.apache.catalina.core.StandardContext listenerStart严重: Exc ...

  3. 注册码 myeclipse6.5-6.8

    package controllersli; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...

  4. Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance 7d90eb80-29e2-4238-b658-ade407ff9456. Last exception: [u'Traceback (most recent call last):\n', u' File "/usr/lib/py

    Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance 7d90eb80-29e2-42 ...

  5. SQLServer 窗口函数

    一.窗口函数的作用 窗口函数是对一组值进行操作,不需要使用GROUP BY 子句对数据进行分组,还能够在同一行中同时返回基础行的列和聚合列.窗口函数,基础列和聚合列的查询都非常简单. 二.语法格式 窗 ...

  6. 快速搭建 Node.js 开发环境以及加速 npm

    如何快速搭建 node 开发环境 npm 超慢 github 无法打开的问题 于是我觉得应该写一篇文章解答所有这些起步问题,让新同学也能顺顺利利入门. 快速搭建 Node.js 开发环境 如果你想长期 ...

  7. c++ 实现百度自动搜索

    void CAttendanceRobotDlg::DocumentCompleteExplorer4(LPDISPATCH pDisp, VARIANT* URL) { // TODO: Add y ...

  8. Android IOS WebRTC 音视频开发总结(四五)-- ORTC背后的真相

    本文主要介绍ORTC(Object Real-time Communication),支持原创,文章来自博客园RTC.Blacker,支持原创,转载必须说明出处,更多详见www.rtc.help. - ...

  9. mysql报错Table '.\erchina_news\v9_search' is marked as crashed and should be repaired

    直切正题 报该问题的是表引导坏了,需要修复表就行 方法一: 找到mysql的安装目录的bin/myisamchk工具,在命令行中输入: myisamchk -c -r ../data/erchina_ ...

  10. Map(双列集合)

    出现的原因:现实生活中有些数据成对存在. 特点:键不可重复,值可以重复. ----------|Map                数据都是以键值对的形式存在,键唯一,值可重复. --------- ...