C. Songs Compression(贪心)

本文介绍了一种通过压缩部分歌曲来使所有歌曲都能存入容量有限的优盘中的算法实现。利用贪心策略,该算法将歌曲按压缩前后大小差值进行排序,选择压缩那些能最大程度减少总大小的歌曲。

C. Songs Compression
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Ivan has
n
songs on his phone. The size of the
i
-th song is
a
i
bytes. Ivan also has a flash drive which can hold at most
m
bytes in total. Initially, his flash drive is empty.

Ivan wants to copy all
n
songs to the flash drive. He can compress the songs. If he compresses the
i
-th song, the size of the
i
-th song reduces from
a
i
to
b
i
bytes (
b
i
<
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
m
. 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
m
).

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
n
and
m
(
1

n

10
5
,
1

m

10
9
) — the number of the songs on Ivan’s phone and the capacity of Ivan’s flash drive.

The next
n
lines contain two integers each: the
i
-th line contains two integers
a
i
and
b
i
(
1

a
i
,
b
i

10
9
,
a
i
>
b
i
) — the initial size of the
i
-th song and the size of the
i
-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
inputCopy
4 21
10 8
7 4
3 1
5 4
outputCopy
2
inputCopy
4 16
10 8
7 4
3 1
5 4
outputCopy
-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

21
. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal
8
+
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
>
21
).

In the second example even if Ivan compresses all the songs the sum of sizes will be equal
8
+
4
+
1
+

4

17
>
16
.题意:要往优盘里存歌曲,给一首歌,给了他被压缩前的占地面积,给了压缩后的占地面积,让我们求所有歌曲都存下来至少需要压缩几首歌曲;
思路:贪心,用个结构体存下歌曲被压缩前,压缩后,压缩前后的体积变化,先把未压缩之前的体积都加起来,然后把压缩前后的体积变化从大到小排好序,每次都选最大的那个体积差,减,每减一个,被压缩的歌曲的数量加一,直到所有的体积和<=m了

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    long long int b,s,c;//Int类型会出错;
}a[100001];
int cmp(node x,node y)
{
    return x.c>y.c;
}
int main()
{
    int n;
    long long int m,sum1=0,sum2=0;
    scanf("%d%lld",&n,&m);
    for(int i=0;i<n;i++)
    {
        scanf("%lld%lld",&a[i].b,&a[i].s);
        a[i].c=a[i].b-a[i].s;
        sum1+=a[i].b;
        sum2+=a[i].s;
    }
    sort(a,a+n,cmp);
    if(sum2>m)printf("-1\n");
    else
    {
        int i=0;//最好从0开始存,这样就算没有压缩的歌曲,直接输出i,不用另外判断;
        while(sum1>m)
        {
            sum1-=a[i].c;
            i++;
        }
        printf("%d\n",i);
    }
    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、付费专栏及课程。

余额充值