zoukankan      html  css  js  c++  java
  • Codeforces Educational Codeforces Round 3 A. USB Flash Drives 水题

    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
    ",ans);
    }
  • 相关阅读:
    redis同步指定key数据到其他redis中
    Golang 生成随机数
    怎么理解“平均负载”? 进行分析等
    Golang打印空心金字塔for循环实现
    python十几行代码实现三级菜单
    mysql增量恢复
    python内建函数
    python练习题总结
    迭代器和生成器
    python基础数据类型
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5060199.html
Copyright © 2011-2022 走看看