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 .
思路:建立一个结构体 ,里面有大数,小数,及二者之差,先进行判断若每个数据的最小数之和小于flsah drive容量,输出-1,否则
以每次压缩后的大小,与flash drive作比较,循环减去差值最大的,输出减的次数
#include<bits/stdc++.h>
using namespace std;
struct A
{
int big;
int small;
int cha;
}a[100008];
int cmp(A c,A d)
{
return c.cha>d.cha;//自定义按照差大的对结构体进行排序
}
int main()
{
int n,m,ans=0;
long long t1=0,t2=0;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].big,&a[i].small);
a[i].cha=a[i].big-a[i].small;
t1+=a[i].small;
t2+=a[i].big;
}
sort(a,a+n,cmp);
int j=0;
if(t1>m)
printf("-1\n");
else
{
while(t2>m)//一每次压缩之后的值与容量m进行比较,小于等于则输出j
{
t2-=a[j].cha;
j++;
}
printf("%d\n",j);
}
return 0;
}
上一篇: 命令行下打包Qt程序
下一篇: 二叉查找树
推荐阅读
-
php文件压缩zlib.output_compression 和 ob_gzhandler,
-
php简单开启gzip压缩方法(zlib.output_compression)
-
php简单开启gzip压缩方法(zlib.output_compression)
-
python爬虫—“爱彼迎”:ERR_HTTP2_COMPRESSION_ERROR/网页可能暂时无法连接,或者它已永久性地移动到了新网址。
-
Image Compression
-
mysql 5.7 Transparent PageIO Compression原理及优缺点讲解
-
php简单开启gzip压缩方法(zlib.output_compression)_PHP
-
php中ob_gzhandler' conflicts with 'zlib output compression问题_PHP教程
-
spark取得lzo压缩文件报错 java.lang.ClassNotFoundException: Class com.hadoop.compression sparkhadooplzo压缩class
-
spark取得lzo压缩文件报错 java.lang.ClassNotFoundException: Class com.hadoop.compression sparkhadooplzo压缩class