题目描述

Ivan has nn songs on his phone. The size of the ii -th song is a_iai​ 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 a_iai​ to b_ibi​ bytes ( b_i < a_ibi​<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.

输入输出格式

输入格式:

The first line of the input contains two integers nn and mm ( 1 \le n \le 10^5, 1 \le m \le 10^91≤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 a_iai​ and b_ibi​ ( 1 \le a_i, b_i \le 10^91≤ai​,bi​≤109, a_i > b_iai​>bi​ ) — the initial size of the ii -th song and the size of the ii -th song after compression.

输出格式:

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

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 \le 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 \le 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 .


思路

题目大意:有n首歌曲,给定压缩前和压缩后的大小,以及U盘的空间m。现在求最少压缩几首歌能把所有歌放入U盘,如果放不进去输出-1。

贪心,先写2个特判以便节省时间。

1.如果压缩后的全部歌曲$s2$大于m,则说明肯定放不进去,输出-1结束即可;

2.如果未压缩的全部歌曲s1小于等于m,则说明一定能全部放进去,输出0即可。

如果不符合上述情况,则把每个歌曲i**未压缩和压缩后的大小差**从大往小排序,因为我们要选尽可能少费空间(差值大的)的让最优解最优。然后**贪心**,差值大的优先放入U盘。

#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef struct//歌曲结构体
{long long int a,b,dc;//a和b分别是对于歌曲i来说,压缩前的空间和压缩后的空间,dc是它们的差值
}lxydl;
lxydl song[100001];
long long int n,m,s1,s2,s,cnt;//注意要开long long
inline int cmp(lxydl a,lxydl b)//按歌曲压缩空间差值从大往小排序
{return a.dc>b.dc;
}
signed main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);register long long int i;cin>>n>>m;for(i=1;i<=n;i++){cin>>song[i].a>>song[i].b;song[i].dc=song[i].a-song[i].b;//计算歌曲的空间差 s1=s1+song[i].a;//计算未压缩的歌曲空间总和 s2=s2+song[i].b;//计算压缩的歌曲空间总和}if(s2>m)//如果压缩后的歌曲还比m大 {cout<<-1<<endl;return 0;}if(s1<=m)//如果未压缩的歌曲比m小 {cout<<0<<endl;//肯定都能放进去,不用压缩 return 0;}sort(song+1,song+n+1,cmp);//排序 long long int space(s1-m);//计算空间差 for(i=1;i<=n;i++){if(s>=space)//如果空间不够了 {break;}else{s=s+song[i].dc;cnt++;}}cout<<cnt<<endl;return 0;
}

CF1015C Songs Compression (#贪心)相关推荐

  1. cf-#501 div3 C. Songs Compression

    题目链接:http://codeforces.com/contest/1015/problem/C 就是输入n和m,m为存储空间 然后n行a和b   a大于b 每一行的a可以压缩为b的大小 求最少只需 ...

  2. 【贪心】Songs Compression

    17暑假练习赛2 Codeforces Round #501 (Div. 3)  1015C 贪心水题,思路简单 坑点:不开long long会过不去,以后统一用long long做题 失误:考虑到了 ...

  3. UVA - 1346 Songs (贪心+排序)

    题意:已知每首歌的标号,长度和播放频率,求一种播放顺序,使得最小,并且输出该播放顺序下第t首歌的标号. 分析: 1.长度越短,播放频率越大的歌排在前面,上式越小. 2.s(i)表示的是当前播放顺序下这 ...

  4. Songs Compression

    https://codeforces.com/contest/1015/problem/C C++版本一 简单排序 /* *@Author: STZG *@Language: C++ */ #incl ...

  5. CodeForces 1015 C Songs Compression

    [题目]http://codeforces.com/contest/1015/problem/C [大意]硬盘容量为21,有4首歌,前是原来大小,后是压缩大小,问最少压缩多少个能全装下. [代码] # ...

  6. 基础算法 —— 贪心算法

    [概述] 贪心算法是从问题的初始状态出发,通过若干次的贪心选择而得到的最优值的一种求解策略,即贪心策略. 简单来说,贪心策略是一种在每次决策时采取当前意义下最优策略的算法,做出的选择至少在某种约束条件 ...

  7. Codeforces Round #501 (Div. 3)【未完结】

    2022.3.7 题单地址:https://codeforces.com/contest/1015 目录 A. Points in Segments B. Obtaining the String[模 ...

  8. UVa1346 - Songs(贪心算法)

    问题:给出n首歌,已知其长度len和使用频率freq.将这些歌按照一定顺序存到磁带中,要求最小. 思路: 以两个为例,s1s2和s2s1计算为 s1s2顺序时为:f1*l1+f2*(l1+l2) s2 ...

  9. Codeforces Global Round 12 D. Rating Compression 思维 + 贪心

    传送门 题意: 给一个长度为nnn的数组aaa,定义一个数组bbb,且bj=minj<=i<=j+k−1aib_j=min_{j<=i<=j+k-1}a_ibj​=minj&l ...

最新文章

  1. UE5废墟破坏游戏场景创建学习教程
  2. 20211006 线性变换
  3. 在Cloudfoundry上部署RESTful服务
  4. Linux 命令之 pwunconv -- 关闭投影密码
  5. el表达式 循环_EL表达式和JSTL标签库(百战程序员047天)
  6. Android开发技术周报 Issue#70++
  7. 简单的窗体抖动托管c++
  8. 网络-console
  9. mysql数据库实例
  10. java swt 双屏_SWT(JFace)体验之打开多个Form
  11. 2015-5-23PDF的下载链接
  12. 2020-03-31
  13. Java程序二进制转化为十进制_用java程序实现二进制像十进制转化或十进 – 手机爱问...
  14. U8如何月结及反结账
  15. Kubernetes(k8s)四、Pod生命周期(初始化容器的应用,探针liveness、readliness应用,)
  16. 用python画几个东西怎么画_一步一步教你如何用Python画一个滑稽
  17. 计算机硬件之间是怎样联系的,计算机硬件系统和软件系统有没有联系
  18. 给博客添加rss订阅
  19. C#中使用 HttpWebRequest 向网站提交数据
  20. NetSniper网络尖兵:宽带网络运营维护管理器

热门文章

  1. 1小时赚300块,不打代码帮人做个吃鸡网页 [IVX实战第3篇]
  2. 【模板】线段树 2 洛谷P3373
  3. matlab 贪吃的蛇,贪吃的蛇教案
  4. 在NS2(2.35版本)中添加 Ping协议
  5. 在CentOS8.4中安装OpenFOAM
  6. BICEP单元测试计划——四则运算Ⅱ
  7. 谷歌变坏了?Chrome 已成众矢之的:15 篇前端热文回看
  8. 李兴平,世界站长第一人
  9. html的注释是什么意思,注释是什么意思?
  10. T530-I7重装win10