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

C Messenger in MAC 

题目大意:

 

思路解析:

        答案计算为  \Sigma a + \Sigma \left | b_{i}-{b_{i-1}} \right | , 可以发现当所选的几个信息固定后,其实后面的一项就变为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++];
        }
    }


}




  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Studying~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值