欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C. Songs Compression(贪心)

程序员文章站 2024-03-12 23:47:56
...

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;
}