B. Maxim Buys an Apartment

Codeforces
IDCF854B
Time1000ms
Memory512MB
Difficulty
constructive algorithmsmath
English · Original
Chinese · Translation
Formal · Original
Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has _n_ apartments that are numbered from 1 to _n_ and are arranged in a row. Two apartments are adjacent if their indices differ by 1. Some of the apartments can already be inhabited, others are available for sale. Maxim often visits his neighbors, so apartment is _good_ for him if it is available for sale and there is at least one already inhabited apartment adjacent to it. Maxim knows that there are exactly _k_ already inhabited apartments, but he doesn't know their indices yet. Find out what could be the minimum possible and the maximum possible number of apartments that are good for Maxim. ## Input The only line of the input contains two integers: _n_ and _k_ (1 ≤ _n_ ≤ 109, 0 ≤ _k_ ≤ _n_). ## Output Print the minimum possible and the maximum possible number of apartments good for Maxim. [samples] ## Note In the sample test, the number of good apartments could be minimum possible if, for example, apartments with indices 1, 2 and 3 were inhabited. In this case only apartment 4 is good. The maximum possible number could be, for example, if apartments with indices 1, 3 and 5 were inhabited. In this case all other apartments: 2, 4 and 6 are good.
Maxim 想要在 Metropolis 的 Line Avenue 的一栋新楼中购买一套公寓。这栋楼有 #cf_span[n] 套公寓,编号从 #cf_span[1] 到 #cf_span[n],呈一行排列。如果两套公寓的编号相差 #cf_span[1],则称它们相邻。有些公寓已经有人居住,其余的可供出售。 Maxim 经常拜访他的邻居,因此一套公寓对他来说是 _good_ 的,当且仅当它可供出售,且至少有一套相邻的公寓已被占用。Maxim 知道恰好有 #cf_span[k] 套公寓已被占用,但他还不知道它们的编号。 请找出对 Maxim 来说可能的最少和最多的好公寓数量。 输入仅有一行,包含两个整数:#cf_span[n] 和 #cf_span[k](#cf_span[1 ≤ n ≤ 109],#cf_span[0 ≤ k ≤ n])。 请输出对 Maxim 来说可能的最少和最多的好公寓数量。 在样例测试中,好公寓的数量可能最少的情况是,例如,编号为 #cf_span[1]、#cf_span[2] 和 #cf_span[3] 的公寓已被占用。此时只有公寓 #cf_span[4] 是好的。好公寓数量可能最多的情况是,例如,编号为 #cf_span[1]、#cf_span[3] 和 #cf_span[5] 的公寓已被占用。此时所有其他公寓:#cf_span[2]、#cf_span[4] 和 #cf_span[6] 都是好的。 ## Input 输入仅有一行,包含两个整数:#cf_span[n] 和 #cf_span[k](#cf_span[1 ≤ n ≤ 109],#cf_span[0 ≤ k ≤ n])。 ## Output 请输出对 Maxim 来说可能的最少和最多的好公寓数量。 [samples] ## Note 在样例测试中,好公寓的数量可能最少的情况是,例如,编号为 #cf_span[1]、#cf_span[2] 和 #cf_span[3] 的公寓已被占用。此时只有公寓 #cf_span[4] 是好的。好公寓数量可能最多的情况是,例如,编号为 #cf_span[1]、#cf_span[3] 和 #cf_span[5] 的公寓已被占用。此时所有其他公寓:#cf_span[2]、#cf_span[4] 和 #cf_span[6] 都是好的。
Let $ n $ be the total number of apartments, numbered $ 1 $ to $ n $, arranged in a row. Let $ k $ be the number of already inhabited apartments ($ 0 \leq k \leq n $). An apartment is *good* if: - It is **available** for sale (i.e., not inhabited), - And it has **at least one adjacent** apartment that is inhabited. --- ### Definitions: Let $ H \subseteq \{1, 2, \dots, n\} $ be the set of inhabited apartments, with $ |H| = k $. Let $ G(H) $ be the set of good apartments given $ H $. We seek: - $ \min_{|H| = k} |G(H)| $ - $ \max_{|H| = k} |G(H)| $ --- ### Constraints: - Apartments at positions $ 1 $ and $ n $ have only one neighbor each. - Apartments $ 2 $ to $ n-1 $ have two neighbors. - A good apartment must be **uninhabited** and adjacent to **at least one** inhabited apartment. --- ## Minimum Number of Good Apartments To **minimize** the number of good apartments, cluster the $ k $ inhabited apartments together in a single contiguous block. - If $ k = 0 $: No inhabited apartments → 0 good apartments. - If $ k = n $: No available apartments → 0 good apartments. - Otherwise, place all $ k $ inhabited apartments consecutively, say from position $ a $ to $ a+k-1 $. Then, the only available apartments adjacent to inhabited ones are: - The one immediately before the block (if $ a > 1 $), - The one immediately after the block (if $ a+k-1 < n $). Thus, maximum 2 good apartments. But if the block touches an end (e.g., starts at 1 or ends at $ n $), then only one side is adjacent. So: - If the block is placed at one end (e.g., apartments $ 1 $ to $ k $), then only apartment $ k+1 $ is good (if $ k < n $). - Similarly, if placed at the other end, only $ n-k $ is good. Hence, **minimum** number of good apartments is: $$ \min_{|H|=k} |G(H)| = \begin{cases} 0 & \text{if } k = 0 \text{ or } k = n \\ 1 & \text{if } 1 \leq k \leq n-1 \text{ and the block is placed at an end} \\ \end{cases} $$ But even if we place the block in the middle, we get 2 good apartments. To **minimize**, we place it at the end → only 1 good apartment. **However**, if $ k = 1 $ and $ n = 1 $, then $ k = n $ → 0. So general formula: - If $ k = 0 $ or $ k = n $: **0** - Else: **1** (achieved by placing the block at one end) > **But wait**: What if $ k = 1 $ and $ n = 2 $? Then the single inhabited apartment has one neighbor — which is available → 1 good apartment. Still 1. > What if $ k = 2 $, $ n = 3 $? Inhabited: [1,2] → good: [3]. Or [2,3] → good: [1]. Still 1. > What if $ k = 2 $, $ n = 4 $? Inhabited: [1,2] → good: [3]. Only one. So yes — **minimum is always 0 if k=0 or k=n, else 1**. Wait — what if $ k = n-1 $? Then only one apartment is available. It must be adjacent to at least one inhabited one (since only one is missing, and the rest are filled). So the single available apartment is adjacent to at least one inhabited → 1 good apartment. So **minimum is always 1 when $ 1 \leq k \leq n-1 $**. ✅ **Minimum = $ \max(0, \min(2, k)) $**? No. Actually: **Minimum = 0 if k=0 or k=n, else 1** --- ## Maximum Number of Good Apartments To **maximize** the number of good apartments, we want to **maximize the number of available apartments that are adjacent to at least one inhabited apartment**. Each inhabited apartment can "cover" up to 2 adjacent available apartments (left and right), **but**: - If two inhabited apartments are adjacent, the space between them is not available → no gain. - If they are separated by at least one available apartment, then each can cover their own neighbors. To maximize coverage, **space out the inhabited apartments** so that no two are adjacent. That is, place inhabited apartments with **at least one gap** between them. In this case, each inhabited apartment can contribute **two** good apartments — unless it is at the boundary. But if we place them with exactly one gap between them (i.e., pattern: I - I - I - ...), then: - Each inhabited apartment (except possibly at ends) has two neighbors, both available → contributes 2 good apartments. - But the available apartments between them are shared? No — if we have: Positions: I - I - I Then apartments: 1 (I), 2 (available), 3 (I), 4 (available), 5 (I) Good apartments: 2, 4 → only 2 good apartments for 3 inhabited. Wait — apartment 2 is adjacent to 1 and 3 → both inhabited → still only counted once. Apartment 4 is adjacent to 3 and 5 → both inhabited → still one apartment. So each **gap** between two inhabited apartments (i.e., a single available apartment between two inhabited) becomes **one good apartment**. Also, if an inhabited apartment is at the end, it can make its single neighbor good. So if we place $ k $ inhabited apartments with **at least one gap** between each pair, then: - Number of gaps between inhabited apartments: $ k - 1 $ - Each such gap is a single available apartment between two inhabited → contributes 1 good apartment. - Additionally, if the first inhabited is not at position 1, then position 1 is available and adjacent to the first inhabited → good. - Similarly, if the last inhabited is not at position $ n $, then position $ n $ is good. So total good apartments = - $ k - 1 $ (gaps between inhabited) - + 1 if the first inhabited is not at 1 - + 1 if the last inhabited is not at n To **maximize**, we want to **maximize the number of available apartments adjacent to inhabited ones**, so we should **minimize overlap** of coverage. Best strategy: **place inhabited apartments as isolated as possible**, i.e., with **exactly one available apartment between them**, and **not touching the ends** if possible. But we can also choose to **place them at the ends** to get extra coverage. Actually, the optimal configuration is to **space them with single gaps**, and **extend as far as possible**. Let’s think differently. Each inhabited apartment can "cover" up to 2 available neighbors. But if two inhabited apartments are adjacent, they share a boundary and cover fewer total available apartments. To maximize total good apartments, we want **no two inhabited apartments adjacent**, and we want to **avoid placing them at the ends**? No — actually, placing at ends gives **one** good neighbor each, same as internal ones give two. Wait — if an inhabited apartment is at position 1, it can only cover position 2. If it’s at position 2, it can cover 1 and 3. So **internal** inhabited apartments can cover **two** good apartments; **end** ones cover **one**. So to maximize, we should **avoid placing inhabited apartments at the ends**, so that each can cover two neighbors. But we have only $ k $ inhabited apartments. Suppose we place all $ k $ inhabited apartments in the interior, each separated by at least one available apartment. Then, each inhabited apartment contributes 2 good apartments → total $ 2k $. But can we do that? We need to place $ k $ inhabited apartments with **at least one gap** between them. Minimum space required: each inhabited apartment takes 1 spot, and between each pair, we need 1 gap → total length = $ k + (k - 1) = 2k - 1 $ So if $ 2k - 1 \leq n $, we can place them all internally with gaps → each contributes 2 good apartments → total good apartments = $ 2k $ But if $ 2k - 1 > n $, then we cannot space them all with gaps → some must be adjacent. But also, if we place them at the ends, we get less coverage. Wait — here's a better way. In the optimal configuration for **maximum** good apartments: - Place inhabited apartments with **exactly one available apartment between each pair**. - Place the first inhabited apartment at position 2, then 4, 6, ..., so that they are not at the ends. Then: - Each inhabited apartment is surrounded by two available apartments → contributes 2 good apartments. - The available apartments at the ends (1 and n) are not adjacent to any inhabited → not good. But if we shift the entire block to the left, putting the first inhabited at position 1, then we lose one good apartment (no left neighbor), but we might be able to fit more? Actually, the maximum number of good apartments is bounded by the number of available apartments that are adjacent to at least one inhabited. Each inhabited apartment can be adjacent to at most 2 available apartments. But if two inhabited apartments are adjacent, they share a boundary — the apartment between them is inhabited, so no good apartment there. So the **maximum** number of good apartments is achieved when **no two inhabited apartments are adjacent**, and **we use as many available apartments as possible as neighbors**. In that case, each inhabited apartment contributes **2** good apartments, **except** if it is at the end. So to maximize, we want **no inhabited apartment at the ends** → then all $ k $ inhabited apartments are internal and each contributes 2 → total $ 2k $ But we can only do that if the total length required is $ \leq n $ To place $ k $ non-adjacent inhabited apartments, we need at least $ k $ positions for them and $ k-1 $ gaps between them → total $ 2k - 1 $ So if $ 2k - 1 \leq n $, then we can place them all internally (e.g., positions 2, 4, ..., 2k) → then the good apartments are positions 1, 3, 5, ..., 2k+1 — but wait, position 1 is adjacent to 2 → good, position 3 is adjacent to 2 and 4 → good, etc. Actually, if we place inhabited at positions $ 2, 4, 6, \dots, 2k $, then: - Good apartments: 1 (adj to 2), 3 (adj to 2 and 4), 5 (adj to 4 and 6), ..., $ 2k - 1 $, and $ 2k + 1 $ (if $ 2k + 1 \leq n $) But we only have $ k $ inhabited apartments → the good ones are: - Left of first: 1 - Between each pair: 3,5,..., up to $ 2k-1 $ → $ k-1 $ of them - Right of last: $ 2k+1 $ So total good apartments = $ 1 + (k - 1) + 1 = k + 1 $ Wait — this contradicts. Alternative approach: Let’s model the maximum number of good apartments as the number of available apartments that are adjacent to at least one inhabited. Let $ A $ be the set of available apartments. Each inhabited apartment can "cover" its left and right neighbors, if they are available. The total number of "adjacent pairs" between inhabited and available is at most $ 2k $, but each good apartment is counted once, even if adjacent to two inhabited. So the maximum number of good apartments is **min(n - k, 2k)** Why? - There are $ n - k $ available apartments → can't have more than that many good apartments. - Each inhabited apartment can contribute to at most 2 good apartments → total coverage capacity is $ 2k $ - So total good apartments ≤ $ \min(n - k, 2k) $ And this bound is **achievable**: - If $ 2k \leq n - k $ → $ 3k \leq n $, then we can place $ k $ inhabited apartments, each separated by at least two available apartments → each inhabited apartment has two available neighbors, and no overlap → total good = $ 2k $ - If $ 2k > n - k $ → $ 3k > n $, then we have more coverage capacity than available apartments → we can make **all** available apartments good → total = $ n - k $ So: ✅ **Maximum = $ \min(n - k, 2k) $** But wait — what if $ k = 0 $? Then $ \min(n, 0) = 0 $ → correct. What if $ k = n $? Then $ \min(0, 2n) = 0 $ → correct. What if $ n = 6, k = 3 $: $ \min(3, 6) = 3 $ Can we get 3 good apartments? Yes — e.g., inhabit 1,3,5 → good: 2,4,6 → 3 good → matches. If $ n = 5, k = 3 $: $ \min(2, 6) = 2 $ Can we get 2 good? Inhabit 1,3,5 → available: 2,4 → both adjacent to inhabited → 2 good → yes. If $ n = 4, k = 2 $: $ \min(2, 4) = 2 $ Inhabit 1 and 4 → good: 2 and 3 → 2 good → yes. Inhabit 2 and 3 → adjacent → good: 1 and 4 → still 2. So yes. ✅ So maximum = $ \min(n - k, 2k) $ --- ## Final Answer: $$ \boxed{ \begin{array}{c} \text{Minimum} = \\ \begin{cases} 0 & \text{if } k = 0 \text{ or } k = n \\ 1 & \text{otherwise} \end{cases} \\ \\ \text{Maximum} = \min(n - k, 2k) \end{array} } $$
Samples
Input #1
6 3
Output #1
1 3
API Response (JSON)
{
  "problem": {
    "name": "B. Maxim Buys an Apartment",
    "description": {
      "content": "Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has _n_ apartments that are numbered from 1 to _n_ and are arranged in a row. Two apartments are adjacent if thei",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 524288
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF854B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has _n_ apartments that are numbered from 1 to _n_ and are arranged in a row. Two apartments are adjacent if thei...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Maxim 想要在 Metropolis 的 Line Avenue 的一栋新楼中购买一套公寓。这栋楼有 #cf_span[n] 套公寓,编号从 #cf_span[1] 到 #cf_span[n],呈一行排列。如果两套公寓的编号相差 #cf_span[1],则称它们相邻。有些公寓已经有人居住,其余的可供出售。\n\nMaxim 经常拜访他的邻居,因此一套公寓对他来说是 _good_ 的,当且仅当它可供...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ n $ be the total number of apartments, numbered $ 1 $ to $ n $, arranged in a row. Let $ k $ be the number of already inhabited apartments ($ 0 \\leq k \\leq n $).\n\nAn apartment is *good* if:\n- It...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments