B. Luba And The Ticket

Codeforces
IDCF845B
Time2000ms
Memory256MB
Difficulty
brute forcegreedyimplementation
English · Original
Chinese · Translation
Formal · Original
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. ## Input You are given a string consisting of 6 characters (all characters are digits from 0 to 9) — this string denotes Luba's ticket. The ticket can start with the digit 0. ## Output Print one number — the minimum possible number of digits Luba needs to replace to make the ticket lucky. [samples] ## Note In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
Luba 有一张由 #cf_span[6] 位数字组成的票。在一次操作中,她可以选择任意位置的数字并将其替换为任意数字。她想知道最少需要替换多少个数字,才能使这张票变成幸运票。 当且仅当前三个数字的和等于后三个数字的和时,这张票被认为是幸运的。 给你一个由 #cf_span[6] 个字符组成的字符串(所有字符都是从 #cf_span[0] 到 #cf_span[9] 的数字)——该字符串表示 Luba 的票。票的首位可以是数字 #cf_span[0]。 请输出一个数字——Luba 为使票变成幸运票所需替换的最少数字个数。 在第一个例子中,票已经是幸运的,因此答案是 #cf_span[0]。 在第二个例子中,Luba 可以将 #cf_span[4] 和 #cf_span[5] 替换为零,票就会变成幸运的。显然至少需要两次替换。 在第三个例子中,Luba 可以将任意一个零替换为 #cf_span[3]。显然至少需要一次替换。 ## Input 给你一个由 #cf_span[6] 个字符组成的字符串(所有字符都是从 #cf_span[0] 到 #cf_span[9] 的数字)——该字符串表示 Luba 的票。票的首位可以是数字 #cf_span[0]。 ## Output 请输出一个数字——Luba 为使票变成幸运票所需替换的最少数字个数。 [samples] ## Note 在第一个例子中,票已经是幸运的,因此答案是 #cf_span[0]。在第二个例子中,Luba 可以将 #cf_span[4] 和 #cf_span[5] 替换为零,票就会变成幸运的。显然至少需要两次替换。在第三个例子中,Luba 可以将任意一个零替换为 #cf_span[3]。显然至少需要一次替换。
**Definitions** Let $ T = d_1 d_2 d_3 d_4 d_5 d_6 $ be a string of 6 digits, where $ d_i \in \{0, 1, \dots, 9\} $ for $ i = 1, \dots, 6 $. Define: - $ S_L = d_1 + d_2 + d_3 $: sum of the first three digits. - $ S_R = d_4 + d_5 + d_6 $: sum of the last three digits. **Constraints** Each digit $ d_i \in \{0, 1, \dots, 9\} $. **Objective** Find the minimum number of digit replacements required such that $ S_L = S_R $. Each replacement allows changing any digit $ d_i $ to any value in $ \{0, 1, \dots, 9\} $.
Samples
Input #1
000000
Output #1
0
Input #2
123456
Output #2
2
Input #3
111000
Output #3
1
API Response (JSON)
{
  "problem": {
    "name": "B. Luba And The Ticket",
    "description": {
      "content": "Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in o",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF845B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in o...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Luba 有一张由 #cf_span[6] 位数字组成的票。在一次操作中,她可以选择任意位置的数字并将其替换为任意数字。她想知道最少需要替换多少个数字,才能使这张票变成幸运票。\n\n当且仅当前三个数字的和等于后三个数字的和时,这张票被认为是幸运的。\n\n给你一个由 #cf_span[6] 个字符组成的字符串(所有字符都是从 #cf_span[0] 到 #cf_span[9] 的数字)——该字符串表示 ...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ T = d_1 d_2 d_3 d_4 d_5 d_6 $ be a string of 6 digits, where $ d_i \\in \\{0, 1, \\dots, 9\\} $ for $ i = 1, \\dots, 6 $.  \nDefine:  \n- $ S_L = d_1 + d_2 + d_3 $: sum of the first t...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments