CodeForces 266B Queue at the School
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
5 1
BGGBG
output
Copy
GBGGB
input
Copy
5 2
BGGBG
output
Copy
GGBGB
input
Copy
4 1
GGGB
output
Copy
GGGB
题目大意:有n个人在排队买东西,遵循着女士优先的原则,男生决定每过一个时间单位就会让自己身后的女生向前移一位。
思路:模拟过程,我们可以将其想象为冒泡排序,只不过排序的次数变成的p次,将男生看为1,女生看为2,每当出现12时,变成21循环且加1,然后就可以了
代码:
/*
*/
#include<map>
#include<set>
#include <vector>
#include<stack>
#include<queue>
#include<cmath>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll unsigned long long
#define inf 0x3f3f3f
#define esp 1e-8
#define bug {printf("mmp\n");}
#define mm(a,b) memset(a,b,sizeof(a))
#define T() int test,q=1;scanf("%d",&test); while(test--)
const int maxn=1e4+10;
const double pi=acos(-1.0);
const int N=201;
const int mod=1e9+7;
char s[maxn];
int a[N];
int main()
{
int n,p;
cin>>n>>p;
scanf("%s",s);
for(int i=0; i<n; i++)
{
if(s[i]=='B')
a[i]=1;
if(s[i]=='G')
a[i]=2;
}
for(int j=0; j<p; j++)
{
for(int i=0; i<n; i++)
{
if(a[i]<a[i+1])
{
swap(a[i],a[i+1]);
i++;
}
}
}
for(int i=0; i<n; i++)
{
if(a[i]==1)
printf("B");
else
printf("G");
}
printf("\n");
return 0;
}
还没有评论,来说两句吧...