9.6 去哪儿笔试 开发

第一题:字符数组字典序 100%

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(reader.readLine());
        String[] as = reader.readLine().split(" ");

        Arrays.sort(as, (s1, s2) -> (s1 + s2).compareTo(s2 + s1));

        for (String str : as) {
            System.out.printf("%s ", str);
        }
    }
}

第二题:二分 + 贪心 100%

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] inputs = reader.readLine().split(" ");
        int n = Integer.parseInt(inputs[0]);
        long m = Integer.parseInt(inputs[1]);
        int[] a = new int[n];
        String[] as = reader.readLine().split(" ");
        for (int i = 0; i < n; ++ i) a[i] = Integer.parseInt(as[i]);
        int[] b = new int[n];
        String[] bs = reader.readLine().split(" ");
        for (int i = 0; i < n; ++ i) b[i] = Integer.parseInt(bs[i]);

        int l = 0, r = n - 1;
        while (l <= r) {
            int mid = l + (r - l) / 2;

            if (check(mid, a, b, m)) {
                r = mid - 1;
            }
            else l = mid + 1;
        }

        System.out.println(l >= n ? -1 : (l + 1));
    }

    private static boolean check(int k, int[] a, int[] b, long limit) {
        int[] a_copy = Arrays.copyOfRange(a, 0, k + 1);
        Arrays.sort(a_copy);
        int[] b_copy = Arrays.copyOfRange(b, 0, k + 1);
        Arrays.sort(b_copy);

        long t = 0L;
        for (int i = k; i >= 0; -- i) {
            t += (long) a_copy[i] * b_copy[i];
            if (t >= limit) return true;
        }
        return false;
    }
}

第三题 没看

#去哪儿旅行秋招#
全部评论

相关推荐

2 7 评论
分享
牛客网
牛客企业服务