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.
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 integersa1, a2, ..., an (1 ≤ ai ≤ 50) — number ai stands for the number of sockets on the i-th supply-line filter.
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.
3 5 3 3 1 2
1
4 7 2 3 3 2 4
2
5 5 1 1 3 1 2 1
-1
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; }