当前位置:网站首页>第九届 蓝桥杯 决赛 交换次数
第九届 蓝桥杯 决赛 交换次数
2022-07-07 15:32:00 【@小红花】
问题描述
IT产业人才需求节节攀升。业内巨头百度、阿里巴巴、腾讯(简称BAT)在某海滩进行招聘活动。
招聘部门一字排开。由于是自由抢占席位,三大公司的席位随机交错在一起,形如:
ABABTATT,这使得应聘者十分别扭。
于是,管理部门要求招聘方进行必要的交换位置,使得每个集团的席位都挨在一起。即最后形如:
BBAAATTT 这样的形状,当然,也可能是:
AAABBTTT 等。现在,假设每次只能交换2个席位,并且知道现在的席位分布,
你的任务是计算:要使每个集团的招聘席位都挨在一起需要至少进行多少次交换动作。输入是一行n个字符(只含有字母B、A或T),表示现在的席位分布。
输出是一个整数,表示至少交换次数。比如,输入:
TABTABBTTTT程序应该输出:
3再比如,输入:
TTAAABB程序应该输出:
0我们约定,输入字符串的长度n 不大于10万
资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 1000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
不要使用package语句。不要使用jdk1.7及以上版本的特性。
主类的名字必须是:Main,否则按无效代码处理。
Java
import java.util.Scanner;
public class 交换次数 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char[] ss = scanner.next().toCharArray();
//六种排列组合的方案
char[][] c = {
{'B','A','T'},
{'B','T','A'},
{'A','B','T'},
{'A','T','B'},
{'T','A','B'},
{'T','B','A'}
};
int ans = Integer.MAX_VALUE;
for(int i = 0;i < 6;i++) {
ans = Math.min(ans,fun(ss,c[i][0],c[i][2],c[i][2]));
}
System.out.println(ans);
}
//将整个字符串分为三个区域
public static int fun(char[] ss,char A,char B,char C) {
//A区域中a的个数
int a = 0;
//B区域中b的个数
int b = 0;
//A区域中非a的个数
int Abc = 0;
//A区域中b的个数
int Ab = 0;
//B区域中a的个数
int Ba = 0;
//B区域中c的个数
int Bc = 0;
int len = ss.length;
//求出A和B两个区域占的大小
for(int i = 0;i < len;i++) {
if(ss[i] == A) a++;
if(ss[i] == B) b++;
}
for(int i = 0;i < a;i++) {
if(ss[i] != A) Abc++;
if(ss[i] == B) Ab++;
}
for(int i = a;i < a + b;i++) {
if(ss[i] == A) Ba++;
if(ss[i] == C) Bc++;
}
//最小交换次数是:将A区域非a的交换出去,B区域非b的交换出去
//因为A区域中有b,B区域中有a,他们直接交换就行,再减去重复的交换次数
return Abc + Bc + Ba - Math.min(Ba, Ab);
}
}
边栏推荐
- 预售17.9万,恒驰5能不能火?产品力在线,就看怎么卖
- Record the migration process of a project
- 目标跟踪常见训练数据集格式
- Personal notes of graphics (4)
- PHP realizes wechat applet face recognition and face brushing login function
- A tour of gRPC:03 - proto序列化/反序列化
- Vs2019 configuration matrix library eigen
- three. JS create cool snow effect
- Statistical learning method -- perceptron
- 01tire+ chain forward star +dfs+ greedy exercise one
猜你喜欢

记录Servlet学习时的一次乱码

node:504报错

字节跳动Android面试,知识点总结+面试题解析

最新2022年Android大厂面试经验,安卓View+Handler+Binder

A tour of gRPC:03 - proto序列化/反序列化

Vs2019 configuration matrix library eigen

"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."

应用在温度检测仪中的温度传感芯片

AutoLISP series (3): function function 3

Sort out several important Android knowledge and advanced Android development interview questions
随机推荐
全网“追杀”钟薛高
What is the difference between IP address and physical address
Usage of config in laravel
模拟Servlet的本质
掌握这个提升路径,面试资料分享
Personal notes of graphics (4)
字节跳动高工面试,轻松入门flutter
Statistical learning method -- perceptron
As an Android Developer programmer, Android advanced interview
【DesignMode】代理模式(proxy pattern)
[designmode] template method pattern
【DesignMode】享元模式(Flyweight Pattern)
【DesignMode】模板方法模式(Template method pattern)
URL和URI的关系
编程模式-表驱动编程
二叉搜索树(特性篇)
水平垂直居中 方法 和兼容
低代码(lowcode)帮助运输公司增强供应链管理的4种方式
Inner monologue of accidental promotion
在哪个期货公司开期货户最安全?