C. Songs Compression

本文介绍了一个关于文件压缩的问题,即如何通过压缩部分歌曲文件来确保所有歌曲能够在容量有限的闪存盘上存储。文章详细解释了问题背景、输入输出格式,并提供了一段C++代码作为解决方案。

C. Songs Compression

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Ivan has nn songs on his phone. The size of the ii-th song is aiai 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 aiai to bibi bytes (bi<aibi<ai).

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.

Input

The first line of the input contains two integers nn and mm (1≤n≤105,1≤m≤1091≤n≤105,1≤m≤109) — 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 aiai and bibi (1≤ai,bi≤1091≤ai,bi≤109, ai>biai>bi) — the initial size of the ii-th song and the size of the ii-th song after compression.

Output

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.

Examples

input

Copy

4 21
10 8
7 4
3 1
5 4

output

Copy

2

input

Copy

4 16
10 8
7 4
3 1
5 4

output

Copy

-1

Note

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≤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≤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>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>16.

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int main()
{
    int n,m;
    int ans=0;
    long long sum1=0;
    long long sum2=0;
    int a[100010];
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;++i)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        sum1+=x;
        sum2+=y;
        a[i]=x-y;
    }
    if(sum2>m) { cout<<-1<<endl;return 0; }
    sort(a,a+n);
    for(int i=n-1;i>=0;--i)
    {
        if(sum1<=m) break;
        else {
            ans++;
            sum1-=a[i];
        }
    }
    cout<<ans<<endl;
    return 0;
}

 

<think>好的,我需要帮助用户解决如何下载和使用System.IO.Compression命名空间的相关库。首先,用户提到他们想下载这个库或文档,并询问如何在.NET中使用。我需要根据提供的引用内容来整理回答。 首先,查看用户提供的引用。引用[1]和[2]提到System.IO.Compression.NET的原生命名空间,用于处理ZIP文件。这意味着在大多数情况下,不需要额外下载,因为它已经包含在.NET框架中。不过,用户可能遇到了问题,比如DLL文件缺失,如引用[3]和[4]所述的情况,这时候需要手动下载DLL文件并放置到指定位置。 接下来,用户的问题是如何下载这个库。由于System.IO.Compression.NET的一部分,对于较新的项目(如.NET Core 2.0及以上或.NET 5+),可能已经默认包含。但如果使用的是旧版本,或者项目文件没有包含必要的引用,可能需要通过NuGet安装。例如,System.IO.Compression.ZipFile可能需要在旧项目中通过NuGet添加。 然后,我需要分步骤说明使用方法。第一步是确认项目类型,如果是.NET Framework 4.5+或.NET Core,应该已经内置。第二步是添加NuGet包,比如System.IO.Compression.ZipFile。第三步是编写代码示例,展示如何压缩和解压文件,引用用户提供的代码示例可能需要调整,但用户没有给出具体的C#代码,可能需要自己构造。例如,使用ZipFile.CreateFromDirectory和ZipFile.ExtractToDirectory方法。 另外,用户可能遇到的常见问题,如DLL缺失,需要根据引用[3]和[4]提供解决方案,即手动下载DLL并放到应用程序目录。同时,需要提醒用户注意32位和64位的兼容性问题。 最后,生成相关问题,比如如何压缩文件夹,如何处理异常,或者如何设置压缩级别等,这些都是用户可能感兴趣的后续问题。 需要确保回答结构清晰,步骤明确,并包含代码示例。同时,引用正确的来源,比如引用[1]和[2]说明原生支持,引用[3]和[4]解决DLL缺失问题。还要注意使用正确的LaTeX格式,虽然问题中可能涉及较少数学公式,但如果有变量或代码块需要正确标注。</think>以下是关于在.NET中使用$System.IO.Compression$命名空间的相关指南: --- ### 1. **如何获取System.IO.Compression库** - **原生支持**:在.NET Framework 4.5+、.NET Core 2.0+ 或 .NET 5/6/7 中,$System.IO.Compression$命名空间已默认集成,无需额外下载[^1][^2]。 - **手动添加**:若项目未自动引用,可通过NuGet安装: ```bash Install-Package System.IO.Compression.ZipFile # 用于操作ZIP文件 Install-Package System.IO.Compression # 基础压缩功能 ``` --- ### 2. **基础使用示例** #### **压缩文件/文件夹** ```csharp using System.IO.Compression; // 压缩文件夹 ZipFile.CreateFromDirectory(@"C:\source_folder", @"C:\output.zip"); // 添加单个文件到ZIP using (ZipArchive zip = ZipFile.Open(@"C:\output.zip", ZipArchiveMode.Update)) { zip.CreateEntryFromFile(@"C:\file.txt", "file.txt"); } ``` #### **解压文件** ```csharp ZipFile.ExtractToDirectory(@"C:\output.zip", @"C:\extracted_folder"); ``` --- ### 3. **常见问题解决** - **DLL缺失问题**:若报错缺少$System.IO.Compression.ZipFile.dll$或$System.IO.Compression.FileSystem.dll$,需手动下载对应版本DLL并放置到应用程序目录[^3][^4]。 - **兼容性**:确保DLL版本与应用程序的32/64位架构一致。 --- ### 4. **文档资源** - **官方文档**:参考[Microsoft Learn - System.IO.Compression](https://learn.microsoft.com/en-us/dotnet/api/system.io.compression)了解类与方法细节。 - **代码示例**:通过.NET官方GitHub仓库获取更多用例。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值