Monday, August 25, 2014

uva : 540 - Team Queue

#include < cstdio > 
#include < iostream >
#include < cmath > 
#include < map > 
#include < queue >
#include < algorithm >
#include < vector >
using namespace std;
int main() {
    int team, y = 1;
    while (scanf("%d", & team) == 1 && team != 0) {
        printf("Scenario #%d\n", y++);
        queue < int > q[team + 1];
        int i, j, k, l = 0, m, n, co = 0, num[200003];
        map < int, int > number;
        map < int, int > index;
        queue < int > start;
        for (i = 1; i <= team; i++) {
            scanf("%d", & n);
            for (j = 0; j < n; j++) {
                scanf("%d", & m);
                number[m] = i;
            }
        }
        string s;
        while (cin >> s && (s != "STOP")) {
            if (s == "ENQUEUE") {
                scanf("%d", & n);
                m = number[n];
                q[m].push(n);
                for (i = l; i < co; i++) {
                    if (num[i] == m) break;
                }
                if (i == co) {
                    num[co++] = m;
                }
            }
            if (s == "DEQUEUE") {
                int f = 0;
                for (i = l; i < co; i++) {
                    m = num[i];
                    while (!q[m].empty()) {
                        printf("%d\n", q[m].front());
                        q[m].pop();
                        f = 1;
                        break;
                    }
                    if (q[m].empty()) l++;
                    if (f == 1) break;
                }
            }
        }
        printf("\n");
    }
    return 0;
}

No comments:

Post a Comment