2020.8.2 拼多多提前批笔试复盘

  1. 模拟题
    没注意第N次到的话特判,只能过96%
public class Main { 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k=0,n=0;
        String[] line = null;
        if (sc.hasNextLine()) {
            line = sc.nextLine().trim().split(" ");
            k = Integer.parseInt(line[0]);
            n = Integer.parseInt(line[1]);
        }
        if (sc.hasNextLine()) {
            line = sc.nextLine().trim().split(" ");
        }
        int backNum = 0;
        if (k == 0) {
            System.out.println("paradox");
            return;
        }
        for (String stepStr : line) {
            int step = Integer.parseInt(stepStr);
            if (step == k) {
                System.out.println("paradox");
                return;
            }
            if (step < k) {
                k -= step;
            } else {
                backNum++;
                k = step - k;
            }
        }
        System.out.println(k + " " + backNum);
    }
}
  1. 枚举+并查集
    枚举错了 只过25%
public class Main {
    static HashMap<String, String> map;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        map = new HashMap<>(n);
        while (sc.hasNextLine()) {
            String line = sc.nextLine().trim().replace(" ","");
            if (line.length() == 6)
                map.put(line, line);
        }
        for (String str : map.keySet()) {
            char[] chars = str.toCharArray();
            for (int i = 0; i < 4; i++) {
                char c4 = chars[4], c5 = chars[5];
                chars[4] = chars[2];
                chars[5] = chars[3];
                chars[2] = c5;
                chars[3] = c4;
                String temp = String.valueOf(chars);
                if (map.containsKey(temp)) {
                    union(str, temp);
                }
                char[] chars1 = temp.toCharArray();
                for (int j = 0; j < 4; j++) {
                    char c0 = chars1[0], c1 = chars1[1];
                    chars1[0] = chars1[2];
                    chars1[1] = chars1[3];
                    chars1[2] = c1;
                    chars1[3] = c0;
                    String temp1 = String.valueOf(chars);
                    if (map.containsKey(temp1)) {
                        union(str, temp1);
                    }
                    char[] chars2 = temp1.toCharArray();
                    for (int k = 0; k < 4; k++) {
                        char c41 = chars2[4], c51 = chars2[5];
                        chars2[4] = chars2[0];
                        chars2[5] = chars2[1];
                        chars2[0] = c51;
                        chars2[1] = c41;
                        String temp2 = String.valueOf(chars2);
                        if (map.containsKey(temp2)) {
                            union(str, temp2);
                        }
                    }
                }
            }
            chars = str.toCharArray();
            for (int i = 0; i < 4; i++) {
                char c0 = chars[0], c1 = chars[1];
                chars[0] = chars[2];
                chars[1] = chars[3];
                chars[2] = c1;
                chars[3] = c0;
                String temp = String.valueOf(chars);
                if (map.containsKey(temp)) {
                    union(str, temp);
                }
                char[] chars1 = temp.toCharArray();
                for (int j = 0; j < 4; j++) {
                    char c01 = chars1[4], c11 = chars1[5];
                    chars1[4] = chars1[2];
                    chars1[5] = chars1[3];
                    chars1[2] = c11;
                    chars1[3] = c01;
                    String temp1 = String.valueOf(chars);
                    if (map.containsKey(temp1)) {
                        union(str, temp1);
                    }
                    char[] chars2 = temp1.toCharArray();
                    for (int k = 0; k < 4; k++) {
                        char c4 = chars2[4], c5 = chars2[5];
                        chars2[4] = chars2[0];
                        chars2[5] = chars2[1];
                        chars2[0] = c5;
                        chars2[1] = c4;
                        String temp2 = String.valueOf(chars2);
                        if (map.containsKey(temp2)) {
                            union(str, temp2);
                        }
                    }
                }
            }
            chars = str.toCharArray();
            for (int i = 0; i < 4; i++) {
                char c4 = chars[4], c5 = chars[5];
                chars[4] = chars[0];
                chars[5] = chars[1];
                chars[0] = c5;
                chars[1] = c4;
                String temp = String.valueOf(chars);
                if (map.containsKey(temp)) {
                    union(str, temp);
                }
                char[] chars1 = temp.toCharArray();
                for (int j = 0; j < 4; j++) {
                    char c0 = chars1[4], c1 = chars1[5];
                    chars1[4] = chars1[2];
                    chars1[5] = chars1[3];
                    chars1[2] = c1;
                    chars1[3] = c0;
                    String temp1 = String.valueOf(chars);
                    if (map.containsKey(temp1)) {
                        union(str, temp1);
                    }
                    char[] chars2 = temp1.toCharArray();
                    for (int k = 0; k < 4; k++) {
                        char c41 = chars2[0], c51 = chars2[1];
                        chars2[0] = chars2[2];
                        chars2[1] = chars2[3];
                        chars2[2] = c51;
                        chars2[3] = c41;
                        String temp2 = String.valueOf(chars2);
                        if (map.containsKey(temp2)) {
                            union(str, temp2);
                        }
                    }
                }
            }
        }
        for (String str : map.keySet()) {
            String f = find(str);
            map.put(str, f);
        }
        HashMap<String, Integer> counts = new HashMap<>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            counts.put(entry.getValue(), counts.getOrDefault(entry.getValue(), 0)+1);
        }
        int m = counts.size();
        List<Integer> list = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : counts.entrySet()) {
            list.add(entry.getValue());
        }
        list.sort((a, b) -> b - a);
        System.out.println(m);
        for (int i = 0; i < list.size()-1; i++) {
            System.out.print(list.get(i)+" ");
        }
        System.out.print(list.get(list.size()-1));
    }
    public static void union(String s1, String s2) {
        String f1 = find(s1), f2 = find(s2);
        if (f1.compareTo(s2) <= 0) {
            map.put(s2, f1);
        } else {
            map.put(s1, f2);
        }
    }
    public static String find(String s) {
        String f = map.get(s);
        if (f.equals(s)) {
            return s;
        } else {
            f = find(f);
            map.put(s, f);
            return f;
        }
    }
}

  1. 暴力只能过50%
    但排序+剪枝能过到90%
    最大最小问题 直接用二分做(不会)
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int t = sc.nextInt();
        int[] lunchHeat = new int[n+1];
        int[] lunchDesty = new int[n+1];
        int[] dinnerHeat = new int[m+1];
        int[] dinnerDesty = new int[m+1];
        lunchHeat[0] = 0;
        lunchDesty[0] = 0;
        dinnerHeat[0] = 0;
        dinnerDesty[0] = 0;
        for (int i = 1; i <= n ; i++) {
            lunchHeat[i] = sc.nextInt();
            lunchDesty[i] = sc.nextInt();
        }
        for (int i = 1; i <= m; i++) {
            dinnerHeat[i] = sc.nextInt();
            dinnerDesty[i] = sc.nextInt();
        }
        int minHeat = Integer.MAX_VALUE;
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= m; j++) {
                if (lunchDesty[i] + dinnerDesty[j] >= t) {
                    minHeat = Math.min(minHeat, lunchHeat[i] + dinnerHeat[j]);
                }
            }
        }
        if (minHeat == Integer.MAX_VALUE) minHeat = -1;
        System.out.println(minHeat);
    }
}

4 插头dp 不会做

原文地址:https://www.cnblogs.com/Akarinnnn/p/13423333.html