A. Vladik and flights

Codeforces
IDCF743A
Time2000ms
Memory256MB
Difficulty
constructive algorithmsgreedyimplementation
English · Original
Chinese · Translation
Formal · Original
Vladik is a competitive programmer. This year he is going to win the International Olympiad in Informatics. But it is not as easy as it sounds: the question Vladik face now is to find the cheapest way to get to the olympiad. Vladik knows _n_ airports. All the airports are located on a straight line. Each airport has unique id from 1 to _n_, Vladik's house is situated next to the airport with id _a_, and the place of the olympiad is situated next to the airport with id _b_. It is possible that Vladik's house and the place of the olympiad are located near the same airport. To get to the olympiad, Vladik can fly between any pair of airports any number of times, but he has to start his route at the airport _a_ and finish it at the airport _b_. Each airport belongs to one of two companies. The cost of flight from the airport _i_ to the airport _j_ is zero if both airports belong to the same company, and |_i_ - _j_| if they belong to different companies. Print the minimum cost Vladik has to pay to get to the olympiad. ## Input The first line contains three integers _n_, _a_, and _b_ (1 ≤ _n_ ≤ 105, 1 ≤ _a_, _b_ ≤ _n_) — the number of airports, the id of the airport from which Vladik starts his route and the id of the airport which he has to reach. The second line contains a string with length _n_, which consists only of characters 0 and 1. If the _i_\-th character in this string is 0, then _i_\-th airport belongs to first company, otherwise it belongs to the second. ## Output Print single integer — the minimum cost Vladik has to pay to get to the olympiad. [samples] ## Note In the first example Vladik can fly to the airport 2 at first and pay |1 - 2| = 1 (because the airports belong to different companies), and then fly from the airport 2 to the airport 4 for free (because the airports belong to the same company). So the cost of the whole flight is equal to 1. It's impossible to get to the olympiad for free, so the answer is equal to 1. In the second example Vladik can fly directly from the airport 5 to the airport 2, because they belong to the same company.
Vladik 是一名竞赛程序员。今年他打算赢得国际信息学奥林匹克竞赛。但事情并没有听起来那么简单:Vladik 目前面临的问题是找到前往竞赛地点的最便宜方式。 Vladik 知道 #cf_span[n] 个机场。所有机场都位于一条直线上。每个机场都有一个从 #cf_span[1] 到 #cf_span[n] 的唯一编号,Vladik 的家位于编号为 #cf_span[a] 的机场旁边,竞赛地点位于编号为 #cf_span[b] 的机场旁边。Vladik 的家和竞赛地点可能位于同一个机场附近。 为了到达竞赛地点,Vladik 可以在任意两个机场之间飞行任意次数,但他必须从机场 #cf_span[a] 出发,并在机场 #cf_span[b] 结束行程。 每个机场属于两家公司之一。从机场 #cf_span[i] 飞往机场 #cf_span[j] 的费用为零,如果两个机场属于同一家公司;否则费用为 #cf_span[|i - j|]。 请输出 Vladik 到达竞赛地点所需的最小费用。 第一行包含三个整数 #cf_span[n]、#cf_span[a] 和 #cf_span[b](#cf_span[1 ≤ n ≤ 105],#cf_span[1 ≤ a, b ≤ n])——机场数量、Vladik 起始机场的编号和他必须到达的机场编号。 第二行包含一个长度为 #cf_span[n] 的字符串,仅由字符 #cf_span[0] 和 #cf_span[1] 组成。如果该字符串的第 #cf_span[i] 个字符是 #cf_span[0],则第 #cf_span[i] 个机场属于第一家公司的;否则属于第二家公司。 请输出一个整数——Vladik 到达竞赛地点所需的最小费用。 在第一个例子中,Vladik 可以先飞往机场 #cf_span[2],支付 #cf_span[|1 - 2| = 1] 的费用(因为两个机场属于不同公司),然后从机场 #cf_span[2] 免费飞往机场 #cf_span[4](因为两个机场属于同一家公司)。因此整个行程的费用为 #cf_span[1]。不可能免费到达竞赛地点,因此答案为 #cf_span[1]。 在第二个例子中,Vladik 可以直接从机场 #cf_span[5] 飞往机场 #cf_span[2],因为它们属于同一家公司。 ## Input 第一行包含三个整数 #cf_span[n]、#cf_span[a] 和 #cf_span[b](#cf_span[1 ≤ n ≤ 105],#cf_span[1 ≤ a, b ≤ n])——机场数量、Vladik 起始机场的编号和他必须到达的机场编号。第二行包含一个长度为 #cf_span[n] 的字符串,仅由字符 #cf_span[0] 和 #cf_span[1] 组成。如果该字符串的第 #cf_span[i] 个字符是 #cf_span[0],则第 #cf_span[i] 个机场属于第一家公司的;否则属于第二家公司。 ## Output 请输出一个整数——Vladik 到达竞赛地点所需的最小费用。 [samples] ## Note 在第一个例子中,Vladik 可以先飞往机场 #cf_span[2],支付 #cf_span[|1 - 2| = 1] 的费用(因为两个机场属于不同公司),然后从机场 #cf_span[2] 免费飞往机场 #cf_span[4](因为两个机场属于同一家公司)。因此整个行程的费用为 #cf_span[1]。不可能免费到达竞赛地点,因此答案为 #cf_span[1]。在第二个例子中,Vladik 可以直接从机场 #cf_span[5] 飞往机场 #cf_span[2],因为它们属于同一家公司。
Let $ n, a, b \in \mathbb{N} $ with $ 1 \leq a, b \leq n $. Let $ s \in \{0,1\}^n $ be a binary string of length $ n $, where $ s[i] $ denotes the company of airport $ i+1 $ (1-indexed). Define the cost function $ c(i, j) $ for $ 1 \leq i, j \leq n $: $$ c(i, j) = \begin{cases} 0 & \text{if } s[i-1] = s[j-1] \\ |i - j| & \text{otherwise} \end{cases} $$ Vladik must find a path $ p = (p_0, p_1, \dots, p_k) $ such that: - $ p_0 = a $, - $ p_k = b $, - $ k \geq 0 $, - and the total cost $ \sum_{t=0}^{k-1} c(p_t, p_{t+1}) $ is minimized. Find $ \min \sum_{t=0}^{k-1} c(p_t, p_{t+1}) $ over all such paths. **Note**: Since direct flight from $ a $ to $ b $ is always allowed, and intermediate stops can only reduce or maintain cost (due to zero-cost same-company flights), the minimum cost is: $$ \min\left( |a - b| \cdot \mathbf{1}_{s[a-1] \ne s[b-1]}, \quad \min_{\substack{1 \leq i,j \leq n \\ s[i-1] = s[a-1],\, s[j-1] = s[b-1]}} \left( |a - i| + |j - b| \right) \right) $$ But since any intermediate same-company airports can be used for free, the optimal strategy reduces to: - If $ s[a-1] = s[b-1] $, cost = 0. - Else, cost = $ \min(|a - b|, \min_{i: s[i-1] = s[a-1]} |a - i| + \min_{j: s[j-1] = s[b-1]} |j - b|) $ Actually, since we can go $ a \to i \to j \to b $ with $ s[i-1] = s[a-1] $, $ s[j-1] = s[b-1] $, and $ c(i,j) = 0 $ if $ s[i-1] = s[j-1] $, but if $ s[a-1] \ne s[b-1] $, then $ s[i-1] \ne s[j-1] $, so $ c(i,j) = |i - j| $, making total cost $ |a - i| + |i - j| + |j - b| \geq |a - b| $, so direct flight is optimal. Wait — correction: if $ s[a-1] \ne s[b-1] $, then: We can go $ a \to i $ (cost $ |a - i| $ if $ s[i-1] \ne s[a-1] $, else 0), then $ i \to j $ (cost 0 if $ s[i-1] = s[j-1] $, else $ |i - j| $), then $ j \to b $ (cost 0 if $ s[j-1] = s[b-1] $, else $ |j - b| $). But since $ s[a-1] \ne s[b-1] $, if we choose $ i $ such that $ s[i-1] = s[a-1] $, and $ j $ such that $ s[j-1] = s[b-1] $, then $ s[i-1] \ne s[j-1] $, so $ c(i,j) = |i - j| $, and total cost is $ |a - i| + |i - j| + |j - b| \geq |a - b| $ by triangle inequality. So the direct flight $ a \to b $ costs $ |a - b| $, and any detour through same-company airports cannot reduce cost below $ |a - b| $. But wait — what if we use **one** intermediate airport $ k $ such that $ s[k-1] = s[a-1] $, and then go $ a \to k \to b $? Then cost = $ c(a,k) + c(k,b) $. If $ s[k-1] = s[a-1] $, then $ c(a,k) = 0 $. Then $ c(k,b) = |k - b| $ if $ s[k-1] \ne s[b-1] $. So total cost = $ |k - b| $. We can choose $ k $ to minimize $ |k - b| $ among all $ k $ with $ s[k-1] = s[a-1] $. Similarly, we could go $ a \to k \to b $ with $ s[k-1] = s[b-1] $, then $ c(a,k) = |a - k| $, $ c(k,b) = 0 $, total = $ |a - k| $. So overall, if $ s[a-1] \ne s[b-1] $, the minimal cost is: $$ \min\left( |a - b|, \min_{\substack{k=1 \\ s[k-1] = s[a-1]}}^n |k - b|, \min_{\substack{k=1 \\ s[k-1] = s[b-1]}}^n |a - k| \right) $$ But note: $ \min_{k: s[k-1] = s[a-1]} |k - b| $ is the distance from $ b $ to the nearest airport with same company as $ a $. Similarly for the other. And since $ |a - b| $ is always an upper bound, the answer is: $$ \begin{cases} 0 & \text{if } s[a-1] = s[b-1] \\ \min\left( |a - b|, \min_{k: s[k-1] = s[a-1]} |k - b|, \min_{k: s[k-1] = s[b-1]} |a - k| \right) & \text{otherwise} \end{cases} $$ But note: $ \min_{k: s[k-1] = s[a-1]} |k - b| \leq |a - b| $ only if there exists a $ k $ with $ s[k-1] = s[a-1] $ closer to $ b $ than $ a $ is. Similarly for the other. Actually, since $ a $ itself satisfies $ s[a-1] = s[a-1] $, we have $ \min_{k: s[k-1] = s[a-1]} |k - b| \leq |a - b| $. Similarly, $ \min_{k: s[k-1] = s[b-1]} |a - k| \leq |a - b| $. So the minimum is just the minimum of those two values. Thus, the answer is: $$ \boxed{ \begin{cases} 0 & \text{if } s[a-1] = s[b-1] \\ \min\left( \min_{\substack{1 \leq k \leq n \\ s[k-1] = s[a-1]}} |k - b|,\ \min_{\substack{1 \leq k \leq n \\ s[k-1] = s[b-1]}} |a - k| \right) & \text{otherwise} \end{cases} } $$
Samples
Input #1
4 1 4
1010
Output #1
1
Input #2
5 5 2
10110
Output #2
0
API Response (JSON)
{
  "problem": {
    "name": "A. Vladik and flights",
    "description": {
      "content": "Vladik is a competitive programmer. This year he is going to win the International Olympiad in Informatics. But it is not as easy as it sounds: the question Vladik face now is to find the cheapest way",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF743A"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Vladik is a competitive programmer. This year he is going to win the International Olympiad in Informatics. But it is not as easy as it sounds: the question Vladik face now is to find the cheapest way...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Vladik 是一名竞赛程序员。今年他打算赢得国际信息学奥林匹克竞赛。但事情并没有听起来那么简单:Vladik 目前面临的问题是找到前往竞赛地点的最便宜方式。\n\nVladik 知道 #cf_span[n] 个机场。所有机场都位于一条直线上。每个机场都有一个从 #cf_span[1] 到 #cf_span[n] 的唯一编号,Vladik 的家位于编号为 #cf_span[a] 的机场旁边,竞赛地点位...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ n, a, b \\in \\mathbb{N} $ with $ 1 \\leq a, b \\leq n $.  \nLet $ s \\in \\{0,1\\}^n $ be a binary string of length $ n $, where $ s[i] $ denotes the company of airport $ i+1 $ (1-indexed).  \n\nDefine t...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments