Coderforces Queue at the School

本文提供了一个关于CodeForces平台上的题目266/B的Java实现方案。该方案通过模拟队列中人物的位置交换来解决特定问题。文章详细展示了如何使用数组和布尔标志来跟踪并实现队列内字符位置的互换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


http://codeforces.com/problemset/problem/266/B




import java.util.Scanner;

public class Queue_at_the_School {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int t = scanner.nextInt();
		char[] ch = scanner.next().toCharArray();
		boolean[] a = new boolean[ch.length];
		initial(a);
		//控制秒数
		for(int i = 0; i < t; i++) {
			//遍历一遍
			for(int j = 1; j < ch.length; j++) {
				if(ch[j] == 'G' && ch[j-1] == 'B' && a[j] && a[j-1]) {
					swap(ch, j, j-1);
					a[j] = false;
					a[j-1] = false;
				}
			}
			initial(a);
		}
		System.out.println(String.valueOf(ch));
	}
	public static void swap(char[] ch, int a, int b) {
		char temp = ch[a];
		ch[a] = ch[b];
		ch[b] = temp;
	}
	public static void initial(boolean[] a) {
		int n = a.length;
		for(int i = 0; i < n; i++ ) {
			a[i] = true;
		}
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值