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


前言

在这里插入图片描述


整体评价

很久没做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包装
    }

}


写在最后

在这里插入图片描述

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值