A. Sockets

來源:互聯網
上載者:User
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 nmk (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
integersa1, 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 33 1 2
output
1
input
4 7 23 3 2 4
output
2
input
5 5 11 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.

解題說明:此題是把m個裝置藉助導線和過濾器(類似於接線板)通上電,這裡導線有k根,過濾器有n個,每個上面插孔數目都不一樣,問最少要用多少個過濾器。顯然,如果導線數目足夠的話,我們一個裝置分配一根導線,這樣不需要過濾器,但是導線數目不夠時,我們要先把導線接到過濾器上,然後把裝置通過過濾器接上電,這樣一個過濾器可以同時給多個裝置供電。求解過程為首先判斷導線和裝置的數目,在導線不夠時對過濾器進行排序,每次選擇介面最多的過濾器,然後再判斷裝置和導線的情況,直到所有裝置都通上電,或是過濾器全部用完。

#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <algorithm>using namespace std;int main(){int n,m,k;int i,a[51];int count;scanf("%d %d %d",&n,&m,&k);for(i=0;i<n;i++){scanf("%d",&a[i]);}sort(a,a+n);if(k>=m){printf("0\n");}else{count=0;for(i=n-1;i>=0;i--){m=m-a[i];k--;count++;if(k>=m){printf("%d\n",count);break;}}if(i<0){printf("-1\n");}}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.