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.
Examples
Input
4 1 4
1010
Output
1
Input
5 5 2
10110
Output
0
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.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given n airports located on a line at positions 1...n (their IDs are their positions). Each airport belongs to either company 0 or 1. The cost of a flight from i to j is 0 if company[i] == company[j], else |i - j|. We can take any number of flights, start at a, end at b. Find minimum total cost.
We need to compute minimum cost to go from a to b, with edges between any pair of airports (complete graph) with costs defined as above.
Observation: Since we can take any number of flights, we can think of building a graph with vertices 1..n, edge weights w(i,j) = (0 if same company else |i-j|). We need shortest path from a to b. However, n up to 1e5, complete graph too large. We need to exploit structure.
Note: If we can travel for free between airports of the same company (0 cost edges), then effectively all airports of a given company are in a zero-cost connected component: we can move between any two same-company airports at cost 0 (by potentially multiple flights: directly? Actually direct edge between two same-company airports is 0, so yes we can go directly zero). So any two airports with same company are essentially at distance 0 from each other. However, careful: If we can chain flights, but moving from i to j of same company directly costs 0, so indeed the distance between any two same-company airports is 0. So all same-company nodes are contracted into a single node per company? But there are two companies: So all 0-labeled airports are at zero distance from each other, and all 1-labeled airports are at zero distance from each other. But wait: Is there any restriction that edges are only between airports? Since we can use any sequence, we can go from a 0-airport to another 0-airport for free. So indeed all same-company airports are interchangeable with zero cost to move between them. So the only costs occur when we move between different company airports. And that cost is the absolute difference of their indices.
Thus, we can think of the contracted graph: two super-nodes: company 0 and company 1. But careful: The costs to move between an airport of company 0 and an airport of company 1 is |i-j|. But if we are at some airport i in company 0, we can first move (for free) to any other airport j in company 0, then take a flight from j to some k in company 1 costing |j - k|, and then from k to any other airport in company 1 for free. So the effective cost to go from a particular airport a (which has some company) to b (maybe different) is the minimum over possible crossing points: if a and b are same company, we can go directly for free? Actually if same company, direct edge is 0, so cost 0. But if they are different companies, we need at least one crossing. Since we can move for free within each company, the optimal cost is essentially the minimum absolute difference between any airport of company of a and any airport of company of b, because we can: start at a (company C_a), move for free to some airport i in C_a, then take a flight to some airport j in C_b with cost |i-j|, then move for free to b. Since the free moves are zero, the total cost is just |i-j|. And we can choose i and j arbitrarily among airports of respective companies. So the minimal cost is min_{i in set of company of a, j in set of company of b} |i - j|. However, note that we could also cross multiple times (e.g., go from a (0) to some 1, then to some 0, then to some 1, etc.), but that would only increase cost because crossing costs are positive (except maybe zero if crossing between same company? but that's zero anyway). Since crossing costs are nonnegative, and you can accomplish everything with at most one crossing. Actually if a and b are same company, cost 0, no crossing needed. If they are different, you need at least one crossing. Could more than one crossing reduce cost? Suppose you go from a (0) to i1 (1) costing |a-i1|? But you can move for free within 0 to some other 0 first. Actually if you cross multiple times, you might have something like: start at a (0), go to x (0) free, cross to y (1) cost |x-y|, then free to z (1), cross to w (0) cost |z-w|, then free to b (0). That would be total cost |x-y| + |z-w|. Since you could instead go from a to b directly within 0 for free, that is worse if both are 0. But if they are different companies, you need an odd number of crossings. One crossing is enough: go from a (0) to some 0, cross to some 1, then free to b (1). Could adding extra crossings (three crossings) ever reduce cost? For instance, maybe the direct crossing from any 0 to any 1 has a large distance, but by crossing to a different 0, then to a 1, then to another 1, then back to 0, then to 1? Actually if a and b are different, we need to end up in the other company. The total cost is sum of absolute differences at each crossing. Since all terms are non-negative, adding extra terms can only increase or keep same if some terms are zero. But can extra terms be zero? They would be zero only if crossing between same company? But crossing between same company is zero cost edge, but that's essentially free move within company which we already account as free. Actually an edge between two airports of same company costs 0. So you could "cross" between two same-company airports for free, but that's just moving within that company. So it doesn't add cost. So extra crossings that are between different companies add positive cost, so they don't help. However, could we have a situation where the minimal |i-j| is large but by using two crossings we could achieve lower total cost? For example, a=1 (0), b=4 (1). Suppose 0s at {1,3}, 1s at {2,4}. min |0-1| = |1-2|=1 or |3-4|=1. So min=1. But maybe going 1(0) -> 2(1) cost 1, then 2(1) -> 3(0) cost 1, then 3(0) -> 4(1) cost 1 total 3, worse. Could we do better by going 1(0) -> 3(0) free, then 3(0) -> 2(1) cost 1, then 2(1)->4(1) free? That's still 1. So not better. Could there be a scenario where the minimal absolute difference is not the optimum because you might need to start at a specific a and end at specific b? But since we can move for free within each company, we can always choose the optimal crossing pair (i,j) that minimizes |i-j|. However, we must also consider that a is fixed; but we can move for free to any airport of its company, so effectively we can start at any airport of the same company as a at zero cost. Similarly we can end at any airport of the same company as b at zero cost. So the problem reduces to: if s = company[a] == company[b], answer = 0. Else answer = min_{i in indices where company[i] = company[a], j in indices where company[j] = company[b]} |i - j|.
So we just need to compute the minimal absolute difference between any index of type C_a and any index of type C_b.
That seems straightforward: we have binary string of length n. We can precompute arrays of positions for each company. Then find min absolute difference between the two sets. Since n up to 1e5, we can do O(n) by merging sorted lists.
But is there any catch? Let's think: Could there be a scenario where using more than one crossing could give cost less than the min absolute difference? Suppose we have a=0 (company 0) and b=5 (company 1). Positions: 0s at {1,10}, 1s at {2, 100}. min |1-2|=1. That's fine. Could there be a scenario where there is no direct crossing because maybe the direct crossing cost is high, but by going via some intermediate airport of a third type? There are only two companies, so no third type. So one crossing is sufficient.
But wait: The cost definition: flight from i to j is zero if same company; else |i-j|. So direct flight between two different company airports costs the absolute distance. If we take two flights: i->k and k->j, total cost is cost(i,k)+cost(k,j). Could there be a case where cost(i,k)+cost(k,j) < |i-j|? Possibly if i and j are of different companies? But note that if i and j are different companies, the direct flight cost is |i-j|. By adding an intermediate k, maybe we can lower cost if the intermediate is strategically placed? For instance, i and j both of different companies? Suppose i=0 (company 0), j=10 (company 1). If k is of company 0? Then i->k is 0 (same company) and k->j cost |k-10|. So total = |k-10|. That's basically same as going from some 0 to j. If we choose k close to j, then cost is small. That is just the min distance from any 0 to j. Similarly if k is of company 1, then i->k cost |i-k|, k->j cost 0, total = |i-k|, i.e., min distance from i to any 1. So one intermediate node of the opposite company can be seen as effectively crossing at that node. But if we use two intermediate nodes: i (0) -> k1 (1) -> k2 (0) -> j (1). Then total = |i-k1| + |k1-k2|? Wait, cost(i,k1) if different? i is 0, k1 is 1 -> |i-k1|. cost(k1,k2): k1 is 1, k2 is 0 -> |k1-k2|. cost(k2,j): k2 is 0, j is 1 -> |k2-j|. So total = |i-k1|+|k1-k2|+|k2-j|. By triangle inequality, this is at least |i-j|? Actually |i-k1|+|k1-k2|+|k2-j| >= |i-j|? Not necessarily, consider i=0, j=10, choose k1=6, k2=5: then |0-6|=6, |6-5|=1, |5-10|=5 total 12 >10. But could it be less? Suppose i=0, j=10, choose k1=9, k2=1: |0-9|=9, |9-1|=8, |1-10|=9 total 26. So seems larger. In general, by triangle inequality, |i-k1|+|k1-k2| >= |i-k2|, and then plus |k2-j| >= |i-j|? Actually |i-k2|+|k2-j| >= |i-j|, so sum of three >= |i-j|. So any path with two crossings (odd number, but three edges) has total cost at least direct flight cost. However direct flight between different companies costs |i-j|. But our goal is to minimize cost, so we would never use more than one crossing (i.e., two edges: free move, then one paid edge, then free move). However, we might also consider that we can take a direct flight from a to b if they are same company, cost 0; if different, direct flight costs |a-b|. But maybe that's not minimal; maybe it's better to go via other airports: e.g., a=1 (0), b=3 (1). Suppose 0s: {1}, 1s: {100}. Then direct flight cost |1-100|=99. But we could go from 1 (0) to 2 (?) wait 2 might be of company 0? Actually we have only two companies, so any other airport must be either 0 or 1. But if we have only one 0 and one 1, the only crossing is direct from 1 to 100 cost 99. Could we go from 1 (0) to some other 0? There is none. So cost 99. But could we go from 1 (0) to 2 (if 2 is 0? but 2 is not 0, it's 1? Actually if 2 is 1, then cost |1-2|=1, then from 2 (1) to 100 (1) free, then to b=3 (1) free? Actually b=3 (1) so from 2 to 3 free, total cost 1. But we assumed b=3 is 1, so indeed there is 1s at 3. So we could cross from 1 (0) to 2 (1) cost 1, then free to 3. That's better than direct from 1 to 3 (if 1 and 3 are different? they are diff company? Actually a=1 (0), b=3 (1) are diff; direct cost |1-3|=2, but via 2 (1) cost 1, which is less. So min_{i in 0, j in 1} |i-j| = min(|1-2|,|1-3|, ...). Since 2 is 1, |1-2|=1. So answer = 1. So indeed min absolute difference between sets is the answer. That can be less than direct distance |a-b| if there is a closer opposite company airport. So the formula holds.
Thus answer = 0 if company[a] == company[b], else min distance between any 0 and any 1 (i.e., min absolute difference between indices of different company) but careful: we need specifically between sets of company of a and company of b. Since there are only two companies, if company[a] != company[b], then we need min distance between the set of indices with company = company[a] and set with company = company[b]. That is indeed the min distance between any 0 and any 1, because the two sets are complements. So essentially answer = 0 if same company, else min_{i in set0, j in set1} |i-j|. So it's just the minimum absolute difference between an index with label 0 and an index with label 1.
But wait: Example 1: n=4, a=1, b=4, string "1010". So positions: 1:1,2:0? Actually string "1010": index1='1', index2='0', index3='1', index4='0'. So company: 1:1, 2:0, 3:1, 4:0. a=1 (company 1), b=4 (company 0). So different. Compute min distance between any 1 and 0: distances: |1-2|=1, |1-4|=3, |3-2|=1, |3-4|=1. Minimum=1. Answer=1. Works.
But before finalizing, double-check any subtlety: Could there be a scenario where the answer is not simply min distance between two sets? Because we are allowed unlimited free moves within same company, so indeed we can choose any airport of a's company to start and any airport of b's company to end. However, is there any cost associated with moving from a to another airport of the same company? The flight cost between same-company airports is 0, so free. So we can start at a, then take a flight to any same-company airport for free. So yes.
Thus answer = 0 if s[a]==s[b] else min_{i where s[i]==s[a], j where s[j]==s[b]} |i-j|.
We need to compute this efficiently.
Simplest: iterate through string and keep track of positions of 0 and 1 in two lists. Then compute min absolute difference between two sorted lists: we can merge or use two-pointer.
But we can even compute in O(n) by scanning and tracking the last seen index of each type and updating min distance. Since we only need min distance between any 0 and any 1 (regardless of order). Approach: Initialize last0 = -inf, last1 = -inf, ans = large. Iterate i from 1 to n: if char == '0': update ans = min(ans, i - last1) if last1 != -inf; update last0 = i. Else char == '1': update ans = min(ans, i - last0) if last0 != -inf; update last1 = i. At the end, ans is min distance between any 0 and 1. Then answer = 0 if same else ans.
Check: For "1010": i=1 (1): last0? none, update ans? i - last0? last0=-inf skip; last1=1. i=2 (0): ans = min(ans, 2 - last1=1) => ans=1; last0=2. i=3 (1): ans = min(1, 3 - last0=1) =>1; last1=3. i=4 (0): ans = min(1, 4 - last1=1) =>1; last0=4. ans=1. Works.
For all zeros? But then a and b could be same company (0) and answer=0. If they differ, but there is only one company? Actually string consists of only 0 or only 1: then if a and b are different companies? But there are no airports of the other company, so is it possible? Since both a and b are airports, they must belong to one of the companies. If string all zeros, then all airports are company 0. So it's impossible that a and b have different companies. So the case where a and b are different companies cannot occur if that company doesn't exist. But input could have a and b both given, but they are indices, and each has a company according to string. So if string all zeros, then both are 0, so same company, answer 0. So min distance between 0 and 1 would be undefined (since one set empty). But our algorithm would output ans=large, but we only use ans if companies differ, which would not happen. So safe.
Thus code:
Read n, a, b.
Read string s (length n). Note: indices 1-based.
If s[a-1] == s[b-1] then print 0.
Else:
ans = n (or large)
last0 = -1, last1 = -1
for i in 1..n:
if s[i-1]=='0':
if last1 != -1: ans = min(ans, i - last1)
last0 = i
else:
if last0 != -1: ans = min(ans, i - last0)
last1 = i
print ans
Check: n up to 1e5, O(n) fine.
Edge Cases: a and b same? Then companies same obviously (since same airport has same company). Answer 0.
Now, is there any scenario where the min distance is 0? That would require two indices i and j with same index? But distinct airports have distinct indices, so distance >=1. However, could there be zero distance? No, because i!=j, so |i-j|>=1. So min distance at least 1. But answer can be 0 only if same company.
But could there be a scenario where the answer is 0 even if companies differ? Only if there exists i and j such that |i-j|=0? Not possible. So answer 0 only if same company.
Thus we are done.
But wait, I recall some Codeforces problem like this: "Airports" or "Cheapest Flights"? Actually it's problem "Airport" or "Vladik and flights"? It might be Codeforces Round #... The solution is indeed as above.
Double-check: Is there any nuance about having to start at airport a and finish at b? Could there be a possibility that even if companies are different, we could get 0 cost by making a flight from a to b via some intermediate airports where all flights are zero? That would require all airports along the way to be of the same company as a and b? But if a and b are different companies, then to have a zero cost path, every edge on the path must be between airports of same company. That would mean all vertices on the path must share the same company? But a and b are different, so impossible. So indeed cost >0.
Thus answer is correct.
Implementation: Use Python.
Complexities: O(n).
Now, the problem statement: "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." So that's exactly as we used.
One more nuance: When we say "Vladik can fly between any pair of airports any number of times" implies that we can use any edges. So yes.
Thus final solution: read input, compute.
Now check with some custom tests:
Test 1: n=2, a=1, b=2, s="01". a=1 (0), b=2 (1). min distance =1. Output 1.
Test 4: n=5, a=3,b=4, s="01010": a=3 (0), b=4 (1). min distance: between 0s {1,3,5} and 1s {2,4}. Minimum: |1-2|=1,|3-2|=1,|3-4|=1,|5-4|=1 =>1. Output 1.
Test 5: n=1, a=1,b=1, s="0" => same company =>0.
Test 6: n=1, a=1,b=1, s="1" =>0.
n=1 but a and b must be between 1 and n, so a=b=1, so same.
Edge: n=100000, all alternating "010101...". min distance always 1. So answer 0 or 1 accordingly.
Implementation details: Input reading with split.
Now, some might think about using graph theory and Dijkstra, but not needed.
So answer.
But before finalizing, re-read problem statement to ensure no twist: "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." This is symmetric.
"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." So any route.
Thus solution stands.
Thus code:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
n = int(data[0]); a = int(data[1]); b = int(data[2])
s = data[3].strip()
if s[a-1] == s[b-1]:
print(0)
return
ans = n # maximum possible distance
last0 = -1; last1 = -1
for i,ch in enumerate(s, start=1):
if ch == '0':
if last1 != -1:
diff = i - last1
if diff < ans: ans = diff
last0 = i
else:
if last0 != -1:
diff = i - last0
if diff < ans: ans = diff
last1 = i
print(ans)
Alternatively we could compute min distance by scanning with two pointers on lists, but above O(n) works.
But careful: In scanning, we only consider distances between a 0 and a 1 where the 0 appears after a previous 1, or a 1 after a previous 0. This ensures we consider all adjacent pairs, but what about pairs where both are far apart but not adjacent? Actually this method captures the minimum distance because the minimum distance must occur between some consecutive indices of opposite types when sorted. Indeed, if we sort all indices, the minimal distance between a 0 and a 1 is the minimal difference between any two indices of opposite type that are neighbors in the sorted order (since if we have indices sorted, the minimal distance between two sets is achieved by some pair that are consecutive in the sorted order across types). Our scan essentially checks every time we encounter a new index of type T, we compute its distance to the most recent index of opposite type, which yields all such consecutive cross-type pairs. So it's correct.
Thus finalize.
Now produce final answer with explanation. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
a = int(data[1])
b = int(data[2])
s = data[3] # string of length n, characters '0'/'1'
code
# if both airports belong to the same company, cost is zero
if s[a - 1] == s[b - 1]:
print(0)
return
# otherwise find the minimal distance between any '0' and any '1'
INF = n # maximum possible distance
best = INF
last0 = -1 # last index where company is '0'
last1 = -1 # last index where company is '1'
for i, ch in enumerate(s, start=1):
if ch == '0':
if last1 != -1:
best = min(best, i - last1)
last0 = i
else: # ch == '1'
if last0 != -1:
best = min(best, i - last0)
last1 = i
print(best)