Uva 12207 - That is Your Queue

Sep 30, 2015

Problem: 12207 - That is Your Queue
Skill Develop: List (STL in C++)
* Only 1000 Command will be available in input file so handle only 1000 element or less.
Algorithm:
1. Initialize list
2. if 'N' character, then pop top element, print it and push into the back of the list
3. otherwise, remove the input element and just push the element into the font of the list.




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <cstdio>
#include <list>

using namespace std;


int main ()
{
    int p, c;
    int cases = 0;

    while ( scanf ("%d %d", &p, &c) ) {
        if ( p == 0 && c == 0 ) break;

        list <int> l;

        p = min (c, p);

        for ( int i = 1; i <= p; i++ ) l.push_back (i);

        char ch [10];
        printf ("Case %d:\n", ++cases);

        for ( int i = 0; i < c; i++ ) {
            scanf ("%s", ch);
            if ( ch [0] == 'N' ) {
                printf ("%d\n", l.front ());
                l.push_back (l.front ());
                l.pop_front ();
            }
            else {
                int num;
                scanf ("%d", &num);
                l.remove (num);
                l.push_front (num);
            }
        }
    }

    return 0;
}


No comments:

Post a Comment