CF1015C.Songs Compression

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Ivan has nn songs on his phone. The size of the ii-th song is aia_i bytes. Ivan also has a flash drive which can hold at most mm bytes in total. Initially, his flash drive is empty.

Ivan wants to copy all nn songs to the flash drive. He can compress the songs. If he compresses the ii-th song, the size of the ii-th song reduces from aia_i to bib_i bytes (bi<aib_i \lt a_i).

Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most mm. He can compress any subset of the songs (not necessarily contiguous).

Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to mm).

If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress.

伊万的手机上有 nn 首歌曲。第 ii 首歌曲的大小为 aia_i 字节。伊万还有一个最多可存储 mm 字节数据的 U 盘,初始时该 U 盘为空。

伊万希望将全部 nn 首歌曲复制到该 U 盘上。他可以对歌曲进行压缩:若压缩第 ii 首歌曲,则其大小由 aia_i 字节减少为 bib_i 字节(其中 bi<aib_i < a_i)。

伊万可以任选一个子集(可能为空)的歌曲进行压缩,并在所有歌曲总大小不超过 mm 字节的前提下,将全部歌曲复制到 U 盘上。注意:被压缩的歌曲不必在原序列中连续。

伊万希望找出使得所有歌曲都能存入 U 盘(即总大小 ≤m\leq m)所需的最少压缩歌曲数量。

如果即使将所有歌曲都压缩也无法全部存入 U 盘(即所有 bib_i 之和仍 >m> m),则输出 -1;否则输出伊万需要压缩的最少歌曲数量。

输入格式

The first line of the input contains two integers nn and mm (1≤n≤105,1≤m≤1091 \le n \le 10^5, 1 \le m \le 10^9) — the number of the songs on Ivan's phone and the capacity of Ivan's flash drive.

The next nn lines contain two integers each: the ii-th line contains two integers aia_i and bib_i (1≤ai,bi≤1091 \le a_i, b_i \le 10^9, ai>bia_i \gt b_i) — the initial size of the ii-th song and the size of the ii-th song after compression.

输入的第一行包含两个整数 nn 和 mm(1≤n≤1051 \le n \le 10^5,1≤m≤1091 \le m \le 10^9)——分别表示 Ivan 手机中歌曲的数量以及 Ivan 闪存盘的容量。

接下来的 nn 行每行包含两个整数:第 ii 行包含两个整数 aia_i 和 bib_i(1≤ai,bi≤1091 \le a_i, b_i \le 10^9,ai>bia_i \gt b_i)——分别表示第 ii 首歌曲的原始大小以及压缩后的大小。

输出格式

If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress.

如果无法通过压缩歌曲子集的方式使所有歌曲都能存入该闪存盘,则输出 -1。否则,输出需要压缩的最少歌曲数量。

输入输出样例

  • 输入#1

    4 21
    10 8
    7 4
    3 1
    5 4

    输出#1

    2
  • 输入#2

    4 16
    10 8
    7 4
    3 1
    5 4

    输出#2

    -1

说明/提示

In the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to 8+7+1+5=21≤218 + 7 + 1 + 5 = 21 \le 21. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal 8+4+3+5=20≤218 + 4 + 3 + 5 = 20 \le 21. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to 10+4+3+5=22>2110 + 4 + 3 + 5 = 22 \gt 21).

In the second example even if Ivan compresses all the songs the sum of sizes will be equal 8+4+1+4=17>168 + 4 + 1 + 4 = 17 \gt 16.

在第一个例子中,伊万可以压缩第一首和第三首歌曲,这样操作后,歌曲大小的总和为 8+7+1+5=21≤218 + 7 + 1 + 5 = 21 \le 21。此外,伊万也可以压缩第一首和第二首歌曲,此时大小总和为 8+4+3+5=20≤218 + 4 + 3 + 5 = 20 \le 21。注意,仅压缩任意一首歌曲不足以将所有歌曲复制到闪存盘上(例如,仅压缩第二首歌曲后,大小总和为 10+4+3+5=22>2110 + 4 + 3 + 5 = 22 \gt 21)。

在第二个例子中,即使伊万压缩了所有歌曲,大小总和仍为 8+4+1+4=17>168 + 4 + 1 + 4 = 17 \gt 16。

输入解题思路,AI测评打分。不知道怎么写?

首页