当前位置: 首页 > 面试经验 >

4.23微众后端笔试

优质
小牛编辑
158浏览
2023-04-23

4.23微众后端笔试

三个大模拟,一直搞不懂t1为什么RE,然后花了很多时间在搞t2,导致t3没时间了,骗了点用例。

82+73+16,估计凉了吧。

T1 前缀和+枚举

预处理nums1, nums2前缀和,枚举左右端点。

import java.util.Scanner;
public class T111 {
    static Scanner in;
    static int n;
    static long[] pre1, pre2;
    static int[] nums1, nums2;
    static int[] limits = new int[4];
    public static void main(String[] args) {
        init();
        int res = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                res += check(i, j) ? 1 : 0;
            }
        }
        System.out.println(res);
    }
    static boolean check(int l, int r) {
        long sum1 = pre1[r+1] - pre1[l], sum2 = pre2[r+1] - pre2[l];
        return sum1 >= limits[0] && sum1 <= limits[1] && sum2 >= limits[2] && sum2 <= limits[3];
    }
    static void init() {
        in = new Scanner(System.in);
        n = in.nextInt();

        nums1 = new int[n];
        nums2 = new int[n];
        for (int i = 0; i < n; i++) {
            nums1[i] = in.nextInt();
        }
        for (int i = 0; i < n; i++) {
            nums2[i] = in.nextInt();
        }
        for (int i = 0; i < 4; i++) {
            limits[i] = in.nextInt();
        }

        pre1 = new long[n+1];
        pre2 = new long[n+1];
        for (int i = 0; i < n; i++) {
            pre1[i+1] = pre1[i] + nums1[i];
            pre2[i+1] = pre2[i] + nums2[i];
        }
    }
}

T2 排序+枚举+小根堆

排序,小根堆维护topM大元素,然后依次枚举。

PS:应该还可以再优化。

import java.util.*;
public class T222 {
    static Scanner in = new Scanner(System.in);
    static int n, m;
    static int[] powers, gains;
    static PriorityQueue<Integer> pq = new PriorityQueue<>(((o1, o2) -> gains[o1]-gains[o2]));
    static Map<Integer, List<Integer>> idx2lessidx = new HashMap<>();  // idx -> 所有 比 powers[idx]小的下标
    static Map<Integer, Integer> val2idx = new HashMap<>();

    public static void main(String[] args) {
        init();
        int[] res = new int[n];

        int[] copy = Arrays.copyOf(powers, n);
        Arrays.sort(copy);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i-1; j++) {
                idx2lessidx.get(i).add(val2idx.get(copy[j]));
            }
        }

        // 贪心,对于每个球,优先选取分数大的球去击败,直至击败完,或者达到 m 上限
        for (int i = 0; i < n; i++) {

            // 按照gain选取top m
            for(int idx : idx2lessidx.get(i)) {
                pq.add(idx);
                if (pq.size() > m) pq.poll();
            }
            for(int x : pq) res[val2idx.get(copy[i])] += gains[x];
            pq.clear();
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(res[i]).append(" ");
        }
        System.out.println(sb.deleteCharAt(sb.length()-1));
    }
    static void init() {
        n = in.nextInt();
        m = in.nextInt();
        in.nextLine();

        powers = new int[n];
        gains = new int[n];
        for (int i = 0; i < n; i++) {
            powers[i] = in.nextInt();
            val2idx.put(powers[i], i);
            idx2lessidx.put(i, new ArrayList<>());
        }
        in.nextLine();

        for (int i = 0; i < n; i++) {
            gains[i] = in.nextInt();
        }
    }
}

T3 模拟

时间不够。没做完。

#微众银行信息集散地##我的实习求职记录##2023毕业生求职有问必答#
 类似资料: