Dynamic Array
InterviewTime limit1sMemory limit1024 MB
Simulate store and delete commands on a dynamic array that doubles when full, then print its final maximum size.
- Level
Easy2 of 10
- Topics
- Implementation, Simulation, Array
- Solved
- No attempts yet
Problem
Do you know about dynamic arrays? If not, you have probably declared arrays with a fixed size. You may have run into a case where the array was too small and the judge returned an unwanted result. A dynamic array grows automatically when its declared size is not enough. The following explains how this works.
A dynamic array operates as follows.
- Create a dynamic array with any initial size you want. The dynamic array starts empty.
- When you need to store an element but the dynamic array is full, declare a new dynamic array whose size is times the current size.
- Copy all elements of the existing dynamic array to the front of the new -times-size array. Then delete the existing array and replace it with the new one. Store the element from step 2 after the copied elements.
To do this, a dynamic array manages two variables. is the current maximum size, and is the number of slots currently in use.
Given the initial size of a dynamic array and a series of store or delete commands, print the maximum size of the array after all commands are processed.
Input
The first line contains three integers , , and , separated by spaces. is the initial maximum size, is the number of store commands, and is the number of delete commands.
From the second line through line , each line contains either 1 or 0. 1 stores an element in the array. 0 deletes an element from the end of the array.
A delete command is never given when the dynamic array has no elements.
Output
Print the maximum size of the dynamic array after all commands are processed.
Hint
A well-known example of a dynamic array is the vector in C++.