A. USB Flash Drives
题目连接:
http://www.codeforces.com/contest/609/problem/A
Description
Sean is trying to save a large file to a USB flash drive. He has n USB flash drives with capacities equal to a1, a2, ..., an megabytes. The file size is equal to m megabytes.
Find the minimum number of USB flash drives needed to write Sean's file, if he can split the file between drives.
Input
The first line contains positive integer n (1 ≤ n ≤ 100) — the number of USB flash drives.
The second line contains positive integer m (1 ≤ m ≤ 105) — the size of Sean's file.
Each of the next n lines contains positive integer ai (1 ≤ ai ≤ 1000) — the sizes of USB flash drives in megabytes.
It is guaranteed that the answer exists, i. e. the sum of all ai is not less than m.
Output
Print the minimum number of USB flash drives to write Sean's file, if he can split the file between drives.
Sample Input
3
5
2
1
3
Sample Output
2
Hint
题意
给你n个u盘,每个u盘的容量为a[i],给你一个M大小的文件,问你最少用多少个U盘就可以装完。
题解:
贪心。将U盘按照从大到小排序,然后依次装进U盘,知道M大小被装完。
代码
#include<bits/stdc++.h>
using namespace std;
int a[105];
bool cmp(int A,int B)
{
return A>B;
}
int main()
{
int n;scanf("%d",&n);
int m;scanf("%d",&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+1+n,cmp);
int ans = 0;
for(int i=1;i<=n;i++)
{
m-=a[i];
ans++;
if(m<=0)break;
}
printf("%d\n",ans);
}