Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Input
The first line contains number n (2 ≤ n ≤ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
Output
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
Examples
Input
3
HTH
Output
0
Input
9
HTHTHTHHT
Output
2
Note
In the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then — the tiger in position 9 with the hamster in position 7.
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 need to solve the problem: given a circular arrangement of H and T (hamsters and tigers), we can swap any two animals (they step out and swap). We want to achieve a configuration where all H's are consecutive, and all T's are consecutive (so the circle has one contiguous block of H and one contiguous block of T; since it's a circle, that means the arrangement consists of a segment of all H and the rest all T, but because it's circular, the break point can be anywhere: essentially the arrangement must have at most one transition from H to T and at most one from T to H). Actually on a circle, if you have two species, the condition "all H's together and all T's together" means that the circle is not mixed: either all H (but problem says at least one of each, so both present) so there will be exactly two contiguous arcs: one of H's and one of T's. That is, there is exactly one boundary where species change (from H to T) and one where it changes back (from T to H). So the arrangement is of the form: some number of consecutive H's, then some number of consecutive T's, and then back to H's because it's circular? Wait careful: In a circle, if you have two species, "all H's together and all T's together" means that if you walk around the circle, you will see a block of H's and a block of T's. Since it's a circle, there are exactly two boundaries: where H changes to T and where T changes to H. So it's either H...H T...T. That is, one contiguous segment of H and one of T. So indeed the string (when linearized starting at some point) will be either all H's then all T's or all T's then all H's. But because it's circular, we can choose the starting point arbitrarily. So the goal is to make the circle such that there exists a point where from that point onward, we have some number of H's (maybe zero) then T's then back? Actually if we linearize by cutting at some point, we will get a linear string that is either all H's followed by all T's or all T's followed by all H's. However, note that the circle condition: if we have all H's together and all T's together, then the circular arrangement is essentially two arcs. When we cut the circle at some point, the resulting linear string could have the form H...H T...T (if the cut is within the H block or the T block appropriately) or T...T H...H. But also possibly if the cut is exactly at a boundary, we get either pure H then T or pure T then H. So overall, any valid circular arrangement corresponds to a linear string with at most one change from H to T and at most one from T to H; actually exactly two changes if both species present. But linear string with exactly one change? Let's think: Suppose we have a circular arrangement with one block of H and one block of T. If we cut at a point inside the H block, the linear string will start with H's, then at some point switch to T's, then at the end we have T's and then... but we cut before reaching the rest of H's? Wait, if we cut inside the H block, the string will start with some H's (the tail of the H block after the cut), then eventually we encounter the T block, go through all T's, then return to the remaining H's (the part of the H block before the cut). So the linear string will have the form: some H's, then T's, then H's. That's two transitions: H->T and T->H. Similarly if cut inside T block, it's T's then H's then T's. So a valid circular arrangement yields a linear string that, when considered cyclically, has exactly two transitions. But the problem likely expects to find minimal swaps to achieve a configuration where all H are together and all T together (in circle). We can swap any two positions.
Observation: Since swaps are between any two positions, the minimal number of swaps to achieve a target permutation is something like n minus number of cycles. But here we are allowed to choose any target arrangement that meets the condition. We want to minimize the number of swaps needed to transform the current circular arrangement into some valid arrangement (any of the valid ones). So we need to compute minimal number of swaps to achieve a circular string with one H block and one T block.
Alternatively, we can think: In a swap, we can exchange two elements. The minimal number of swaps to transform one binary string into another (with equal numbers of H and T? Not necessarily equal counts) is something like: we need to swap mismatched positions. Since we have two types, the minimal number of swaps to get from current string S (circular) to a target string T (also circular) is the number of positions where S differs from T divided by 2? Actually careful: If we want to transform S to T by swapping any two positions, the minimal number of swaps required equals the number of mismatched positions minus the number of cycles in the permutation that maps positions of mismatched elements. But since it's a binary string with possibly different counts? Actually the target arrangement must have the same number of H's and T's as original because we're just swapping, so counts are fixed. So we have a fixed number of H = h and T = t, with h + t = n. In the target arrangement, we want all H together and all T together. That means any valid target is determined by the position of the H block (or the cut). The H block can be placed anywhere as a contiguous segment of length h (circularly). So there are n possible starting positions for the H block (since circle, but some may be same if rotations? Actually rotations of the circle are considered different arrangements? The problem: we have a fixed circle with labeled positions? The animals are placed around the arena in positions; presumably positions are fixed (like positions 1 to n around the circle). So the target arrangement is not considered up to rotation; we need to assign which positions become H. But the condition "all hamsters stood together and all tigers also stood together" does not require that the block is in a specific location; any block location is acceptable. So we can choose any set of h positions that are consecutive (circularly) to become H. So essentially we need to choose a segment of length h (wrapping around) to be H, and the rest T. So we need to compute, for each possible starting index i (0-indexed) of the H block (i from 0 to n-1), the cost to transform the current string into a string where positions i, i+1, ..., i+h-1 (mod n) are H and the rest are T. The minimal cost over i is the answer.
Cost to transform to a target binary string via swaps: Since we have exactly h H's in both source and target, the minimal number of swaps needed is the number of positions where source has T but target wants H (these are "misplaced" H's) = number of H's missing in the H block. Actually let's define: For a given target block, we need to move H's into the block and move out any H's outside the block. Swapping two positions can fix two mismatches at once if one is H outside and one is T inside. So minimal swaps = min(# of H outside block, # of T inside block) = the number of "errors". In fact, the minimal number of swaps to turn source into target (both with same count of H) is exactly the number of positions where source and target differ, divided by 2. Because each swap can fix at most two mismatches. More formally, define mismatches positions where source has H but target has T (call them H->T) and where source has T but target has H (T->H). The number of mismatches is even and the counts of these two types are equal (since total H counts match). Then the minimal number of swaps is exactly the number of H->T mismatches (which equals number of T->H mismatches). Because each swap can swap one H in T region with one T in H region, fixing both. And you cannot fix a mismatch with less than one swap per two mismatches? Actually you could also swap a mismatched H with a correctly placed H? That would not reduce total mismatches efficiently. So optimal is to pair each H in T region with a T in H region. So number of swaps required = number of H's outside the H block (which equals number of T's inside the H block) = let's denote for the block: let X = number of T's inside the block (positions that should be H but are T), which equals number of H's outside the block (since total H = h, inside block H count = h - X, so outside block H count = h - (h - X) = X). So indeed cost = X.
Thus for a given block (set of h consecutive positions), cost = number of T's in that block (or equivalently number of H's outside the block). So we need to compute, over all length-h circular segments, the minimum number of T's in that segment.
But wait: is that always the minimal number of swaps? Consider if we swap two animals, they don't have to be one from inside block and one from outside? Could there be a scenario where swapping two H's inside the block or two T's outside reduces future swaps? No because that would not directly fix mismatches. The minimal number of swaps needed to achieve a specific target is indeed the number of mismatched positions divided by 2 = number of H's in the T region. That's a well-known result for transforming one binary string into another with the same number of 1s via adjacent swaps? Not adjacent, but any swaps. With arbitrary swaps, it's just the number of positions where they differ divided by 2, because you can pair mismatched positions. Yes, each swap corrects two positions. So cost = mismatches/2 = number of T in H block = number of H in T block.
But we must be cautious: The transformation is via swaps of any two animals, not necessarily adjacent. So it's correct.
Thus the problem reduces to: Given a circular binary string of length n, with h = number of H's, find the minimum over all contiguous (circular) segments of length h of the number of T's in that segment. Since the H block can be placed anywhere, and the rest become T block. That yields cost = min_Tcount_in_segment_of_length_h.
But we must consider also the possibility that the H block could be placed such that the H's are together but also the T's are together, but note that if we place the H block as a segment of length h, the T block automatically is the complement (length t). That ensures both species are contiguous. So yes.
But is there any alternative where the H block is not exactly h consecutive positions? That would be the only way to have all H together: if there are h H's, they must occupy exactly h consecutive positions on the circle for them to be contiguous. So indeed the block length is h. So the above is correct.
So answer = minimum over i from 0 to n-1 of (number of T's in segment of length h starting at i). This can be computed easily by sliding window because n <= 1000, so O(n^2) is fine. But we can do O(n) by precomputing prefix sums and scanning circularly.
We need to be careful: The segment is circular. So we can duplicate the string to handle wrap-around, or use prefix sums modulo.
Implementation: read n, read string s (length n). Count number of H (or T whichever). Let h = count of 'H'. For each start index i in [0, n-1], consider segment s[i..i+h-1] (mod n). Compute number of T's in it. Take min.
Complexities: O(n * h) naive but n <= 1000, h <= n, worst-case O(n^2)=1e6, fine. But we can do O(n) with sliding window: Since we need segments of fixed length h, we can compute an array of length 2n by concatenating s to itself, compute prefix sums of T count, then for each i from 0 to n-1, compute sum over i..i+h-1. Then take min.
But be careful: if h = 0 or h = n? At least one hamster and one tiger, so both present, so 1 <= h <= n-1. So fine.
Double-check examples:
Example 1: n=3, s=HTH. h = count(H)=2? Wait H T H -> H count = 2, T count =1. So segments of length 2: positions (0,1): H T => T count =1; (1,2): T H => T count =1; (2,0) circular: H H? Actually segment length 2 starting at 2: indices 2 (H) and 0 (H) => T count =0. So min=0. Output 0. matches.
Example 2: n=9, s=HTHTHTHHT. Let's count H: H T H T H T H H T? Actually s: positions: 0:H,1:T,2:H,3:T,4:H,5:T,6:H,7:H,8:T? Wait given: "HTHTHTHHT". Let's write: index:0 H,1 T,2 H,3 T,4 H,5 T,6 H,7 H,8 T? But string length 9: H T H T H T H H T? That is 9 chars: H,T,H,T,H,T,H,H,T. Yes. So H count: indices 0,2,4,6,7 =5. So h=5. We need min T count in segments of length 5. Compute all segments (circular). Let's compute quickly: We'll list the 9 possible start indices and compute T count in next 5.
Indices:
0..4: H T H T H => T at 1,3 => 2 T's.
1..5: T H T H T => T at 1,3,5? Actually indices:1:T,2:H,3:T,4:H,5:T => T count=3 (positions 1,3,5)
2..6: H T H T H => indices2:H,3:T,4:H,5:T,6:H => T count=2 (3,5)
3..7: T H T H H => indices3:T,4:H,5:T,6:H,7:H => T count=2 (3,5)
4..8: H T H H T => indices4:H,5:T,6:H,7:H,8:T => T count=2 (5,8)
5..0 (wrap): T H H T H => indices5:T,6:H,7:H,8:T,0:H => T count=2 (5,8)
6..1: H H T H T => indices6:H,7:H,8:T,0:H,1:T => T count=2 (8,1)
7..2: H T H T H => indices7:H,8:T,0:H,1:T,2:H => T count=2 (8,1)
8..3: T H T H T => indices8:T,0:H,1:T,2:H,3:T => T count=3 (8,1,3)
Wait careful: for start 8: indices:8,0,1,2,3 = T,H,T,H,T => T at 8,1,3 =>3.
Now min seems to be 2. Output 2. matches.
Thus solution is correct.
But let's consider if there is any other possible target arrangement: Could the H block be of length t? No, because number of H is h, so block length must be h. So the above covers all.
Now we need to output the minimal number of swaps.
Therefore solution is simple.
But wait, is there any nuance that the circle arrangement after swaps can also be considered as the H block being of length h but the T block also contiguous automatically. However, note that the condition "all hamsters stood together and all tigers also stood together" in a circle means there are exactly two contiguous blocks. But if h = 0 or h = n, it's degenerate, but not the case. So correct.
Thus implement:
Read n.
Read string s.
Count h = number of 'H'.
If h == 0 or h == n, answer = 0 but not possible due to constraints (at least one of each).
Create string t = s + s (or use modulo indexing). Or use sliding window: create array a of length 2n where a[i] = 1 if s[i%n] == 'T' else 0.
Compute prefix sum ps of length 2n+1: ps[0]=0; for i from 0 to 2n-1: ps[i+1] = ps[i] + a[i].
Initialize ans = INF.
For i from 0 to n-1:
cnt_T = ps[i+h] - ps[i]
ans = min(ans, cnt_T)
Print ans.
Edge Cases: n up to 1000, fine.
Alternative: Could there be any scenario where it's better to have the H block be not a contiguous segment? That wouldn't satisfy condition.
Therefore answer.
Now, is there any possibility that the minimal swaps could be less than the minimum T count? Could we achieve the target with fewer swaps by swapping in a more clever way not corresponding to directly pairing mismatched H and T? Since each swap can at best fix two mismatches, and the number of mismatches is 2 * (# T in H block). So you need at least that many swaps. So it's optimal.
Thus solution is correct.
But wait, the problem statement says: "He orders two animals to step out of the circle and swap places." That means swaps are between any two positions (not necessarily adjacent). So our reasoning holds.
Thus answer.
Now we need to produce final answer: Implementation in C++ or Python. Let's produce Python solution:
def solve():
import sys
data = sys.stdin.read().split()
n = int(data[0])
s = data[1].strip()
h = s.count('H')
# If either count zero, answer 0 (but not needed)
# duplicate s
ss = s + s
# prefix sum of T's
ps = [0] * (2*n + 1)
for i,ch in enumerate(ss):
ps[i+1] = ps[i] + (ch == 'T')
ans = n # max possible
for i in range(n):
cnt = ps[i+h] - ps[i]
if cnt < ans:
ans = cnt
print(ans)
But careful: The segment length is h, and i goes up to n-1. For i near n-1, i+h may be up to n-1+h <= 2n-1 because h <= n-1, so i+h <= n-1 + (n-1) = 2n-2, which is within 2n. So fine.
Now think about test: n=2, s=HT, h=1, segments: start0: index0 H? Actually s="HT", h=1. segments of length1: at start0: H => T count=0; start1: T => T count=1. min=0. answer 0. Indeed we need 0 swaps because H and T are already separated? But in a circle of two, positions opposite each other. Are they together? In a circle with 2 positions, H and T: The H is next to T, T is next to H. But condition: all hamsters together and all tigers together: With one H and one T, they cannot be together because there is only one of each, but they are separated? Actually "together" means they are consecutive: all H's should be in a contiguous block. With one H, a block of length 1 is fine; similarly for T. However, are they separated? The arrangement is H T. In a circle, the H is adjacent to T, but does that violate "all hamsters stood together"? Since there is only one hamster, it's trivially together. Similarly for tiger. So they are already together? Actually the condition requires that all hamsters are in one contiguous segment, and all tigers are in one contiguous segment. With one of each, any arrangement satisfies this? Let's think: In a circle, a block of length 1 is contiguous. So the H alone forms a contiguous block. The T alone forms a contiguous block. However, the two blocks are adjacent; that's okay because there are exactly two blocks. So the arrangement is already valid. So answer 0 is correct.
Another test: n=4, s=HTHT, h=2. Segments length 2: positions: (0,1): H T => T count=1; (1,2): T H => 1; (2,3): H T =>1; (3,0): T H =>1? Actually (3,0): indices 3 T, 0 H => T count=1. So min=1. Is answer 1? Let's think: Current arrangement: H T H T alternating. To have all H together and all T together, we need two consecutive H's and two consecutive T's. One swap can do it? For example, swap positions 1 (T) and 2 (H) yields H H T T? Let's see: originally [H,T,H,T]. Swap indices 1 and 2: become [H, H, T, T] which is valid. Yes 1 swap. So answer 1 matches min T count.
Thus solution works.
But is there any scenario where minimal swaps could be less than the number of T in H block? Possibly if we consider that we are not forced to choose a specific target; maybe we can achieve the condition without making the H block exactly a segment of length h? The condition requires that all H's are together; they must occupy a set of positions that are consecutive on the circle. That set is exactly a contiguous block of length h. So yes.
Thus answer.
Now complexity O(n). So done.
But wait, is it possible that the minimal number of swaps to achieve the condition overall (without specifying which positions become H) might be less than the minimal mismatches for any fixed block because we might also consider swapping animals to achieve a configuration where the H block is not exactly of length h? That doesn't make sense.
Thus solved.
Double-check with more examples: n=5, s=HHHTT? Actually h=3. Then compute min T count in segments length 3. Suppose s=HHHTT. Then segments: start0: HHH => T count=0 => answer 0. Already all H together? Actually string is HHHTT (linear) but circularly it is also HHHTT with wrap? At wrap from end to start: T next to H? But since H block is from positions 0-2, T block positions 3-4, it's contiguous, so valid. So answer 0. Good.
Now edge: n=5, s=HTHTH (alternating). h=3? Count H: H T H T H => 3 H, 2 T. Then segments length 3: start0: H T H => T count=1; start1: T H T => 2? Actually indices1-3: T,H,T => T count=2; start2: H T H => 1; start3: T H H? Wait indices 3,4,0: T, H, H? Actually index3=T,4=H,0=H => T count=1; start4: H H H? indices4=H,0=H,1=T => T count=1? Actually start4: indices 4,0,1 => H, H, T => T count=1. So min=1. Could we achieve with 1 swap? Let's try: H T H T H. Want three consecutive H's. Suppose we swap positions 1 (T) and 4 (H) => becomes H H H T T? Let's see: original: 0:H,1:T,2:H,3:T,4:H. Swap 1 and 4: 0:H,1:H,2:H,3:T,4:T => yes that's HHHTT, valid. So 1 swap works. So answer 1 matches min.
Thus correct.
One more: n=6, s=TTTHHH, h=3, min T in segment length 3 = 0? Actually segment starting at 0: TTT => 3 T, not good. But we can choose segment starting at 3: HHH => T count=0. So answer 0 because already valid. Good.
Thus solution appears.
Now implement. Since n <= 1000, we could also brute force O(n^2). But I'll implement O(n) sliding window.
Now potential pitfalls: The string may have spaces or newline? Input format: first line n, second line string. So reading via sys.stdin.read().split() is fine.
Now let's ensure we handle the case where h = 0 or h = n? Input guarantees at least one of each, so not needed but safe.
Thus final answer.
But wait, is there any nuance about the circle that might allow two valid configurations: one with H block and T block, but also the reverse: if we consider the arrangement where T block is together and H block is together, it's the same condition. Our method includes both because choosing a segment of length h for H is equivalent to choosing the complement segment of length t for T. But note: Could there be a configuration where the H block is not contiguous but all H are together? No.
Thus done.
Now consider if h > t? It doesn't matter.
So final solution is straightforward.
But the problem statement says "swap two animals to step out and swap places" meaning swaps are arbitrary. So minimal swaps we derived.
Let's think if there is any possibility of doing fewer swaps than the number of mismatches? Could there be a scenario where the target block is not a segment of length h but because the circle's arrangement is considered up to rotation? But positions are fixed. So we need to assign which positions become H. So we have to choose exactly which positions will be H in final arrangement. If we choose a block of length h, it determines the assignment. So the cost is exactly number of T in that block. Could there be a final arrangement where H's are contiguous but not as a single block of length h? They must be exactly h consecutive positions. So it's exactly a block.
Thus solution.
Now code.
But wait: The minimal number of swaps might be smaller than the minimum number of T in block if we allow the possibility of doing swaps that also rotate the circle? But rotating the circle would correspond to a cyclic shift of positions, which is not allowed because positions are fixed. So we can't rotate; we must swap animals.
Thus answer.
Now, final solution in Python:
def main():
import sys
data = sys.stdin.read().strip().split()
if not data: return
n = int(data[0])
s = data[1].strip()
h = s.count('H')
# If h == 0 or h == n -> 0, but not needed.
# Build extended string
ss = s + s
# prefix sum of T's
ps = [0] * (2*n + 1)
for i, ch in enumerate(ss):
ps[i+1] = ps[i] + (1 if ch == 'T' else 0)
ans = n # maximum possible swaps (can't exceed n)
for i in range(n):
cnt = ps[i+h] - ps[i]
ans = min(ans, cnt)
print(ans)
if name == "main":
main()
That's it.
But wait: There's a nuance: The segment length h might be zero? But not possible. So fine.
Now I'll produce final answer. ```python
import sys
def main() -> None:
data = sys.stdin.read().split()
if not data:
return
n = int(data[0])
s = data[1].strip()
code
h_cnt = s.count('H') # number of hamsters
# duplicate the string to handle circular segments easily
ss = s + s
# prefix sums of tigers (T)
pref = [0] * (2 * n + 1)
for i, ch in enumerate(ss):
pref[i + 1] = pref[i] + (1 if ch == 'T' else 0)
ans = n # worst case: every position is wrong
for start in range(n):
tigers_in_segment = pref[start + h_cnt] - pref[start]
if tigers_in_segment < ans:
ans = tigers_in_segment
print(ans)