牛客网公司真题(笔试)系列
2024/3/24
美团2021校招笔试-编程题(通用编程试题,第10场)
目录
一. 题目
二.思路
将1人桌和0人桌分别建立一个小顶堆,
遍历每个等待的人,
如果是男生,优先1人桌小顶堆;
如果是女生,优先0人桌小顶堆;
二. 自测通过,但提交超时
import java.util.Scanner;
import java.util.*;
//有一个坑是:输出桌号是1到n, 而不是0到n-1
//第二个坑是:0人桌占上一个之后,得把这个桌加入1人桌优先队列里
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt(); //有多少组数据
for(int i = 0; i < t; i++){
int n = in.nextInt(); //餐桌数
int[] nums = new int[n]; //餐桌人数情况
String str = in.next();
for(int j = 0; j < n; j++){
nums[j] = str.charAt(j) - '0';
}
int m = in.nextInt(); //等待人数
String str1 = in.next(); //等待男女情况
char[] chars = new char[m];
for(int k = 0; k < m; k++){
chars[k] = str1.charAt(k);
}
int[] result = test(n, nums, m, chars);
for(int o = 0; o < m; o++){
System.out.println(result[o]);
}
}
}
public static int[] test(int n, int[] nums, int m, char[] chars){
int[] res = new int[m];
//建两个队列是因为,2人桌已经满了,就没有可坐的位置了
PriorityQueue<Integer> zeroTable = new PriorityQueue<>();
PriorityQueue<Integer> oneTable = new PriorityQueue<>();
//形成两个优先队列
for(int i = 0; i < n; i++){