CodeForces 266B Queue at the School

傷城~ 2022-02-24 04:20 262阅读 0赞

Queue at the School

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

During the break the schoolchildren, boys and girls, formed a queue of n people in the canteen. Initially the children stood in the order they entered the canteen. However, after a while the boys started feeling awkward for standing in front of the girls in the queue and they started letting the girls move forward each second.

Let’s describe the process more precisely. Let’s say that the positions in the queue are sequentially numbered by integers from 1 to n, at that the person in the position number 1 is served first. Then, if at time x a boy stands on the i-th position and a girl stands on the (i + 1)-th position, then at time x + 1 the i-th position will have a girl and the (i + 1)-th position will have a boy. The time is given in seconds.

You’ve got the initial position of the children, at the initial moment of time. Determine the way the queue is going to look after t seconds.

Input

The first line contains two integers n and t (1 ≤ n, t ≤ 50), which represent the number of children in the queue and the time after which the queue will transform into the arrangement you need to find.

The next line contains string s, which represents the schoolchildren’s initial arrangement. If the i-th position in the queue contains a boy, then the i-th character of string s equals “B”, otherwise the i-th character equals “G”.

Output

Print string a, which describes the arrangement after t seconds. If the i-th position has a boy after the needed time, then the i-th character a must equal “B”, otherwise it must equal “G”.

Examples

input

Copy

  1. 5 1
  2. BGGBG

output

Copy

  1. GBGGB

input

Copy

  1. 5 2
  2. BGGBG

output

Copy

  1. GGBGB

input

Copy

  1. 4 1
  2. GGGB

output

Copy

  1. GGGB
  2. 题目大意:有n个人在排队买东西,遵循着女士优先的原则,男生决定每过一个时间单位就会让自己身后的女生向前移一位。

思路:模拟过程,我们可以将其想象为冒泡排序,只不过排序的次数变成的p次,将男生看为1,女生看为2,每当出现12时,变成21循环且加1,然后就可以了

代码:

  1. /*
  2. */
  3. #include<map>
  4. #include<set>
  5. #include <vector>
  6. #include<stack>
  7. #include<queue>
  8. #include<cmath>
  9. #include<string>
  10. #include<cstdio>
  11. #include<cstring>
  12. #include<cstdlib>
  13. #include<iostream>
  14. #include<algorithm>
  15. using namespace std;
  16. #define ll unsigned long long
  17. #define inf 0x3f3f3f
  18. #define esp 1e-8
  19. #define bug {printf("mmp\n");}
  20. #define mm(a,b) memset(a,b,sizeof(a))
  21. #define T() int test,q=1;scanf("%d",&test); while(test--)
  22. const int maxn=1e4+10;
  23. const double pi=acos(-1.0);
  24. const int N=201;
  25. const int mod=1e9+7;
  26. char s[maxn];
  27. int a[N];
  28. int main()
  29. {
  30. int n,p;
  31. cin>>n>>p;
  32. scanf("%s",s);
  33. for(int i=0; i<n; i++)
  34. {
  35. if(s[i]=='B')
  36. a[i]=1;
  37. if(s[i]=='G')
  38. a[i]=2;
  39. }
  40. for(int j=0; j<p; j++)
  41. {
  42. for(int i=0; i<n; i++)
  43. {
  44. if(a[i]<a[i+1])
  45. {
  46. swap(a[i],a[i+1]);
  47. i++;
  48. }
  49. }
  50. }
  51. for(int i=0; i<n; i++)
  52. {
  53. if(a[i]==1)
  54. printf("B");
  55. else
  56. printf("G");
  57. }
  58. printf("\n");
  59. return 0;
  60. }

发表评论

表情:
评论列表 (有 0 条评论,262人围观)

还没有评论,来说两句吧...

相关阅读