B. Wrath

Codeforces
IDCF892B
Time2000ms
Memory256MB
Difficulty
greedyimplementationtwo pointers
English · Original
Chinese · Translation
Formal · Original
_Hands that shed innocent blood!_ There are _n_ guilty people in a line, the _i_\-th of them holds a claw with length _L__i_. The bell rings and every person kills some of people in front of him. All people kill others at the same time. Namely, the _i_\-th person kills the _j_\-th person if and only if _j_ < _i_ and _j_ ≥ _i_ - _L__i_. You are given lengths of the claws. You need to find the total number of alive people after the bell rings. ## Input The first line contains one integer _n_ (1 ≤ _n_ ≤ 106) — the number of guilty people. Second line contains _n_ space-separated integers _L_1, _L_2, ..., _L__n_ (0 ≤ _L__i_ ≤ 109), where _L__i_ is the length of the _i_\-th person's claw. ## Output Print one integer — the total number of alive people after the bell rings. [samples] ## Note In first sample the last person kills everyone in front of him.
_Hands that shed innocent blood!_ 有 #cf_span[n] 个有罪的人排成一列,第 #cf_span[i] 个人手持一把长度为 #cf_span[Li] 的爪子。钟声响起,每个人都会杀死他前面的一些人。所有人同时行动。具体来说,第 #cf_span[i] 个人会杀死第 #cf_span[j] 个人,当且仅当 #cf_span[j < i] 且 #cf_span[j ≥ i - Li]。 给你每个人的爪子长度,你需要求出钟声响起后仍然活着的人的总数。 第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 106]) —— 有罪之人的数量。 第二行包含 #cf_span[n] 个用空格分隔的整数 #cf_span[L1, L2, ..., Ln] (#cf_span[0 ≤ Li ≤ 109]),其中 #cf_span[Li] 是第 #cf_span[i] 个人的爪子长度。 请输出一个整数——钟声响起后仍然活着的人的总数。 在第一个样例中,最后一个人杀死了他前面的所有人。 ## Input 第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 106]) —— 有罪之人的数量。第二行包含 #cf_span[n] 个用空格分隔的整数 #cf_span[L1, L2, ..., Ln] (#cf_span[0 ≤ Li ≤ 109]),其中 #cf_span[Li] 是第 #cf_span[i] 个人的爪子长度。 ## Output 请输出一个整数——钟声响起后仍然活着的人的总数。 [samples] ## Note 在第一个样例中,最后一个人杀死了他前面的所有人。
Let $ n $ be the number of people, indexed from $ 1 $ to $ n $. Let $ L_i $ denote the claw length of the $ i $-th person. For each person $ i $, they kill all persons $ j $ such that: $$ j < i \quad \text{and} \quad j \geq i - L_i $$ That is, person $ i $ kills all people in the range $ [\max(1, i - L_i), i - 1] $. A person $ j $ is alive after the bell rings if and only if **no** person $ i > j $ kills them, i.e., for all $ i > j $, it is **not** true that $ j \in [i - L_i, i - 1] $. Equivalently, person $ j $ is killed if there exists some $ i > j $ such that: $$ i - L_i \leq j < i $$ Thus, person $ j $ is **alive** if and only if: $$ \forall i > j, \quad j < i - L_i \quad \text{or} \quad j \geq i $$ But since $ j < i $ always holds for $ i > j $, the condition simplifies to: $$ \forall i > j, \quad j < i - L_i \quad \iff \quad \forall i > j, \quad L_i < i - j $$ Alternatively, person $ j $ is **killed** if: $$ \exists i > j \text{ such that } L_i \geq i - j $$ So, person $ j $ is alive if and only if: $$ \max_{i > j} (i - L_i) > j $$ is **false** — that is, if: $$ \max_{i > j} (i - L_i) \leq j $$ But more directly: Define for each $ i $, the leftmost index that person $ i $ can kill: $ \ell_i = \max(1, i - L_i) $. Then person $ j $ is killed if there exists $ i > j $ such that $ \ell_i \leq j $. So, person $ j $ is alive if and only if: $$ \min_{i > j} \ell_i > j $$ Thus, the number of alive people is: $$ \left| \left\{ j \in \{1, 2, \dots, n\} \mid \min_{i > j} \max(1, i - L_i) > j \right\} \right| $$ Note: For $ j = n $, there is no $ i > j $, so the minimum over an empty set is $ \infty $, so person $ n $ is always alive. We can compute this efficiently by processing from right to left. Let $ R_j = \min_{i > j} \max(1, i - L_i) $, with $ R_n = \infty $. Then: - Person $ j $ is alive $ \iff R_j > j $ We can compute $ R_j $ iteratively from $ j = n-1 $ down to $ 1 $: $$ R_j = \min\left( \max(1, j+1 - L_{j+1}), R_{j+1} \right) $$ **Final formal statement:** Let $ n \in \mathbb{N} $, $ L_1, \dots, L_n \in \mathbb{Z}_{\geq 0} $. Define $ \ell_i = \max(1, i - L_i) $ for $ i = 1, \dots, n $. Define $ R_n = \infty $, and for $ j = n-1, \dots, 1 $: $$ R_j = \min(\ell_{j+1}, R_{j+1}) $$ Then the number of alive people is: $$ \sum_{j=1}^n \mathbf{1}_{\{R_j > j\}} $$
Samples
Input #1
4
0 1 0 10
Output #1
1
Input #2
2
0 0
Output #2
2
Input #3
10
1 1 3 0 0 0 2 1 0 3
Output #3
3
API Response (JSON)
{
  "problem": {
    "name": "B. Wrath",
    "description": {
      "content": "_Hands that shed innocent blood!_ There are _n_ guilty people in a line, the _i_\\-th of them holds a claw with length _L__i_. The bell rings and every person kills some of people in front of him. All",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF892B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "_Hands that shed innocent blood!_\n\nThere are _n_ guilty people in a line, the _i_\\-th of them holds a claw with length _L__i_. The bell rings and every person kills some of people in front of him. All...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "_Hands that shed innocent blood!_\n\n有 #cf_span[n] 个有罪的人排成一列,第 #cf_span[i] 个人手持一把长度为 #cf_span[Li] 的爪子。钟声响起,每个人都会杀死他前面的一些人。所有人同时行动。具体来说,第 #cf_span[i] 个人会杀死第 #cf_span[j] 个人,当且仅当 #cf_span[j < i] 且 #cf_span...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ n $ be the number of people, indexed from $ 1 $ to $ n $.  \nLet $ L_i $ denote the claw length of the $ i $-th person.\n\nFor each person $ i $, they kill all persons $ j $ such that:\n$$\nj < i \\qu...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments