Acwing 138 周赛 解题报告 | 珂学家 | 偏序 + DP构造


前言

Acwing 138 周赛 解题报告 | 珂学家 | 偏序 + DP构造_第1张图片


整体评价

很久没做acwing周赛了, 之前vp过一些周赛,感觉风格变了。

这次感觉还可以,都是些眼熟的套路题。


A. 5458. 进水排水问题

思路: 签到题

按题意描述编写

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(new BufferedInputStream(System.in));
        int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(), d = sc.nextInt();
        System.out.println((a + b) < (c + d) ? "YES" : "NO");
    }

}

B. 5459. 区间嵌套

思路: 偏序

经典的俄罗斯套娃模型,典题

主要通过 排序 做文章

  • 一级排序,左端点从小到大
  • 二级排序,右端点从大到小

然后维护最大的右端点区间id即可,不需要treemap维护了

时间复杂度为 O ( n l o g n ) O(nlogn) O(nlogn)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        AReader sc = new AReader();
        int n = sc.nextInt();

        int[][] intervals = new int[n][3];
        for (int i = 0; i < n; i++) {
            intervals[i][0] = sc.nextInt();
            intervals[i][1] = sc.nextInt();
            intervals[i][2] = i;
        }
        Arrays.sort(intervals, (a, b) -> {
            if (a[0] != b[0]) return a[0] < b[0] ? -1 : 1;
            if (a[1] != b[1]) return a[1] > b[1] ? -1 : 1;
            return 0;
        });

        int ansI = -1, ansJ = -1;
        // 偏序关系吗?
        TreeMap<Integer, Integer> tree = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            int right = intervals[i][1];

            Map.Entry<Integer, Integer> ent = tree.ceilingEntry(right);
            if (ent != null) {
                ansI = intervals[i][2] + 1;
                ansJ = ent.getValue() + 1;
                break;
            }
            tree.put(right, intervals[i][2]);
        }

        System.out.println(ansI + " " + ansJ);
    }

    static
    class AReader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");
        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }
        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {
                String nextLine = innerNextLine();
                if (nextLine == null) {
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);
            }
            return true;
        }
        public String nextLine() {
            tokenizer = new StringTokenizer("");
            return innerNextLine();
        }
        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

//        public BigInteger nextBigInt() {
//            return new BigInteger(next());
//        }
        // 若需要nextDouble等方法,请自行调用Double.parseDouble包装
    }

}


C. 5460. 连续整数序列

思路: DP + 回溯数组


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;

public class Main {


    public static void main(String[] args) {
        AReader sc = new AReader();
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        Map<Integer, Integer> hash = new HashMap<>();
        int[] dp = new int[n];
        int[] from = new int[n];
        Arrays.fill(from,  -1);

        for (int i = 0; i < n; i++) {
            int v = arr[i];
            if (hash.containsKey(v - 1)) {
                dp[i] = dp[hash.get(v - 1)] + 1;
                from[i] = hash.get(v - 1);
            } else {
                dp[i] = 1;
                from[i] = -1;
            }
            hash.put(v, i);
        }

        int idx = -1;
        for (int i = 0; i < n; i++) {
            if (idx == -1 || dp[idx] < dp[i]) {
                idx = i;
            }
        }

        System.out.println(dp[idx]);

        List<Integer> lists = new ArrayList<>();
        while (idx != -1) {
            lists.add(idx + 1);
            idx = from[idx];
        }
        Collections.reverse(lists);
        System.out.println(lists.stream().map(String::valueOf).collect(Collectors.joining(" ")));

    }

    static
    class AReader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");
        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }
        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {
                String nextLine = innerNextLine();
                if (nextLine == null) {
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);
            }
            return true;
        }
        public String nextLine() {
            tokenizer = new StringTokenizer("");
            return innerNextLine();
        }
        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

//        public BigInteger nextBigInt() {
//            return new BigInteger(next());
//        }
        // 若需要nextDouble等方法,请自行调用Double.parseDouble包装
    }

}


写在最后

Acwing 138 周赛 解题报告 | 珂学家 | 偏序 + DP构造_第2张图片

你可能感兴趣的:(java,算法,开发语言,力扣,leetcode)