Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a "plus") and negative (a "minus"). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other.
Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own.
Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 100000) — the number of magnets. Then n lines follow. The i-th line (1 ≤ i ≤ n) contains either characters "01", if Mike put the i-th magnet in the "plus-minus" position, or characters "10", if Mike put the magnet in the "minus-plus" position.
Output
On the single line of the output print the number of groups of magnets.
Examples
Input
6
10
10
10
01
10
10
Output
3
Input
4
01
01
10
10
Output
2
Note
The first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets.
The second testcase has two groups, each consisting of two magnets.
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 read the problem: It describes magnets in a row, each magnet has two poles: positive and negative. They are rectangular magnets placed horizontally, so each magnet has a left end and a right end, and the poles are on ends? Actually typical representation: each magnet is a bar with a north pole and south pole. The problem states each magnet has two poles, positive (plus) and negative (minus). The magnets are placed horizontally, so we consider orientation: either "01" meaning plus-minus? Or "10" meaning minus-plus? Need to interpret.
We have input lines: each line is either "01" or "10". They represent the orientation of the magnet when placed. We need to determine the number of groups after arranging all magnets one by one to the right.
Specifically: Mike starts by laying one magnet horizontally. Then each step adds one more magnet horizontally to the right end of the row. Depending on how he puts the magnet, it either is attracted to the previous one (forming a group) or repelled, then he lays it at some distance to the right from the previous one. A sole magnet not linked forms its own group.
Essentially: When we place magnets consecutively, the interaction depends on the adjacent poles: the rightmost pole of the previous magnet (the one at the boundary) and the leftmost pole of the new magnet. If they are opposite (attract), then the new magnet will be attracted and join the previous group (i.e., it will be adjacent, linked). If they are like (repel), then they will be separated by a distance (so they are not in the same group). So the groups are contiguous segments of magnets that are directly adjacent? Actually if they are repelled, they are placed at a distance, so they are not connected; thus groups are separated by gaps. So we need to count number of contiguous groups where magnets are attracted consecutively.
But wait: The description: "Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one)." So if attracted, they become linked (adjacent). If repelled, there is a gap. So groups are maximal sets of magnets that are linked consecutively (i.e., where each adjacent pair attracted). So we need to count how many groups.
But how do we know if attraction or repulsion? It depends on the poles facing each other: the rightmost pole of the previous magnet and the leftmost pole of the new magnet. So we need to know the orientation of each magnet: each magnet has two ends: left and right. The representation "01" or "10" likely indicates the poles from left to right: maybe "01" means left pole is 0 (positive? or negative?) and right pole is 1 (negative? or positive?). Usually in such problems (like Codeforces 344A - Magnets), they use "01" and "10" to represent orientation of magnets. Actually there is a well-known Codeforces problem "Magnets" (344A) where they count groups when magnets are placed in a line. In that problem: Each magnet is represented as "01" or "10". The magnets attract when adjacent poles are different. So groups are counted as number of times the orientation changes? Actually that problem: count number of groups. For n magnets, each magnet is either "01" or "10". When consecutive magnets are different, they attract? Actually let's recall: In Codeforces 344A, the problem is: "Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. ..." It's exactly the same description. And the solution is simply to count the number of adjacent pairs that are different, and then add 1? Actually typical solution: Count the number of changes from one magnet to the next. For example, given input: 6 magnets: 10,10,10,01,10,10 output 3. Let's test: sequences: 10,10,10,01,10,10. Pairs: (10,10) same -> groups? If same, they repel? Actually let's see: In Codeforces 344A, the magnets attract when the right pole of the first is different from the left pole of the second. They represent each magnet as a combination of poles: "01" means left is '0' (say negative) and right is '1' (positive); "10" means left is '1', right is '0'. So attraction occurs when right pole of previous (second character) equals left pole of current (first character)? Actually if they are opposite, they attract. Opposite means one is positive and the other negative. If we assign 0 as negative, 1 as positive? Not sure. But typical solution for Codeforces 344A: We count the number of groups as the number of times the magnet orientation changes plus 1? Let's test: sequence: 10,10,10,01,10,10. Changes: between 10->10: no change; 10->10: no; 10->01: change (10 to 01 is different); 01->10: change; 10->10: no change. So number of changes = 2. Then groups = changes + 1? That would be 3. That matches output 3. Next example: 01,01,10,10. Sequence: 01,01: no change; 01->10: change; 10->10: no change. Changes = 1, then groups = 2, matches output 2. So indeed, the number of groups is simply the number of adjacent pairs where the two magnets are different plus 1. Or equivalently, we can count groups by scanning and increment a counter whenever current magnet differs from previous.
But is that always correct? Let's think: groups are formed by magnets that attract. When do they attract? If the right pole of previous equals the left pole of current? Actually attraction occurs when the facing poles are opposite (i.e., different). So if previous magnet's right pole is, say, A, and current magnet's left pole is B, they attract if A != B. Then they will be linked. So if they attract, they are in the same group. If they repel (A == B), then they are separated, thus start a new group.
Thus for magnet i and magnet i+1, we need to check if they attract or repel. That depends on previous magnet's right pole and current's left pole. So we need to know: orientation of magnet i: string s_i of length 2, s_i[0] is left pole, s_i[1] is right pole. For magnet i+1: s_{i+1}[0] is left pole. So they attract if s_i[1] != s_{i+1}[0]. They repel if s_i[1] == s_{i+1}[0].
Now, groups: start with first magnet as a group. For each subsequent magnet, if it attracts to the previous magnet (i.e., s_{i-1}[1] != s_i[0]), then it joins the same group; else (repel) it starts a new group. So number of groups = 1 + count of indices i from 2 to n where s_{i-1}[1] == s_i[0] (repel condition)? Actually we count when a new group starts, which occurs when repel: i.e., s_{i-1}[1] == s_i[0]. So groups = 1 + number of repels. Alternatively, we can count groups as 1 + count of i where s_{i-1}[1] == s_i[0]. However, the example sequence: 10,10,10,01,10,10. Let's compute:
s1="10": left=1, right=0
s2="10": left=1, right=0 -> s1[1] == s2[0]? 0 == 1? No -> 0 != 1, so they attract (join group). So no new group.
s2->s3: s2[1]==s3[0]? 0==1? no -> attract.
s3->s4: s3[1]==s4[0]? 0==0? s3="10": right=0; s4="01": left=0 -> 0==0 -> repel -> new group.
s4->s5: s4[1]==s5[0]? s4="01": right=1; s5="10": left=1 -> 1==1 -> repel -> new group.
s5->s6: s5="10": right=0; s6="10": left=1 -> 0!=1 attract -> no new group.
Thus repel count = 2 (at indices 4 and 5). Groups = 1+2=3. That matches.
Second example: 01,01,10,10.
s1="01": left=0, right=1
s2="01": s1[1]==s2[0]? 1==0? No -> attract
s3="10": s2[1]==s3[0]? 1==1? Yes -> repel -> new group
s4="10": s3[1]==s4[0]? 0==1? No -> attract
Repel count=1, groups=2.
So indeed, groups = 1 + count of i such that s_{i-1}[1] == s_i[0].
Alternatively, count groups as number of times the magnet orientation differs? Actually in the typical Codeforces 344A solution, they count groups as the number of times the magnet string changes? But that would be different: change when s_i != s_{i-1}. In the first example: changes: 10->10 no change, 10->10 no, 10->01 change, 01->10 change, 10->10 no => changes =2, groups = changes+1=3. That matches if we treat "change" as different string. But is that always equal to the condition s_{i-1}[1] == s_i[0]? Let's check: If s_{i-1} != s_i (i.e., string different), does it guarantee s_{i-1}[1] == s_i[0]? Let's test. The strings are only two possibilities: "01" and "10". So if they are different, that means one is "01" and the other is "10". For "01" and "10": "01" right=1, "10" left=1, so s_{i-1}[1] (1) == s_i[0] (1) => repel. Wait that's repel. So if they are different, we get repel, meaning new group. If they are same, both "01" or both "10": For "01" and "01": right=1, left=0 -> 1 != 0 -> attract. For "10" and "10": right=0, left=1 -> 0 != 1 -> attract. So indeed, when strings are equal, they attract (no new group); when strings differ, they repel (new group). So the condition s_{i-1}[1] == s_i[0] is equivalent to s_{i-1} != s_i. Because the only two strings, the cross condition yields equality exactly when they are different. Let's verify:
Case s_{i-1} = "01", s_i = "10": then s_{i-1}[1] = '1', s_i[0] = '1' -> equal -> repel. So yes, new group when they differ. When s_{i-1} = "10", s_i = "01": s_{i-1}[1] = '0', s_i[0] = '0' -> equal -> repel.
When s_{i-1} = "01", s_i = "01": s_{i-1}[1] = '1', s_i[0] = '0' -> not equal -> attract. Similarly for "10","10": s_{i-1}[1] = '0', s_i[0] = '1' -> not equal -> attract.
Thus the rule simplifies to: groups = number of times the magnet string changes from previous plus 1. Or groups = 1 + count_{i=2..n} (s_i != s_{i-1}).
That's the classic solution.
But wait, problem constraints: n up to 100000, so O(n) is fine.
Thus the solution: read n, then read n strings (each exactly two characters). Initialize groups = 1 (if n>=1). For i from 1 to n-1, compare current string with previous; if different, increment groups. Output groups.
Edge cases: n=0? But constraints say n≥1, so n>=1.
Thus the solution is straightforward.
But the problem statement: "Determine the number of groups that the magnets formed." This is exactly Codeforces problem 344A.
We'll implement accordingly.
But the conversation includes some instructions: The user says "Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. ..." So they've given the problem.
Thus as ChatGPT, we should produce a solution.
We need to output a single integer.
We'll write code in some language? Usually we can provide a Python solution.
Let's write solve() function.
Implementation:
def solve():
import sys
data = sys.stdin.read().split()
n = int(data[0])
magnets = data[1:] # each string
# Alternatively read line by line
groups = 1
for i in range(1, n):
if magnets[i] != magnets[i-1]:
groups += 1
print(groups)
But careful: if n==0 (not possible), groups would be 0 but not needed.
Test with examples:
Example1: n=6, magnets = ['10','10','10','01','10','10'] -> changes at i=3? Actually compare indices: i=1: '10' vs '10' same; i=2: '10' vs '10' same; i=3: '10' vs '01' diff -> groups=2; i=4: '01' vs '10' diff -> groups=3; i=5: '10' vs '10' same -> groups=3. Output 3.
But maybe the problem expects a different approach? Could be that the magnets are placed with orientation but the left and right pole representation matters only in the rule? Already accounted.
Thus final answer: Python code.
But the user didn't explicitly ask for code. The user just gave the problem statement. In such interactions, the AI typically provides solution and explanation. Since the user said "Mad scientist Mike entertains himself... Determine the number of groups" and then gives input and output examples, they might be asking to solve the problem. Usually they expect code? The prompt: "Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. ... Determine the number of groups that the magnets formed. Input ... Output ..." So it's a typical programming problem statement. The user likely expects a solution: algorithm description and maybe code. But as ChatGPT, we should output the final answer, which is the code? However, the instruction in the chat: "You are an AI programming assistant, ... Provide code in the requested language." Not sure.
Given the history: The user's first message is the problem statement. So we need to provide the solution. Usually we provide an explanation and then the code in a language (like Python). So I'll do that.
But note: The user didn't specify a language. Often it's safe to provide Python.
Thus final answer: Provide Python code.
But there is also possible that the problem expects the answer as just the number? No, it's a programming problem, we need to write a program that reads input and outputs the number.
Thus output code.
I'll write:
python
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
magnets = data[1:1+n] # just in case there might be extra lines
if n == 0:
print(0)
return
groups = 1
for i in range(1, n):
if magnets[i] != magnets[i-1]:
groups += 1
print(groups)
if __name__ == "__main__":
main()
But careful: The magnets strings could be read with newline, but read().split() works.
Alternatively, using sys.stdin lines: but it's fine.
Time complexity O(n), memory O(n) for storing strings. Could also do streaming, reading one by one, but storing is fine for n=100000.
So that's it.
Thus final answer. ```python
import sys
def main() -> None:
data = sys.stdin.read().split()
if not data:
return
n = int(data[0])
magnets = data[1:1 + n] # read exactly n magnet strings
if n == 0:
print(0)
return
code
groups = 1 # the first magnet always starts a group
for i in range(1, n):
if magnets[i] != magnets[i - 1]:
groups += 1 # a new group begins when the orientation changes
print(groups)