zoukankan      html  css  js  c++  java
  • Sockets

    原题链接:点击打开链接
    A. Sockets
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Vasya has got many devices that work on electricity. He's got n supply-line filters to plug the devices, the i-th supply-line filter has ai sockets.

    Overall Vasya has got m devices and k electrical sockets in his flat, he can plug the devices or supply-line filters directly. Of course, he can plug the supply-line filter to any other supply-line filter. The device (or the supply-line filter) is considered plugged to electricity if it is either plugged to one of k electrical sockets, or if it is plugged to some supply-line filter that is in turn plugged to electricity.

    What minimum number of supply-line filters from the given set will Vasya need to plug all the devices he has to electricity? Note that all devices and supply-line filters take one socket for plugging and that he can use one socket to plug either one device or one supply-line filter.

    Input

    The first line contains three integers n, m, k (1 ≤ n, m, k ≤ 50) — the number of supply-line filters, the number of devices and the number of sockets that he can plug to directly, correspondingly. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 50) — number ai stands for the number of sockets on the i-th supply-line filter.

    Output

    Print a single number — the minimum number of supply-line filters that is needed to plug all the devices to electricity. If it is impossible to plug all the devices even using all the supply-line filters, print -1.

    Sample test(s)
    Input
    3 5 3
    3 1 2
    
    Output
    1
    
    Input
    4 7 2
    3 3 2 4
    
    Output
    2
    
    Input
    5 5 1
    1 3 1 2 1
    
    Output
    -1
    
    Note

    In the first test case he can plug the first supply-line filter directly to electricity. After he plug it, he get 5 (3 on the supply-line filter and 2 remaining sockets for direct plugging) available sockets to plug. Thus, one filter is enough to plug 5 devices.

    One of the optimal ways in the second test sample is to plug the second supply-line filter directly and plug the fourth supply-line filter to one of the sockets in the second supply-line filter. Thus, he gets exactly 7 sockets, available to plug: one to plug to the electricity directly, 2 on the second supply-line filter, 4 on the fourth supply-line filter. There's no way he can plug 7 devices if he use one supply-line filter.


    AC Code:

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
        int n, m, k, cnt;  //cnt为已通电插孔数
        int a[51], i;  //每个插座拥有的插孔数量
        while(cin >> n >> m >> k)
        {
            cnt = k;
            for(i = 0; i < n; i++)
                cin >> a[i];
            sort(a, a + i);
            while(cnt < m && i)
            {
                cnt += a[i - 1] - 1;
                i--;
            }
            if(cnt < m) cout << -1 << endl;
            else cout << n - i << endl;
        }
        return 0;
    }
    


  • 相关阅读:
    云如何解决安全问题 狼人:
    存储安全 系统的最后一道防线 狼人:
    云安全仍是企业决策者最大担心 狼人:
    骇客宣称已入侵多家认证机构 波及微软、谷歌 狼人:
    盘点云计算服务中的隐患 狼人:
    云服务安全吗?美国政府用实际行动告诉你 狼人:
    微软高层称移动设备越多 对信息安全需更多考量 狼人:
    云计算需要让安全优先 狼人:
    金山网络两月被黑4次 入侵黑客留名挑衅 狼人:
    惠普推出全新企业级安全软件 狼人:
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910552.html
Copyright © 2011-2022 走看看