技术标签: 算法 c++ codeforces 每日一题 数据结构
B. Queue at the School
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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".
译文
学校排队
时间限制:2 秒
内存限制:256 MB
输入:标准输入
输出:标准输出
课间休息时,男女学生在食堂排起了 n 人的长队。起初,他们按照进入食堂的顺序站好。然而,过了一会儿,男生开始觉得站在女生前面很尴尬,于是他们开始让女生每秒钟向前移动一次。
让我们更精确地描述一下这个过程。假设队列中的位置按 1 到 n 的整数顺序编号,1 号位置上的人先得到服务。那么,如果在第 x 秒时,第 i 个位置上站着一个男孩,第 (i + 1) 个位置上站着一个女孩,那么在第 x + 1 秒时,第 i 个位置上将站着一个女孩,第 (i + 1) 个位置上将站着一个男孩。
你已经得到了学生们在初始时刻的初始位置。确定 t 秒后队列的样子。
输入
第一行包含两个整数 n 和 t(1 ≤ n,t ≤ 50),分别表示队列中的人数和队列转变为所需排列的时间。
下一行包含字符串 s,它代表学生的初始排列。如果队列中的第 i 个位置是男孩,则字符串 s 的对应第 i 个字符等于“B”,否则第 i 个字符等于“G”。
输出
打印字符串 a,表示 t 秒后队列的样子。如果第 i 个位置在 t 秒后是一个男孩,则第 i 个字符必须为 "B",否则为 "G"。
样例
输入
5 1
BGGBG
输出
GBGGB
输入
5 2
BGGBG
输出
GGBGB
输入
4 1
GGGB
输出
GGGB
题解
#include <bits/stdc++.h>
using namespace std;
int n, t;
string s;
int main()
{
cin >> n >> t >> s;
// 需要同时将当前这一秒的所有BG全部改为GB
while (t--)
{
for (int i = 0; i < s.size();)
{
if (s[i] == 'B' && s[i + 1] == 'G')
swap(s[i], s[i + 1]), i += 2;
else
i += 1;
}
}
cout << s;
return 0;
}
文章浏览阅读2.4k次。一、实验原理1、Huffman编码Huffman Coding(哈夫曼编码)是一种无失真的编码方式,是可变字长编码(VLC)的一种。Huffman编码基于信源的概率统计模型,它的基本思路是:出现概率大的信源符号编长码,出现概率小的信源符号编短码,从而使平均码长最小。在程序实现时常使用一种叫树的数据结构实现Huffman编码,由它编出的码是即时码。2、Huffman编码方法_哈夫曼编码的压缩效率与
文章浏览阅读522次。我的名字是瑞恩·里德(Ryan Reid),我目前是大学学习3D图形和设计的高级学生。 我从小就开始画画,我父亲在七岁时向我介绍了3D软件,自从我一直在学习和练习我的手艺。 我会显示一些我创建Mondus的过程 。在我进入Mondus建模之前,我想找到CG社区没有挖掘的东西。 我首先在ZBrush中做了一些头脑风暴和概念,然后选择创造一个非洲男性,有着色(也称为白化病)。 我做了一些关于它是如何造成的研究,它基本上是一个遗传的疾病,其特征是很少或没有黑色素生产。 我遇到一个名叫Deejay Jewell的_建模师大神
文章浏览阅读4.8k次,点赞2次,收藏9次。基于spring框架 实现的奇门云网关 WMS端 通用API方法:package org.tempuri;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.StringReader;import java.io.Strin..._奇门wms struct
文章浏览阅读471次。原生应用开发框架React-Nativefacebook 推出的使用react,js开发的原生框架react官网Weex阿里巴巴开源的使用vue开发的原生框架weex官网Flutterdart语言,类似es6,如果对面向对象有深入理解的话,很快上手,google公司推出的Flutter官网React-Native基于react优雅的架构设计支持热重..._pc原生应用开发技术
文章浏览阅读9.7k次,点赞2次,收藏8次。在上一篇文章里面的官方demo建立索引方式使用的是最基本的索引,这里在介绍两种基础索引更多索引类别1--更快的检索IndexIVFFlat官方介绍:为了加快搜索速度,可以将数据集分割成几部分。我们在d维空间中定义Voronoi单元格,并且每个数据库矢量都落入其中一个单元格中。在搜索时,只有查询x所在单元中包含的数据库向量y与少数几个相邻查询向量进行比较。(划分搜索空间)这是通过Inde..._indexivfflat
文章浏览阅读1.7w次,点赞5次,收藏14次。IAR注释中文显示乱码,在Tools&gt;&gt;Options&gt;&gt;Editor&gt;&gt;Default character 选择正确的编码比如UTF-8,然后关闭文件,再次打开。_iar中文乱码
文章浏览阅读363次,点赞7次,收藏8次。【代码】Linux中禁用ctrl alt del快捷键重启。_linx 关闭 ctrl alt del
文章浏览阅读4.7k次,点赞2次,收藏7次。需求:项目中有高度、宽度自适应需求,需要适应不同分辨率的高度及宽度,在不同分辨率下效果区别不会很大html代码如下:<template> <div id="home"> <div class="head" > <v-head></v-head> </div&..._vue3 dom元素获取距离浏览器工具栏的高度
文章浏览阅读6.8k次,点赞2次,收藏6次。以前在Spring框架中使用Spring Security需要我们进行大量的XML配置,但是,Spring Boot在这里依然有惊喜带给我们,我们今天就一起来看看。 毫无疑问,Spring Boot针对Spring Security也提供了自动配置的功能,这些默认的自动配置极大的简化了我们的开发工作,我们今天就来看看这个吧。创建Project并添加相关依赖 配置applicat..._spring-security配置允许访问ip
文章浏览阅读737次,点赞9次,收藏8次。检测到hyper-v 时,勾选自动安装。_vm17pro密钥许可证
文章浏览阅读1.5k次。修改/etc/my.cnfcharacter-set-server=utf8[client]default-character-set=utf8注释掉 sql_mode重新新建mysql表在hive的元数据库中执行以下SQL语句,然后重新创建刚才的表即可 。alter table COLUMNS_V2 modify column COMMENT varchar(256) character set utf8;alter table TABLE_PARAMS modify column PAR_sqoop --default-character-set=utf8
文章浏览阅读845次。一、简介 在用python写脚本时,通常需要处理xml格式的文件或字符串。由于json格式处理的方便性, 我们可将其转为json格式进行处理。 二、步骤 1、安装工具包xmltodict 在命令行输入:pip install xmltodict 2、在代码使用xmltodict.parse(xml_str)进行处理 如 def load_json(xml_path): #获取xml文件 xml_f..._python xml 转json 格式