Codeforces Round 932 (Div. 2) --- C. Messenger in MAC --- 题解

C Messenger in MAC

题目大意:

思路解析:

答案计算为 , 可以发现当所选的几个信息固定后,其实后面的一项就变为b_max - b_min,得到了这个结论之后,其实我们可以直接把整个信息按照b进行排序,枚举l,r,那么我最多能选的信息的限制就变为了 a的和 <= L - (br -bl),

因为我们需要选择最多的信息,所以我们在l-r中选择尽量a较小的信息。但是我们可以把选择较小,转为当我们在还能选择时就把当前的选择,当超过时,就不选择当前已经选择中最大的信息。

这部分代码:(这里set可以使用任意排序的数据结构) 每次选择的东西大于限制时,就弹出已经选择了的最大信息

代码实现:

复制代码
import java.io.*;
import java.util.*;

import static java.lang.String.*;


public class Main {
    static int MAXN = 100005;
    static int n;
    static int mod = 1000000;
    static long INF = (long) 1e18;


    public static void main(String[] args) throws IOException {
        FastScanner f = new FastScanner();
        PrintWriter w = new PrintWriter(System.out);
        int t = f.nextInt();
        for (int o = 0; o < t; o++) {
            int n = f.nextInt();
            int l = f.nextInt();
            int[][]  a = new int[n][2];
            for (int i = 0; i < n; i++) {
                a[i][0] = f.nextInt();
                a[i][1] = f.nextInt();
            }
            Arrays.sort(a, ((o1, o2) -> {
                return o1[1] - o2[1];
            }));
            int max = 0;

            for (int i = 0; i < n; i++) {
                PriorityQueue<Integer> set = new PriorityQueue<>(new Comparator<Integer>() {
                    @Override
                    public int compare(Integer o1, Integer o2) {
                        return o2 - o1;
                    }
                });
                long cur = 0;

                for (int j = i; j < n; j++) {
                    if (a[j][1] - a[i][1] > l) break;
                    cur += a[j][0];
                    set.add(a[j][0]);
                    while(a[j][1] - a[i][1] + cur > l){
                        int num = set.poll();
                        cur -= num;
                    }
                    max = Math.max(max, set.size());
                }

            }
            w.println(max);
        }
        w.flush();
        w.close();
    }

    private static class FastScanner {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        private FastScanner() throws IOException {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        private short nextShort() throws IOException {
            short ret = 0;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do ret = (short) (ret * 10 + c - '0');
            while ((c = read()) >= '0' && c <= '9');
            if (neg) return (short) -ret;
            return ret;
        }

        private int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do ret = ret * 10 + c - '0';
            while ((c = read()) >= '0' && c <= '9');
            if (neg) return -ret;
            return ret;
        }

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do ret = ret * 10 + c - '0';
            while ((c = read()) >= '0' && c <= '9');
            if (neg) return -ret;
            return ret;
        }

        private char nextChar() throws IOException {
            byte c = read();
            while (c <= ' ') c = read();
            return (char) c;
        }

        private String nextString() throws IOException {
            StringBuilder ret = new StringBuilder();
            byte c = read();
            while (c <= ' ') c = read();
            do {
                ret.append((char) c);
            } while ((c = read()) > ' ');
            return ret.toString();
        }

        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1) buffer[0] = -1;
        }

        private byte read() throws IOException {
            if (bufferPointer == bytesRead) fillBuffer();
            return buffer[bufferPointer++];
        }
    }


}
相关推荐
岁忧2 分钟前
LeetCode 高频 SQL 50 题(基础版)之 【高级字符串函数 / 正则表达式 / 子句】· 上
sql·算法·leetcode
eachin_z1 小时前
力扣刷题(第四十九天)
算法·leetcode·职场和发展
闻缺陷则喜何志丹1 小时前
【强连通分量 缩点 拓扑排序】P3387 【模板】缩点|普及+
c++·算法·拓扑排序·洛谷·强连通分量·缩点
机器学习之心2 小时前
机器学习用于算法交易(Matlab实现)
算法·机器学习·matlab
江梦寻2 小时前
MacOS下Homebrew国内镜像加速指南(2025最新国内镜像加速)
开发语言·后端·python·macos·架构·策略模式
AL流云。2 小时前
【优选算法】C++滑动窗口
数据结构·c++·算法
*Lisen2 小时前
重新安装解决mac vscode点击不能跳转问题
ide·vscode·macos
qq_429879673 小时前
省略号和可变参数模板
开发语言·c++·算法
飞川撸码4 小时前
【LeetCode 热题100】网格路径类 DP 系列题:不同路径 & 最小路径和(力扣62 / 64 )(Go语言版)
算法·leetcode·golang·动态规划
Neil今天也要学习4 小时前
永磁同步电机参数辨识算法--IPMSM拓展卡尔曼滤波全参数辨识
单片机·嵌入式硬件·算法