A. Curriculum Vitae

Codeforces
IDCF846A
Time1000ms
Memory256MB
Difficulty
brute forceimplementation
English · Original
Chinese · Translation
Formal · Original
Hideo Kojima has just quit his job at Konami. Now he is going to find a new place to work. Despite being such a well-known person, he still needs a CV to apply for a job. During all his career Hideo has produced _n_ games. Some of them were successful, some were not. Hideo wants to remove several of them (possibly zero) from his CV to make a better impression on employers. As a result there should be no unsuccessful game which comes right after successful one in his CV. More formally, you are given an array _s_1, _s_2, ..., _s__n_ of zeros and ones. Zero corresponds to an unsuccessful game, one — to a successful one. Games are given in order they were produced, and Hideo can't swap these values. He should remove some elements from this array in such a way that no zero comes right after one. Besides that, Hideo still wants to mention as much games in his CV as possible. Help this genius of a man determine the maximum number of games he can leave in his CV. ## Input The first line contains one integer number _n_ (1 ≤ _n_ ≤ 100). The second line contains _n_ space-separated integer numbers _s_1, _s_2, ..., _s__n_ (0 ≤ _s__i_ ≤ 1). 0 corresponds to an unsuccessful game, 1 — to a successful one. ## Output Print one integer — the maximum number of games Hideo can leave in his CV so that no unsuccessful game comes after a successful one. [samples]
Hideo Kojima 刚刚从 Konami 辞职。现在他需要找一份新工作。尽管他是一位广为人知的人物,但他仍然需要一份简历来申请职位。 在他整个职业生涯中,Hideo 制作了 #cf_span[n] 款游戏。其中一些是成功的,一些是失败的。Hideo 希望从他的简历中删除若干款游戏(可能为零款),以给雇主留下更好的印象。结果,他的简历中不应出现任何失败的游戏紧接在成功游戏之后的情况。 更正式地,你被给定一个由 0 和 1 组成的数组 #cf_span[s1, s2, ..., sn]。0 对应一个失败的游戏,1 对应一个成功的游戏。游戏按其被制作的顺序给出,Hideo 不能交换这些值。他应从该数组中删除某些元素,使得没有任何 0 紧接在 1 之后。 此外,Hideo 仍希望在他的简历中尽可能多地保留游戏。请帮助这位天才确定他最多可以保留多少款游戏。 第一行包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 100])。 第二行包含 #cf_span[n] 个用空格分隔的整数 #cf_span[s1, s2, ..., sn](#cf_span[0 ≤ si ≤ 1])。#cf_span[0] 对应一个失败的游戏,#cf_span[1] 对应一个成功的游戏。 请输出一个整数——Hideo 可以保留在简历中的最大游戏数量,使得没有任何失败的游戏出现在成功游戏之后。 ## Input 第一行包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 100])。第二行包含 #cf_span[n] 个用空格分隔的整数 #cf_span[s1, s2, ..., sn](#cf_span[0 ≤ si ≤ 1])。#cf_span[0] 对应一个失败的游戏,#cf_span[1] 对应一个成功的游戏。 ## Output 请输出一个整数——Hideo 可以保留在简历中的最大游戏数量,使得没有任何失败的游戏出现在成功游戏之后。 [samples]
Let $ s = [s_1, s_2, \dots, s_n] $ be a binary sequence where $ s_i \in \{0, 1\} $. **Constraint:** The resulting subsequence must not contain any occurrence of $ 1 $ immediately followed by $ 0 $, i.e., no substring "10". **Objective:** Maximize the length of a subsequence (not necessarily contiguous, but preserving order) of $ s $ satisfying the above constraint. --- **Formal Problem Statement:** Given a binary sequence $ s \in \{0,1\}^n $, find the maximum length of a subsequence $ s' $ of $ s $ such that there do not exist indices $ i < j $ in $ s' $ with $ s'_i = 1 $ and $ s'_j = 0 $. --- **Equivalent Reformulation:** The constraint "no 1 followed by 0" implies that in the resulting subsequence, all 0s must come before all 1s. That is, the subsequence must be of the form: $$ \underbrace{0, 0, \dots, 0}_{a \text{ times}}, \underbrace{1, 1, \dots, 1}_{b \text{ times}} $$ for some $ a, b \geq 0 $. Thus, the problem reduces to: Find the maximum value of $ a + b $, where $ a $ is the number of 0s selected from some prefix of $ s $, and $ b $ is the number of 1s selected from the corresponding suffix of $ s $, such that all selected 0s appear before all selected 1s in the original sequence. --- **Optimization Strategy:** For each possible split point $ k \in \{0, 1, \dots, n\} $, consider: - Taking all 0s from positions $ 1 $ to $ k $, - Taking all 1s from positions $ k+1 $ to $ n $. Then the total length is: $$ \text{count}_0(1..k) + \text{count}_1(k+1..n) $$ The maximum over all $ k \in \{0, 1, \dots, n\} $ gives the answer. --- **Final Mathematical Formulation:** Let $ n \in \mathbb{N} $, $ s \in \{0,1\}^n $. Define for $ k \in \{0, 1, \dots, n\} $: $$ f(k) = \sum_{i=1}^{k} [s_i = 0] + \sum_{i=k+1}^{n} [s_i = 1] $$ Then the answer is: $$ \max_{k \in \{0, 1, \dots, n\}} f(k) $$
Samples
Input #1
4
1 1 0 1
Output #1
3
Input #2
6
0 1 0 0 1 0
Output #2
4
Input #3
1
0
Output #3
1
API Response (JSON)
{
  "problem": {
    "name": "A. Curriculum Vitae",
    "description": {
      "content": "Hideo Kojima has just quit his job at Konami. Now he is going to find a new place to work. Despite being such a well-known person, he still needs a CV to apply for a job. During all his career Hideo ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF846A"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Hideo Kojima has just quit his job at Konami. Now he is going to find a new place to work. Despite being such a well-known person, he still needs a CV to apply for a job.\n\nDuring all his career Hideo ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Hideo Kojima 刚刚从 Konami 辞职。现在他需要找一份新工作。尽管他是一位广为人知的人物,但他仍然需要一份简历来申请职位。\n\n在他整个职业生涯中,Hideo 制作了 #cf_span[n] 款游戏。其中一些是成功的,一些是失败的。Hideo 希望从他的简历中删除若干款游戏(可能为零款),以给雇主留下更好的印象。结果,他的简历中不应出现任何失败的游戏紧接在成功游戏之后的情况。\n\n更正...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ s = [s_1, s_2, \\dots, s_n] $ be a binary sequence where $ s_i \\in \\{0, 1\\} $.\n\n**Constraint:**  \nThe resulting subsequence must not contain any occurrence of $ 1 $ immediately followed by $ 0 $,...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments