D. Packmen Strike Back

Codeforces
IDCF883D
Time3000ms
Memory256MB
Difficulty
binary searchdpmath
English · Original
Chinese · Translation
Formal · Original
Game field is represented by a line of _n_ square cells. In some cells there are packmen, in some cells there are asterisks and the rest of the cells are empty. Packmen eat asterisks. Before the game starts you can choose a movement direction, left or right, for each packman. Once the game begins all the packmen simultaneously start moving according their directions. A packman can't change the given direction. Once a packman enters a cell containing an asterisk, packman immediately eats the asterisk. Once the packman leaves the cell it becomes empty. Each packman moves at speed 1 cell per second. If a packman enters a border cell, the packman stops. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions. Your task is to assign a direction to each packman so that they eat the maximal number of asterisks. If there are multiple ways to assign directions to eat the maximal number of asterisks, you should choose the way which minimizes the time to do that. ## Input The first line contains integer number _n_ (2 ≤ _n_ ≤ 1 000 000) — the number of cells in the game field. The second line contains _n_ characters. If the _i_\-th character is '_._', the _i_\-th cell is empty. If the _i_\-th character is '_*_', the _i_\-th cell contains an asterisk. If the _i_\-th character is '_P_', the _i_\-th cell contains a packman. The field contains at least one asterisk and at least one packman. ## Output Print two integer numbers — the maximal number of asterisks packmen can eat and the minimal time to do it. [samples] ## Note In the first example the leftmost packman should move to the right, the rightmost packman should move to the left. All the asterisks will be eaten, the last asterisk will be eaten after 4 seconds.
游戏场地由一条包含 #cf_span[n] 个方格的直线表示。某些格子中有包人(packman),某些格子中有星号(asterisk),其余格子为空。包人会吃掉星号。 游戏开始前,你可以为每个包人选择一个移动方向:左或右。游戏开始后,所有包人同时按照各自的方向移动,且一旦选定方向便不可更改。 当一个包人进入包含星号的格子时,会立即吃掉该星号。包人离开格子后,该格子变为空。每个包人以每秒 1 格的速度移动。若包人到达边界格子,则停止移动。包人之间互不干扰;一个格子中可以同时存在任意数量、朝任意方向移动的包人。 你的任务是为每个包人分配一个方向,使得它们吃掉的星号数量最大化。如果有多种分配方式都能达到最大星号数量,则选择耗时最少的方案。 第一行包含一个整数 #cf_span[n](#cf_span[2 ≤ n ≤ 1 000 000])——游戏场地的格子数量。 第二行包含 #cf_span[n] 个字符。若第 #cf_span[i] 个字符是 '_._',则第 #cf_span[i] 个格子为空;若为 '_*_',则包含一个星号;若为 '_P_',则包含一个包人。 场地中至少包含一个星号和一个包人。 请输出两个整数:包人能吃掉的最大星号数量,以及达成该数量所需的最小时间。 在第一个示例中,最左边的包人应向右移动,最右边的包人应向左移动。所有星号都将被吃掉,最后一个星号将在 4 秒后被吃掉。 ## Input 第一行包含一个整数 #cf_span[n](#cf_span[2 ≤ n ≤ 1 000 000])——游戏场地的格子数量。第二行包含 #cf_span[n] 个字符。若第 #cf_span[i] 个字符是 '_._',则第 #cf_span[i] 个格子为空;若为 '_*_',则包含一个星号;若为 '_P_',则包含一个包人。场地中至少包含一个星号和一个包人。 ## Output 请输出两个整数:包人能吃掉的最大星号数量,以及达成该数量所需的最小时间。 [samples] ## Note 在第一个示例中,最左边的包人应向右移动,最右边的包人应向左移动。所有星号都将被吃掉,最后一个星号将在 4 秒后被吃掉。
**Definitions** Let $ n \in \mathbb{Z} $, $ 2 \leq n \leq 10^6 $, be the number of cells. Let $ F = (f_1, f_2, \dots, f_n) $ be the field, where $ f_i \in \{ \text{'.', '*', 'P'} \} $. Let $ P = \{ p_1, p_2, \dots, p_m \} \subseteq \{1, 2, \dots, n\} $ be the sorted positions of packmen ($ m \geq 1 $). Let $ A = \{ a_1, a_2, \dots, a_k \} \subseteq \{1, 2, \dots, n\} $ be the sorted positions of asterisks ($ k \geq 1 $). **Constraints** Each packman at position $ p_j $ is assigned a direction $ d_j \in \{ \text{left}, \text{right} \} $. A packman at $ p_j $ moving left covers $ [1, p_j] $, moving right covers $ [p_j, n] $. A packman stops at the boundary. Packmen move simultaneously at speed 1 cell/second. A packman eats an asterisk the moment it enters its cell. Packmen do not block each other. **Objective** Maximize the number of eaten asterisks, and among all such assignments, minimize the time $ T $ when the last asterisk is eaten. Formally, find: $$ \max_{d \in \{L,R\}^m} \left| \bigcup_{j=1}^m \text{Covered}_j(d_j) \cap A \right|, \quad \min_{d} \max_{a \in A \cap \bigcup_j \text{Covered}_j(d_j)} \text{Time}(p_j, a, d_j) $$ where $ \text{Covered}_j(\text{left}) = [1, p_j] $, $ \text{Covered}_j(\text{right}) = [p_j, n] $, and $ \text{Time}(p_j, a, \text{left}) = p_j - a $ if $ a \leq p_j $, $ \text{Time}(p_j, a, \text{right}) = a - p_j $ if $ a \geq p_j $.
Samples
Input #1
6
*.P*P*
Output #1
3 4
Input #2
8
*...P..*
Output #2
1 3
API Response (JSON)
{
  "problem": {
    "name": "D. Packmen Strike Back",
    "description": {
      "content": "Game field is represented by a line of _n_ square cells. In some cells there are packmen, in some cells there are asterisks and the rest of the cells are empty. Packmen eat asterisks. Before the game",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 3000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF883D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Game field is represented by a line of _n_ square cells. In some cells there are packmen, in some cells there are asterisks and the rest of the cells are empty. Packmen eat asterisks.\n\nBefore the game...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "游戏场地由一条包含 #cf_span[n] 个方格的直线表示。某些格子中有包人(packman),某些格子中有星号(asterisk),其余格子为空。包人会吃掉星号。\n\n游戏开始前,你可以为每个包人选择一个移动方向:左或右。游戏开始后,所有包人同时按照各自的方向移动,且一旦选定方向便不可更改。\n\n当一个包人进入包含星号的格子时,会立即吃掉该星号。包人离开格子后,该格子变为空。每个包人以每秒 1 格...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $, $ 2 \\leq n \\leq 10^6 $, be the number of cells.  \nLet $ F = (f_1, f_2, \\dots, f_n) $ be the field, where $ f_i \\in \\{ \\text{'.', '*', 'P'} \\} $.  \nLet $ P =...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments