0% found this document useful (0 votes)
18 views

CC31F-1 Lesson

Uploaded by

keremtuzel.kt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

CC31F-1 Lesson

Uploaded by

keremtuzel.kt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CC31F-1 Lesson 04

Robot World
Farmer John's storage is a very tall but narrow room that only
one haybale can fit with respect to the width and the length. So SAMPLE INPUT
the haybales can only be stored by stacking them starting from
the floor. Initially the storage is empty and there are haybales 20
numbered starting from 1 in the warehouse. ADD
ADD Simulation Problem
FJ has a robot equipped with one arm that can hold one haybale. ADD
He controls the robot with two commands: ADD and REMOVE. With ADD
ADD command, the robot takes the next haybale in the warehouse REMOVE
and puts it on top of the haybales in the storage. When it ADD
receives REMOVE command it takes the haybale on the top in the ADD
storage and carries it to the barn. REMOVE
REMOVE
At the end of the week, FJ wants to learn the status of the REMOVE
storage in terms of the list of the haybales. Your program will ADD
help FJ to find out the list of the haybales. For example, given ADD
the sequence of FJ's commands throughout the week, the list of REMOVE
the haybales in the storage will be as follows: REMOVE
REMOVE
Command Storage REMOVE
------- ---------- ADD
ADD 1 ADD
ADD 1 2 REMOVE
ADD 1 2 3 ADD
ADD 1 2 3 4
REMOVE 1 2 3
ADD 1 2 3 5
ADD 1 2 3 5 6
REMOVE 1 2 3 5
REMOVE 1 2 3
REMOVE 1 2 Stack Data Structure:
ADD 1 2 7
ADD 1 2 7 8 Operations
REMOVE 1 2 7 push(x) O(1)
REMOVE 1 2 x = pop() O(1)
REMOVE 1 size() O(1)
REMOVE
ADD 9
ADD 9 10
REMOVE 9
ADD 9 11
Finally, the haybales numbered 9 and 11 will remain in the
storage.

INPUT FORMAT

Line 1: A single integer: N <= 1,000,000, the number FJ's


commands
Lines 2..N+1: The sequence of FJ's commands
SAMPLE INPUT

20
ADD
ADD
ADD
ADD
REMOVE
ADD
ADD
REMOVE
REMOVE
REMOVE
ADD
ADD
REMOVE
REMOVE
REMOVE
REMOVE
ADD
ADD
REMOVE
ADD
OUTPUT FORMAT

Line 1: M, the number of haybales remained in the storage


Lines 2..M+1: The list of haybales starting from bottom to top.
SAMPLE OUTPUT

2
9
11

Registers in the Shop SAMPLE INPUT


Bessie likes shopping at CowMart. She is also
curious about the payment system at the registers. 3
Registers in the Shop SAMPLE INPUT
Bessie likes shopping at CowMart. She is also
curious about the payment system at the registers. 3
There are less than 1,000 customers in the store. C 1
When a customer want to pay, she goes to the the end C 5
of the line. When a register is available,the next C 3
customer in front of the line goes to the register R 3
and pays. Bessie would like to know which customers C 2
payed at which register at the end of the day. R 3
Initially there are no customers in the store. A C 5
customer can pay then buy something again. In that R 2
case, the customer has to enter the line at the end C 4
again. The number of registers in the store is 0 < N C 1
<= 30. R 3
C 3
As an example, there are 5 customers and 3 registers R 2
in the store. The sequence of available registers R 1
and customers to enter the line is given as follows: C 5
C 2
Process Line Register 1 Register 2 Register R 2
3 R 2
======= ==== ========== ========== R 1
========== R 1
C 1 1
C 5 1 5
C 3 1 5 3
R 3 5 3 1
C 2 5 3 2 1
R 3 3 2 1 5
C 5 3 2 5 1 5
R 2 2 5 3 1 5
C 4 2 5 4 3 1 5
C 1 2 5 4 1 3 1 5
R 3 5 4 1 3 1 5 2
C 3 5 4 1 3 3 1 5 2
R 2 4 1 3 3 5 1 5 2
R 1 1 3 4 3 5 1 5 2
C 5 1 3 5 4 3 5 1 5 2
C 2 1 3 5 2 4 3 5 1 5 2
R 2 3 5 2 4 3 5 1 1 5 2
R 2 5 2 4 3 5 1 3 1 5 2
R 1 2 4 5 3 5 1 3 1 5 2
R 1 4 5 2 3 5 1 3 1 5 2
Here 'C' corresponds to the customer and 'R'
corresponds to the register. Then the list of the
customers payed at the registers are as follows:

Register 1: 4 5 2
Register 2: 3 5 1 3
Register 3: 1 5 2
INPUT FORMAT

Line 1: N, the number of registers in the store.


Line 2..end-of-file: Each line is ither C then
followed by customer number or R then followed by
register number. Number of lines is less than
10,000.
SAMPLE INPUT

3
C 1
C 5
C 3
R 3
C 2
R 3
C 5
R 2
C 4
C 1
R 3
C 3
R 2
R 1
C 5
C 2
R 2
R 2
R 1
R 1

OUTPUT FORMAT

Line 1..N: Line i has R_i, the number of customers


in that register, then R_i numbers corresponding to
the list of the customers payed at register i
ordered from the earliest to latest.

SAMPLE OUTPUT
3 4 5 2
4 3 5 1 3
3 1 5 2

Line Dancing
[ Memory: 16 MB, CPU: 1 sec ]

There are N students conveniently numbered 1..N forming a line. The Read/understand the problem and share your
line is initially empty, and students join one by one to the left or solution idea.
right side. Sometimes, a number of students on the left or right side
of the line leave.

The students enter the line in numerical order 1..N. Once a student
leaves the line they never re-enter it.

You will be given S (1 <= S <= 100,000) input specifications, either


specifying that a student enters the line (with a parameter
indicating whether on the left or right), or K students leave the
line (with parameters indicating whether from the left or right side
and how many students leave). The input never requests an operation
that is impossible.

Determine the order of the students in the line after all input has
been processed. Print the answer from left to right. The answer is
guaranteed to be non-empty.

INPUT FORMAT

Line 1: A single integer S (1 <= S <= 100,000)


Lines 2..S+1: Line i+1 contains query i in one of four formats:
A L -- a student enters the left side of the line
A R -- a student enters the right side of the line
D L K -- K students depart the left side of the line
D R K -- K students depart the right side of the line
OUTPUT FORMAT

Line 1..?: Print the numbers of the students in the line from left to
right, one number per line

SAMPLE INPUT

7
A L
A R
A L
A R
D R 2
A R
D L 1
SAMPLE OUTPUT

1
5
OUTPUT DETAILS

Input Resulting Student Line


A L 1
A R 1 2
A L 3 1 2
A R 3 1 2 4
D R 2 3 1
A R 3 1 5
D L 1 1 5

Homework: Implement the past problems + Speed codding problems.

You might also like