C. Songs Compression

本文介绍了一个关于文件存储的问题,即如何通过压缩部分歌曲来优化存储空间的使用,以确保所有歌曲能够被成功存储在一个给定容量的闪存盘中。讨论了算法实现思路,包括数据结构的设计、排序策略及关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Ivan has n

songs on his phone. The size of the i-th song is ai 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 ai to bi bytes (bi<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 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≤105,1≤m≤109

) — 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 ai and bi (1≤ai,bi≤109, ai>bi) — 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

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

.

大概题意:

就是输入n和m

m为存储空间

然后n行a和b   a大于b

每一行的a可以压缩为b的大小

求最少只需要压缩几个,使得m可以存下所有数据

如果全部压缩都不满足,就输出-1

 

思路:

定义个结构体,存下a,b和压缩节约的大小,即为a和b的差值c

算出最大的和sum2,和最小的和sum1

如果sum1也大于m,就输出-1

再用sort根据差值c从大到小排序

让sum2依次根据c从大到小减

直到sum2小于等于m即可

输出减去的个数即为压缩的次数

 

#include<bits/stdc++.h>
using namespace std;
#define ll long long
struct no{
    ll a,b,c;
}x[100009];
bool cmp(no x1,no x2)
{
    return x1.c>x2.c;
}
int main()
{
    int n,i;
    ll m;
    while(scanf("%d%lld",&n,&m)!=EOF)
    {
        ll  sum1=0,sum2=0;
        ll t=0;
        for(i=1;i<=n;i++)
        {
            cin>>x[i].a>>x[i].b;
            sum1+=x[i].b;
            sum2+=x[i].a;
            x[i].c=x[i].a-x[i].b;
        }
        sort(x+1,x+1+n,cmp);
        if(sum1>m)cout<<"-1"<<endl;
        else if(sum2<=m)cout<<"0"<<endl;
        else
        {
            for(i=1;i<=n;i++)
            {
                sum2-=x[i].c;
                ++t;
                if(sum2<=m)
                    break;
            }
            cout<<t<<endl;
        }
    }
    return 0;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

居贝比

如有帮助,打个赏,恰个饭~

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

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

打赏作者

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

抵扣说明:

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

余额充值