Description
New Year is coming! Our big boss Wine93 will distribute some “Red Package”, just like Alipay and Wechat.
Wine93 has m yuan, he decides to distribute them to n people and everyone can get some money(0 yuan is not allowed and everyone’s money is an integer), Now k people has gotten money, it’s your turn to get “Red Package”, you want to know, at least how much money to give you, then you can must become the “lucky man”. and the m yuan must be used out.
Noting that if someone’s money is strictly much than others’, than he is “lucky man”.
Input
Input starts with an integer T (T <= 50) denoting the number of test case.
For each test case, three integers n, m, k (1 <= k < n <= 100000, 0< m <= 100000000) will be given.
Next line contains k integers, denoting the money that k people get. You can assume that the k integers’ summation is no more than m.
For each test case, three integers n, m, k (1 <= k < n <= 100000, 0< m <= 100000000) will be given.
Next line contains k integers, denoting the money that k people get. You can assume that the k integers’ summation is no more than m.
Output
Ouput the least money that you need to become the “lucky man”, if it is impossible, output “Impossible” (no quote).
Sample Input
3 3 5 2 2 1 4 10 2 2 3 4 15 2 3 5
Sample Output
Impossible 4 6
Hint
无
求出可能的钱数区间,在区间内用二分法找到一个值使它大于k人的钱数,并大于剩下n-k-1人的最大钱数。
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 int n,m,k,sum,ans,a[100000+11],ri,le,mid,max0; 5 bool f(int x) 6 { 7 if(x <= max0) 8 return 0; 9 if(x > (m-sum-x-(n-k-2))) 10 return 1; 11 return 0; 12 } 13 int main() 14 { 15 int t; 16 scanf("%d",&t); 17 while(t--) 18 { 19 sum=0; 20 max0=0; 21 scanf("%d %d %d",&n,&m,&k); 22 for(int i = 0 ; i < k ; i++) 23 { 24 scanf("%d",&a[i]); 25 max0=max(max0,a[i]); 26 sum+=a[i]; 27 } 28 if((m-sum-(n-k-1))<= max0) 29 { 30 printf("Impossible "); 31 continue; 32 } 33 le=max0; 34 ri=m-sum-(n-k-1); 35 while(le <= ri) 36 { 37 mid=( le + ri)/2; 38 if(f(mid)) 39 { 40 ans=mid; 41 ri=mid-1; 42 } 43 else 44 { 45 le=mid+1; 46 } 47 } 48 printf("%d ",ans); 49 } 50 }