TM19A - editorial

PROBLEM LINK:

Practice
Contest

Author: Dev Vaghani
Tester: Harsh Raj
Editorialist: Dev Vaghani

DIFFICULTY:

EASY

PREREQUISITES:

Greedy, Math

PROBLEM:

In a queue men allow women standing behind them to go in front of them every second. Given initial queue and number of seconds find the final queue.

EXPLANATION:

Add the positions where swaps needs to be done in a list, and swap them every second.

DETAILED EXPLANATION:

Since, left to right, swapping might not work, due to mistakes that can happen due to long queue of GLLL, cause only 1 lady passes a gentleman. So, we can use a list to keep track of each position that needs to be swapped. We use this list to swap the positions and increment seconds by 1. We do the same to the entire queue again.

SOLUTIONS:

Solution
using System;
using System.Collections.Generic;
class some
{
	public static void Main()
	{
		int n=int.Parse(Console.ReadLine());
		while((n--)>0)
		{
			string[]ss=Console.ReadLine().Split();
			int a=int.Parse(ss[0]);
			int b=int.Parse(ss[1]);
			string s1=Console.ReadLine();
			char[]s=s1.ToCharArray();
			for(int i=0;i<b;i++)
			{
				List<int>toswap=new List<int>();
				for(int j=0;j<s.Length-1;j++)
				{
					if(s[j]=='G' && s[j+1]=='L')
					{
						toswap.Add(j);
					}
				}
				for(int j=0;j<toswap.Count;j++)
				{	
					char temp=s[toswap[j]];
					s[toswap[j]]=s[toswap[j]+1];
					s[toswap[j]+1]=temp;
				} 	
				
			}
			Console.WriteLine(a);
			Console.WriteLine(new string(s));
		}
	}
}