队列、栈

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;
}












  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
队列的C语言代码: #include <stdio.h> #include <stdlib.h> #define MAXSIZE 5 typedef struct { int data[MAXSIZE]; int front; int rear; } Queue; void initQueue(Queue *Q) { Q->front = 0; Q->rear = 0; } int isFull(Queue Q) { return (Q.rear + 1) % MAXSIZE == Q.front; } int isEmpty(Queue Q) { return Q.front == Q.rear; } void enQueue(Queue *Q, int x) { if (isFull(*Q)) { printf("队列已满,无法入队!\n"); return; } Q->data[Q->rear] = x; Q->rear = (Q->rear + 1) % MAXSIZE; } int deQueue(Queue *Q) { if (isEmpty(*Q)) { printf("队列为空,无法出队!\n"); return -1; } int x = Q->data[Q->front]; Q->front = (Q->front + 1) % MAXSIZE; return x; } void printQueue(Queue Q) { int i = Q.front; printf("队列元素为:"); while (i != Q.rear) { printf("%d ", Q.data[i]); i = (i + 1) % MAXSIZE; } printf("\n"); } int main() { Queue Q; initQueue(&Q); enQueue(&Q, 1); enQueue(&Q, 2); enQueue(&Q, 3); printQueue(Q); deQueue(&Q); printQueue(Q); enQueue(&Q, 4); enQueue(&Q, 5); enQueue(&Q, 6); printQueue(Q); return 0; } 的C语言代码: #include <stdio.h> #include <stdlib.h> #define MAXSIZE 5 typedef struct { int data[MAXSIZE]; int top; } Stack; void initStack(Stack *S) { S->top = -1; } int isFull(Stack S) { return S.top == MAXSIZE - 1; } int isEmpty(Stack S) { return S.top == -1; } void push(Stack *S, int x) { if (isFull(*S)) { printf("已满,无法入!\n"); return; } S->top++; S->data[S->top] = x; } int pop(Stack *S) { if (isEmpty(*S)) { printf("为空,无法出!\n"); return -1; } int x = S->data[S->top]; S->top--; return x; } int getTop(Stack S) { if (isEmpty(S)) { printf("为空,无法取顶元素!\n"); return -1; } return S.data[S.top]; } void printStack(Stack S) { printf("元素为:"); while (!isEmpty(S)) { printf("%d ", pop(&S)); } printf("\n"); } int main() { Stack S; initStack(&S); push(&S, 1); push(&S, 2); push(&S, 3); printStack(S); push(&S, 4); push(&S, 5); push(&S, 6); printStack(S); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值