Boys and Girls(CF-253A)
Problem Description
There are n boys and m girls studying in the class. They should stand in a line so that boys and girls alternated there as much as possible. Let's assume that positions in the line are indexed from left to right by numbers from 1 to n + m. Then the number of integers i (1 ≤ i < n + m) such that positions with indexes i and i + 1 contain children of different genders (position i has a girl and position i + 1 has a boy or vice versa) must be as large as possible.
Help the children and tell them how to form the line.
Input
The single line of the input contains two integers n and m (1 ≤ n, m ≤ 100), separated by a space.
Output
Print a line of n + m characters. Print on the i-th position of the line character "B", if the i-th position of your arrangement should have a boy and "G", if it should have a girl.
Of course, the number of characters "B" should equal n and the number of characters "G" should equal m. If there are multiple optimal solutions, print any of them.
Examples
Input
3 3
Output
GBGBGB
Input
4 2
Output
BGBGBB
Note
In the first sample another possible answer is BGBGBG.
In the second sample answer BBGBGB is also optimal.
题意:n 个男孩 m 个女孩排成一行,现在要使得男孩和女孩交错排列的个数最多,输出一种排队方案
思路:设 n、m 中最小的那个为 x,首先可以保证的是,连续输出 x 组 GB 或 BG 能使得交错排列个数尽量多,然后输出剩余的 n+m-x 个 G 或 B,为保证输出的 x 组字符的最后一个字符与剩下的 n+m-x 的第一个字符字符不同,因此需要根据 n、m 的大小关系来判断前 x 组输出的是 GB 还是 BG
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
const int MOD = 1E9+7;
const int N = 200000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int n,m;
scanf("%d%d",&n,&m);
if(n>m){
for(int i=1;i<=m;i++)
printf("BG");
for(int i=1;i<=n-m;i++)
printf("B");
}
else{
for(int i=1;i<=n;i++)
printf("GB");
for(int i=1;i<=m-n;i++)
printf("G");
}
return 0;
}

浙公网安备 33010602011771号