15008 - Printer War   

Description

There’s a printing war going on in The Office. Everyone in the office can no longer print whatever and whenever they want. Michael and Dwight are the protector of the printer, and they need your help to manage the printing queue!

You will receive Q commands, and each command is either SUBMIT, PRINT, CANCEL, and STATUS . Here are the rules in details:

  • For SUBMIT, the command shape will look like this: SUBMIT <user> <file> <pages>
    • <file> must be <name>.<ext>, where:
      • <name> is at least 1 character, using only ABCDEFGHIJKLMNOPQRSTUVWXYZ, abcdefghijklmnopqrstuvwxyz, 0123456789, _- and <ext> is exactly pdf, txt or docx (lowercase). Otherwise, output REJECT BADNAME.
    • <pages> must be digits only, 1 ≤ pages ≤ 100, if it is not within range, output REJECT BADPAGES .
    • Each employee has a quota to submit the pages that they want to print, and that is 50 pages total; if exceed, then output REJECT QUOTA .
    • If the submission is a success, you output OK <id> . The <id> is the i-th validated printing job in the queue.
    • <user> is 1-20 alphanumeric characters, case-sensitive.
  • For PRINT , the command shape will look like this: PRINT <n>
    • You need to print out/remove (or pop, in a queueing term) n jobs (n is integer), from the oldest to the latest.
    • If there are any printing queued, then you have to output PRINTED <id> <user> <file> . Printing fewer than n jobs is not an error and does not produce IDLE.
    • If queue was empty, output IDLE .
  • For CANCEL , the command shape will look like this: CANCEL <user>
    • You need to remove all that user’s jobs still in the queue, and refund their quota. You need to output CANCELLED <count> .
  • For STATUS, the command shape will look like this: STATUS
    • You need to output QUEUE <len> followed with a line of the front job NEXT <id> <user> .
    • If there’s no queue, then output NEXT NONE .
  • You need to output TOTAL <pages_printed> after all the commands have finished being inputted, followed with line(s) of who printed ≥ 1 page, sorted by pages descending, and then name ascending, i.e. <user> <pages> .
  • You also need to make sure that the commands are in the proper shape, if not, output REJECT BADREQUEST .

Michael and Dwight thank your services!

Note: Crash Course about Loop: https://hackmd.io/@gladysvalerie/S1wWLYCYze

Input

First line: integer Q (1 ≤ Q ≤ 1000)

Next Q lines: one command each, tokens separated by single spaces, according to the rules explained in the description.

Anything else described from the description, outputs REJECT BADREQUEST and the command is ignored.

Command keywords are uppercase.

Output

Per command, checked in this exact order:

SUBMIT

  1. <file> fails ^[A-Za-z0-9_-]+\.(pdf|txt|docx)$REJECT BADNAME
  2. <pages> not digits, or not 1 ≤ pages ≤ 100 → REJECT BADPAGES
  3. used[user] + pages > 50REJECT QUOTA
  4. otherwise → OK <id>

Ids start at 1 and increase by 1 per accepted job only; rejected submissions never consume an id. A job keeps its id after leaving the queue.

PRINT <n>

  • Queue empty → IDLE (one line, regardless of n).
  • Otherwise pop min(n, queue_length) jobs from the front, one PRINTED <id> <user> <file> line each. n = 0 on a non-empty queue outputs nothing.

CANCEL <user>

  • Remove all of that user's queued jobs, refund those pages to their quota, output CANCELLED <count>. Unknown user → CANCELLED 0. Already-printed pages are never refunded.

STATUS

  • QUEUE <len>, then NEXT <id> <user> for the front job, or NEXT NONE if empty.

After all Q commands:

  • TOTAL <pages_printed> — exactly once.
  • Then one line <user> <pages> per user who printed ≥ 1 page, sorted by pages descending, then username ascending. Users with 0 printed pages are omitted entirely.

Sample Input  Download

Sample Output  Download




Discuss