数据结构 领取礼品的顺序 (STL+模拟)

Description

很多购物网站或者商场经常会搞些所谓的先来先得之类的促 销。在这种促销下,该网站或商场许诺给客户准备礼物,但因为礼物有限,只能先来先得,送完为止。可以模拟这个先来先得场景:每个客户用一个编号来表示,用 户给出的客户编号的顺序就是客户访问网站或者商场的次序,设计的程序就是输出这一串编号,即获得礼品的客户顺序。

Input

输入包含多组数据,第一行为数据组数。对 于每组数据,第一行输入两个整数n(2<=n<=1000)、k(1<=k<=1000)分别代表n次事件,和最初有k个礼品, 接下来n行输入,每行需要输入的第一个整数m(-100<=m<=300)代表礼品发放还是加入等待队列的人数。当输入m为0时代表发放礼 品,要么发光所有礼品,要么发完队列中所有的人;当输入的m>0时表示有m个新到的用户进入队列,后面紧跟m个整数,代表m个来购物的客户的编号 (编号可能有重复);当m<0时代表又来了|m|个礼物。

Output

对于每组数据,第一行输出数据组数,然后对于每个m为0的操作,输出领取奖品的人数和编号,具体格式见样例。

Sample Input

1
2 3
3 1 2 3
0

Sample Output

Case #1 :
3 1 2 3

HINT

简单的队列应用,复杂度O(n)。


Append Code

析:就是一个队列,FIFO,然后模拟就行了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <list>
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std;
 
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
queue<int> q;
 
int main(){
    int T;  cin >> T;
    for(int kase = 1; kase <= T; ++kase){
        printf("Case #%d :
", kase);
        while(!q.empty())  q.pop();
        int k, x, y;
        scanf("%d %d", &n, &k);
        for(int i = 0; i < n; ++i){
            scanf("%d", &x);
            if(!x){
                if(k < q.size()){
                    printf("%d", k);
                    for(int j = 0; j < k; ++j){
                        printf(" %d", q.front());
                        q.pop();
                    }
                    k = 0;
                }
                else{
                    printf("%d", q.size());
                    k -= (int)q.size();
                    while(!q.empty()){
                        printf(" %d", q.front());
                        q.pop();
                    }
                }
                printf("
");
            }
            else if(x < 0)  k += -x;
            else{
                for(int j = 0; j < x; ++j){
                    scanf("%d", &y);
                    q.push(y);
                }
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/5875544.html