1、双向队列
描述
新建的 Beijing Institute Group 银行 (BIG-Bank) 在北理工开了一个营业点,他们装备了由 IBM Coral Studio 提供的现代化计算环境,使用现代的信息技术。通常,每个银行客户都有唯一的用来标识身份的正整数 K ,而每当他来到银行寻求服务时,银行都会给他一个正整数 P ,代表他的优先权。银行一个年轻的管理员李凌对软件服务系统的功能十分惊讶。他建议打破银行传统的最低优先权优先的服务方式,加入最高优先权优先的服务。于是,软件系统将得到如下类型的请求:
0 系统终止服务
1 K P 将客户 K 加到等待队列中,并赋优先权 P
2 为有最高优先权的人服务,并将他从等待队列中删除
3 为有最低优先权的人服务,并将他从等待队列中删除
银行的软件工程师张国文现在抽不开身,请你替他写一个程序实现这种服务策略。
输入
输入的每行包含一个可能的请求,只有最后一行是停止请求 ( 代码 0) 。你可以认为每个加入客户的请求 ( 代码 1) 中,赋予的优先权都是不同的。每个标识 K 都是小于 106 的数,优先权 P 都是小于 107 的数,客户可能请求多次服务,每次都会得到不同的优先权。
输出
对每个代码为 2 或 3 的请求,程序必须打印被服务的客户的标识。如果此时队列是空,那么打印 0 。
样例输入
2
1 20 14
1 30 3
2
1 10 99
3
2
2
0
样例输出
0
20
30
10
0
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
int cmd;
struct Node {
int Rank;
int key;
};
Node node;
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
int K, P;
deque<Node>Deque;
while (scanf("%d", &cmd) && cmd != 0) {
if (cmd == 1) {
scanf("%d%d", &K, &P);
node.key = K; node.Rank = P;
if (Deque.empty()) {
Deque.push_front(node);
} else {
if (P < Deque.front().Rank) {
Deque.push_front(node);
} else if (P > Deque.back().Rank) {
Deque.push_back(node);
} else {
deque<Node>::iterator itr;
for (itr = Deque.begin(); itr != Deque.end(); ++itr) {
if (itr->Rank > P) {
Deque.insert(itr, 1, node);
break;
}
}
}
}
} else if (cmd == 2) {
if (Deque.empty()) {
printf("0\n");
} else {
printf("%d\n", Deque.back().key);
Deque.pop_back();
}
} else {
if (Deque.empty()) {
printf("0\n");
} else {
printf("%d\n", Deque.front().key);
Deque.pop_front();
}
}
}
return 0;
}
2、nucoj 1926 队列极值
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 0x7fffffff;
const int maxn = 5e5 + 10;
int n, x, Case = 0;
char cmd[10];
struct Stack {
int cur, max_cur, min_cur;
int item[maxn], next_max[maxn], next_min[maxn];
Stack(int x = -1, int y = -1, int z = -1):cur(x),max_cur(y),min_cur(z){}
inline bool empty() { return cur == -1 ? true : false; }
inline int top() { return item[cur]; }
inline int Max() { return max_cur == -1 ? -INF : item[max_cur]; }
inline int Min() { return min_cur == -1 ? INF : item[min_cur]; }
inline void push(int x) {
++cur; item[cur] = x;
if (x > Max()) { next_max[cur] = max_cur; max_cur = cur; } else { next_max[cur] = -1; }
if (x < Min()) { next_min[cur] = min_cur; min_cur = cur; } else { next_min[cur] = -1; }
}
inline void pop() {
if (cur == max_cur) { max_cur = next_max[cur]; }
if (cur == min_cur) { min_cur = next_min[cur]; } --cur;
}
};
struct Queue {
Stack a, b;
inline bool empty() { if (a.empty() && b.empty()) { return true; } return false; }
inline void clear() { a.cur = a.max_cur = a.min_cur = b.cur = b.max_cur = b.min_cur = -1; }
inline void push(int x) { b.push(x); }
inline int front() {
if (a.empty()) { while (!b.empty()) { a.push(b.top()); b.pop(); } } return a.top();
}
inline void pop() {
if (a.empty()) { while (!b.empty()) { a.push(b.top()); b.pop(); } } a.pop();
}
inline int Max() { return max(a.Max(), b.Max()); }
inline int Min() { return min(a.Min(), b.Min()); }
};
Queue q;
int main() {
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // __AiR_H
while (scanf("%d", &n) && n != 0) {
printf("Case %d:\n", ++Case); q.clear();
while (n--) {
scanf("%s", cmd);
if (cmd[0] == 'E') { scanf("%d", &x); q.push(x); continue; }
if (q.empty()) { printf("EMPTY!\n"); continue; }
if (cmd[0] == 'D') { printf("%d\n", q.front()); q.pop(); }
else if (cmd[1] == 'A') { printf("%d\n", q.Max()); }
else { printf("%d\n", q.Min()); }
}
}
#ifdef __AiR_H
printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
return 0;
}