2013年8月10日

UVa 12207 - That is Your Queue

#include <cstdio>
#include <deque>
#define PF push_front
#define PB push_back
using namespace std;

int main() {
  int Case = 1, P, C;
  while (scanf("%d%d", &P, &C) && P || C) {
    deque<int> v;
    if (P <= 2000) {
      for (int i = 1; i <= P; i++) {
        v.PB(i);
      }
    } else {
      for (int i = 1; i <= 1000; i++) {
        v.PB(i);
      }
      for (int i = P - 999; i <= P; i++) {
        v.PB(i);
      }
    }
    printf("Case %d:\n", Case++);
    char s[9];
    while (C--) {
      scanf("%s", s);
      if (s[0] == 'N') {
        int first = v.front();
        while (!first) {
          v.pop_front();
          first = v.front();
        }
        printf("%d\n", first);
        v.pop_front();
        v.PB(first);
      } else if (s[0] == 'E') {
        int x;
        scanf("%d", &x);
        for (int i = 0; i < v.size(); i++) {
          if (v[i] == x) {
            v[i] = 0;
          }
        }
        v.PF(x);
      }
    }
  }
  return 0;
}

沒有留言:

張貼留言