J. Narrow Bus

Codeforces
IDCF10079J
Time2000ms
Memory256MB
Difficulty
English · Original
Formal · Original
The narrow bus is so narrow it doesn't have any seats and the passengers have to stand in a row. The bus has two doors that can be used for entering or leaving the bus: one in the front and one in the back. People can't easily swap in the bus because it is too narrow, so the order people are standing in does not change while the bus is running. At each bus stop, either a single person enters the bus or a single person leaves the bus. Given a description of people entering and leaving the bus, count how many people will have to get out when somebody leaves the bus. The first line of input contains a single integer n, the number of actions at bus stops (1 ≤ n ≤ 105). The next n lines will contain one of the three types of actions each. The actions happen consecutively as they are given in the input. Each action happens at a single bus stop. After the action is completed, the bus leaves for the next stop. Print a single integer for each type "_O_" query, the number of people who will have to get out and go back in the bus when a person leaves at the stop. In the example, after everyone gets in the bus during the first 5 stops, the people are ordered 1, 2, 3, 4, 5. At the 6th stop, the 2nd person leaves through the front door and the 1st person is forced to get out of the bus. The 1st person then uses the back door to enter the bus again. The order changes to 3, 4, 5, 1. During the next two stops two people use the front door to enter the bus, and the order becomes 7, 6, 3, 4, 5, 1. At the 9th stop, the 4th person leaves through the back door and now the 5th and the 1st person are forced to get out of the bus. They then use the front door to get back in the bus. The order changes to 5, 1, 7, 6, 3. ## Input The first line of input contains a single integer n, the number of actions at bus stops (1 ≤ n ≤ 105). The next n lines will contain one of the three types of actions each. The actions happen consecutively as they are given in the input. "_F_" — a person enters the bus using the front door. "_B_" — a person enters the bus using the back door. "_O_ i" — the i-th person that has entered the bus today leaves. Each action happens at a single bus stop. After the action is completed, the bus leaves for the next stop. ## Output Print a single integer for each type "_O_" query, the number of people who will have to get out and go back in the bus when a person leaves at the stop. [samples] ## Note In the example, after everyone gets in the bus during the first 5 stops, the people are ordered 1, 2, 3, 4, 5.At the 6th stop, the 2nd person leaves through the front door and the 1st person is forced to get out of the bus. The 1st person then uses the back door to enter the bus again. The order changes to 3, 4, 5, 1.During the next two stops two people use the front door to enter the bus, and the order becomes 7, 6, 3, 4, 5, 1.At the 9th stop, the 4th person leaves through the back door and now the 5th and the 1st person are forced to get out of the bus. They then use the front door to get back in the bus. The order changes to 5, 1, 7, 6, 3.
**Definitions** Let $ n \in \mathbb{Z} $ be the number of actions. Let $ A = (a_1, a_2, \dots, a_n) $ be a sequence of actions, where each $ a_i \in \{ \text{"I"}, \text{"F"}, \text{"B"} \} $: - "I" denotes a person entering the bus (at the end of the line). - "F" denotes a person leaving through the **front** door (the first person in line exits). - "B" denotes a person leaving through the **back** door (the last person in line exits). Let $ L = (p_1, p_2, \dots, p_m) $ be the current line of passengers, ordered from front to back, where $ p_1 $ is the frontmost and $ p_m $ is the rearmost. Initially, $ L = \emptyset $. Let $ \text{exit\_count} $ be the total number of people forced to exit and re-enter the bus during all "F" and "B" actions. **Constraints** 1. $ 1 \le n \le 10^5 $ 2. For each action $ a_i $: - If $ a_i = \text{"I"} $, a new person enters at the back of $ L $. - If $ a_i = \text{"F"} $, the frontmost person $ p_1 $ exits; all persons from $ p_2 $ to $ p_m $ must exit and re-enter via the back door. - If $ a_i = \text{"B"} $, the rearmost person $ p_m $ exits; all persons from $ p_1 $ to $ p_{m-1} $ must exit and re-enter via the front door. **Objective** For each action $ a_i = \text{"F"} $ or $ a_i = \text{"B"} $, compute the number of people forced to exit and re-enter: - If $ a_i = \text{"F"} $: output $ |L| - 1 $ - If $ a_i = \text{"B"} $: output $ |L| - 1 $ Output the **sum** of all such values over all "F" and "B" actions.
API Response (JSON)
{
  "problem": {
    "name": "J. Narrow Bus",
    "description": {
      "content": "The narrow bus is so narrow it doesn't have any seats and the passengers have to stand in a row. The bus has two doors that can be used for entering or leaving the bus: one in the front and one in the",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10079J"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "The narrow bus is so narrow it doesn't have any seats and the passengers have to stand in a row. The bus has two doors that can be used for entering or leaving the bus: one in the front and one in the...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $ be the number of actions.  \nLet $ A = (a_1, a_2, \\dots, a_n) $ be a sequence of actions, where each $ a_i \\in \\{ \\text{\"I\"}, \\text{\"F\"}, \\text{\"B\"} \\} $:  \n-...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments