Codeforces 344A Magnets Solution & Explanation

Difficulty : 800

Problem Description

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.

Eamples

Input6
10
10
10
01
10
10
Output3

The group will be : 101010, 01 , 1010

Input4
01
01
10
10
Output2

Solution

In our solution, we only need to check whether the last element of the current row and the last element of the next row are the same. If they are, then they are in the same group; otherwise, sorry, they will be in another group!

C# Solution

Solution1

int n = int.Parse(Console.ReadLine());  //number of magnets 

int cnt = 1;
char curr = Console.ReadLine()[1];

for(int i = 1; i<n ; i++)
{
    char compare = Console.ReadLine()[1]; 
    if(curr != compare){
        cnt+=1;
        curr = compare;
    }
}

Console.WriteLine(cnt);

Java Solution

Solution1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine()); //number of magnets 
        int cnt = 1;
        
        char curr = scanner.nextLine().charAt(1);

        for(int i = 1; i<n ; i++)
        {
            char compare = scanner.nextLine().charAt(1); 
            if(curr != compare){
                cnt+=1;
                curr = compare;
            }
        }
        System.out.println(cnt);
    }
}

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 344A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *