CF609A USB Flash Drives

題目描述

Sean is trying to save a large file to a USB flash drive. He has nn USB flash drives with capacities equal to a_{1},a_{2},...,a_{n}a1​,a2​,...,an​ megabytes. The file size is equal to mm megabytes.

Find the minimum number of USB flash drives needed to write Sean's file, if he can split the file between drives.

輸入格式

The first line contains positive integer nn ( 1<=n<=1001<=n<=100 ) — the number of USB flash drives.

The second line contains positive integer mm ( 1<=m<=10^{5}1<=m<=105 ) — the size of Sean's file.

Each of the next nn lines contains positive integer a_{i}ai​ ( 1<=a_{i}<=10001<=ai​<=1000 ) — the sizes of USB flash drives in megabytes.

It is guaranteed that the answer exists, i. e. the sum of all a_{i}ai​ is not less than mm .

輸出格式

Print the minimum number of USB flash drives to write Sean's file, if he can split the file between drives.

題意翻譯

Sean正在把一個大文件考到n個U盤裏。這個文件足有m MB那麼大。第i個U盤的容量是a[i]。 假設Sean能把文件分裝到多個U盤中,請求出他最少需要用多少個U盤來考這個文件。

輸入輸出格式

輸入格式

第一行包括一個整數n(1<=n<=100)-U盤的個數。

第二行包含一個整數m(1<=m<=10^5)-文件的大小。

以下n行每一行包括一個整數a[i] (1<=a[i]<=1000)-第i個U盤的大小。

確定答案出現-也就是說所有a[i]的總和不小於m。

輸出格式

輸出至少要多少U盤。

翻譯提供者:sunhaina061031

輸入輸出樣例

輸入 #1

3
5
2
1
3

輸出 #1

2

輸入 #2

3
6
2
3
2

輸出 #2

3

輸入 #3

2
5
5
10

輸出 #3

1

說明/提示

In the first example Sean needs only two USB flash drives — the first and the third.

In the second example Sean needs all three USB flash drives.

In the third example Sean needs only one USB flash drive and he can use any available USB flash drive — the first or the second.

 

解題思路:貪心思想,直接對U盤容量排序,然後依次選擇大的就行。


#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=110;
int a[maxn];
bool cmp(int a,int b){
    return a>b;
}
int main(){
    int n,m,ans=0;
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    sort(a,a+n,cmp);
    for(int i=0;i<n;i++){
        m=m-a[i];
        ans++;
        if(m<=0)
            break;
    }
    cout<<ans<<endl;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章