C. Maximum Number

Codeforces
IDCF795C
Time1000ms
Memory256MB
Difficulty
constructive algorithmsgreedy
English · Original
Chinese · Translation
Formal · Original
Stepan has the newest electronic device with a display. Different digits can be shown on it. Each digit is shown on a seven-section indicator like it is shown on the picture below. <center>![image](https://espresso.codeforces.com/1b26f8faef5a8a5117060246d28eb2aea6e5ebe8.png)</center>So, for example, to show the digit 3 on the display, 5 sections must be highlighted; and for the digit 6, 6 sections must be highlighted. The battery of the newest device allows to highlight at most _n_ sections on the display. Stepan wants to know the maximum possible integer number which can be shown on the display of his newest device. Your task is to determine this number. Note that this number must not contain leading zeros. Assume that the size of the display is enough to show any integer. ## Input The first line contains the integer _n_ (2 ≤ _n_ ≤ 100 000) — the maximum number of sections which can be highlighted on the display. ## Output Print the maximum integer which can be shown on the display of Stepan's newest device. [samples]
Stepan 拥有一款最新电子设备,其显示屏可以显示不同的数字。每个数字通过一个七段指示器显示,如下图所示。 例如,要在显示屏上显示数字 #cf_span[3],需要点亮 #cf_span[5] 段;而显示数字 #cf_span[6] 则需要点亮 #cf_span[6] 段。 该设备的电池最多允许点亮 #cf_span[n] 段。 Stepan 想知道,在显示屏上能显示的最大整数是多少。你的任务是确定这个数字。注意,这个数字不能包含前导零。假设显示屏的尺寸足以显示任意大的整数。 第一行包含整数 #cf_span[n] (#cf_span[2 ≤ n ≤ 100 000]) —— 显示屏上可点亮的最大段数。 请输出 Stepan 最新设备显示屏上能显示的最大整数。 ## Input 第一行包含整数 #cf_span[n] (#cf_span[2 ≤ n ≤ 100 000]) —— 显示屏上可点亮的最大段数。 ## Output 请输出 Stepan 最新设备显示屏上能显示的最大整数。 [samples]
Let $ c_d $ denote the number of segments required to display digit $ d \in \{0, 1, 2, \dots, 9\} $, with: $$ \begin{align*} c_0 &= 6, & c_1 &= 2, & c_2 &= 5, \\ c_3 &= 5, & c_4 &= 4, & c_5 &= 5, \\ c_6 &= 6, & c_7 &= 3, & c_8 &= 7, & c_9 &= 6. \end{align*} $$ Given an integer $ n $ (with $ 2 \leq n \leq 100{,}000 $), find the maximum integer (in decimal representation, without leading zeros) such that the total number of segments used to display all its digits is at most $ n $. **Objective:** Maximize the integer value (lexicographically) under the constraint: $$ \sum_{i=1}^{k} c_{d_i} \leq n, $$ where $ d_1 d_2 \dots d_k $ is the decimal representation of the number, $ d_1 \ne 0 $, and $ k \geq 1 $. --- **Key Insight:** To maximize the integer: 1. **Maximize the number of digits**: Use as many digits as possible, since a number with more digits is larger than one with fewer (no leading zeros). 2. **Greedy left-to-right digit selection**: For each position, choose the **largest possible digit** $ d $ such that the remaining segments can still form a valid number with the remaining digits. Let $ m = \min_{d \in \{0,\dots,9\}} c_d = c_1 = 2 $. Then the **maximum number of digits** is $ \left\lfloor \frac{n}{2} \right\rfloor $. We construct the number digit by digit from left to right: - For position $ i $ (starting from left), try digits from 9 down to 1 (for first digit) or 9 down to 0 (for subsequent digits). - For candidate digit $ d $, check if $ c_d \leq \text{remaining\_segments} $ and $ \text{remaining\_segments} - c_d \geq 2 \cdot (\text{remaining\_positions} - 1) $. - Choose the largest such $ d $, subtract $ c_d $ from remaining segments, and proceed. This greedy construction yields the lexicographically largest number under the segment constraint.
Samples
Input #1
2
Output #1
1
Input #2
3
Output #2
7
API Response (JSON)
{
  "problem": {
    "name": "C. Maximum Number",
    "description": {
      "content": "Stepan has the newest electronic device with a display. Different digits can be shown on it. Each digit is shown on a seven-section indicator like it is shown on the picture below. <center>![image](h",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF795C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Stepan has the newest electronic device with a display. Different digits can be shown on it. Each digit is shown on a seven-section indicator like it is shown on the picture below.\n\n<center>![image](h...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Stepan 拥有一款最新电子设备,其显示屏可以显示不同的数字。每个数字通过一个七段指示器显示,如下图所示。\n\n例如,要在显示屏上显示数字 #cf_span[3],需要点亮 #cf_span[5] 段;而显示数字 #cf_span[6] 则需要点亮 #cf_span[6] 段。\n\n该设备的电池最多允许点亮 #cf_span[n] 段。\n\nStepan 想知道,在显示屏上能显示的最大整数是多少。你的...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ c_d $ denote the number of segments required to display digit $ d \\in \\{0, 1, 2, \\dots, 9\\} $, with:\n\n$$\n\\begin{align*}\nc_0 &= 6, & c_1 &= 2, & c_2 &= 5, \\\\\nc_3 &= 5, & c_4 &= 4, & c_5 &= 5, \\\\\n...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments