Day 3 | Queue at the school | C++
- Ayush Mahajan
- Aug 28, 2022
- 1 min read
Time for our 3rd question. Today we will try to solve: Queue at the school. https://codeforces.com/contest/266/problem/B… Don't forget to try it yourself first.
What I have in mind when I read the question is to actually swap girls and boys as mentioned in the question.
As a competitive programmer, I have one issue in mind is that making changes mentioned in the question takes O(N) times, where N is the length of the string. If we run this for T (number of seconds) times, then it is O(N*T) times.
But if we look at constraints, we see that N*T = 50*50 = 2500 As a rule of thumb, I assume that I can do 100,000,000 operations per second. From this assumption, my code easily runs in under 1 second. Thus, I can move ahead with this approach.
But the question is how do we implement how to do 1 iteration of changes per second mentioned in the question? We can run a for-loop and if a boy is ahead of girl (A[i] == B and A[i+1] == G), then we swap them.
But remember, that if you swap them. Then you won't consider this boy again. So if you swap i and i+1, then you should start checking from i+2 Reason BGG should become GBG But if we go from i to i+1, BGG would become GGB Like here: https://codeforces.com/contest/266/submission/169250731
Again remember, making mistakes is important to learn more things. I learned i and i+2 point by making a mistake only - https://codeforces.com/contest/266/submission/169250701
![Day 44 | Binary Search Practice [Leetcode] | C++](https://static.wixstatic.com/media/fbea2a_167f1b66234f4d30b91bcf5b517f83f3~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_167f1b66234f4d30b91bcf5b517f83f3~mv2.jpg)
![Day 43 | Graph and Tree Practice [Leetcode] II | C++](https://static.wixstatic.com/media/fbea2a_361d6f7cd90c431b9f25518e1503da94~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_361d6f7cd90c431b9f25518e1503da94~mv2.jpg)
![Day 42 | Graph Practice [LeetCode] | C++](https://static.wixstatic.com/media/fbea2a_6570bb28577149f19db06704d542789a~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_6570bb28577149f19db06704d542789a~mv2.jpg)
Comments